-
Notifications
You must be signed in to change notification settings - Fork 1
/
Boid.pde
166 lines (153 loc) · 3.76 KB
/
Boid.pde
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//----------------------------------------------------------
// Boid class
// heavily based on Daniel Shiffman's Flocking code
// Source: https://processing.org/examples/flocking.html
//----------------------------------------------------------
class Boid {
float r, speed, force;
String type;
PVector pos, vel, acc;
Boid(String type, float x, float y){
pos = new PVector(x,y);
vel = PVector.random2D();
acc = new PVector(0,0);
switch(type){
// Eich Missiles
case "I-Type":
r = 13.0;
speed = 7;
force = 0.03;
break;
// Ploor Missiles
case "P-Type":
break;
// Eddore Missiles
case "E-Type":
break;
// Spy Ship
case "S-Type":
break;
}
}
void update(ArrayList<Boid> boids){
flock(boids);
vel.add(acc);
vel.limit(speed);
pos.add(vel);
acc.mult(0);
}
void display() {
float theta = vel.heading() + radians(90);
fill(255,0, 0);
pushMatrix();
translate(pos.x, pos.y);
rotate(theta);
beginShape(TRIANGLES);
vertex(0, -r*2);
vertex(-r, r*2);
vertex(r, r*2);
endShape();
popMatrix();
}
void applyForce(PVector force){
acc.add(force);
}
void flock(ArrayList<Boid> boids){
PVector sep = separation(boids);
PVector ali = alignment(boids);
PVector coh = cohesion(boids);
PVector tar = target(new PVector(player.x,player.y));
sep.mult(5.5);
ali.mult(1.0);
coh.mult(1.0);
tar.mult(4.0);
applyForce(sep);
applyForce(ali);
applyForce(coh);
applyForce(tar);
}
PVector separation(ArrayList<Boid> boids){
float separation = 100.0;
PVector steer = new PVector(0,0);
float count = 0.0;
for(int i=0; i<boids.size(); i++){
Boid b = boids.get(i);
float dist = PVector.dist(pos, b.pos);
if(dist > 0 && dist < separation){
PVector diff = PVector.sub(pos, b.pos);
diff.normalize();
diff.div(dist);
steer.add(diff);
count += 1.0;
}
}
if(count > 0.0){
steer.div(count);
}
if(steer.mag() > 0){
steer.normalize();
steer.mult(speed);
steer.sub(vel);
steer.limit(force);
}
return steer;
}
PVector alignment(ArrayList<Boid> boids){
float neighborDist = 50.0;
PVector sum = new PVector(0,0);
float count = 0.0;
for(int i=0; i<boids.size(); i++){
Boid b = boids.get(i);
float dist = PVector.dist(pos,b.pos);
if( dist > 0 && dist < neighborDist){
sum.add(b.vel);
count += 1.0;
}
}
if (count > 0.0){
sum.div(count);
sum.normalize();
sum.mult(speed);
PVector steer = PVector.sub(sum, vel);
steer.limit(force);
return steer;
} else {
return new PVector(0,0);
}
}
PVector cohesion(ArrayList<Boid> boids){
float neighborDist = 50.0;
PVector sum = new PVector(0,0);
float count = 0.0;
for(int i=0; i<boids.size(); i++){
Boid b = boids.get(i);
float dist = PVector.dist(pos,b.pos);
if( dist > 0 && dist < neighborDist){
sum.add(b.pos);
count += 1.0;
}
}
if (count > 0.0){
sum.div(count);
return seek(sum);
} else {
return new PVector(0,0);
}
}
PVector seek(PVector tar){
PVector targetDirection = PVector.sub(tar, pos);
targetDirection.normalize();
targetDirection.mult(speed);
PVector steer = PVector.sub(targetDirection, vel);
steer.limit(force);
return steer;
}
PVector target(PVector p){
PVector targetDirection = PVector.sub(p, pos);
targetDirection.normalize();
targetDirection.mult(speed);
PVector steer = PVector.sub(targetDirection, vel);
steer.limit(force);
return steer;
}
}