-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredator.pde
More file actions
62 lines (55 loc) · 1.65 KB
/
Predator.pde
File metadata and controls
62 lines (55 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Predator extends Bird{
Predator(){
position = new PVector(0, 0);
velocity = new PVector(0, 0);
}
Predator(int x_, int y_, PVector velocity_){
position = new PVector(x_, y_);
velocity = velocity_;
}
Predator(Bird b){
position = b.position;
velocity = b.velocity;
}
void drawBird(){
translate(position.x, height - position.y);
rotate(-velocity.heading()-PI/2);
fill(200, 0, 0);
triangle(0, 10, -4, -2, 4, -2);
fill(0);
rotate(velocity.heading()+PI/2);
translate(-position.x, -height + position.y);
}
void updateVelocity(){
if(predators.size() > 1){
PVector closestDisplacement = getClosestBird(this, predators);
PVector velocityChangeFromCollisionAviodance = closestDisplacement.setMag(1/closestDisplacement.mag());
velocityChangeFromCollisionAviodance.mult(-1);
velocity.add(closestDisplacement.mult(parameters[8]));
}
ArrayList<Bird> neighbors = getNeighbors(this, parameters[10], parameters[9]);
PVector averageDisplacement = new PVector();
for(Bird b : neighbors){
PVector displacement = PVector.sub(b.position, position);
if(displacement.x > width/2){
displacement.x -= width;
}
if(displacement.y > height/2){
displacement.y -= height;
}
averageDisplacement.add(displacement);
}
if(neighbors.size() > 0){
averageDisplacement.div(neighbors.size());
}else{
averageDisplacement = new PVector(0, 0);
}
velocity.add(averageDisplacement.mult(parameters[7]/parameters[10]));
if(velocity.mag() > 5){
velocity.setMag(velocity.mag() - (velocity.mag() - 5)*0.2);
}
if(velocity.mag() < 3){
velocity.setMag(velocity.mag() + (3 - velocity.mag())*0.4);
}
}
}