-
Notifications
You must be signed in to change notification settings - Fork 3
/
BoidTest.pde
169 lines (139 loc) · 4.27 KB
/
BoidTest.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
167
168
169
class Boid {
protected PVector pos;
protected PVector vel;
protected PVector acc;
protected boolean isAlive;
protected float maxForce; //max steering force
protected float maxSpeed;
protected final float SIZE;
protected final float SEP_WEIGHT;
protected final float ALI_WEIGHT;
protected final float COH_WEIGHT;
protected final float INS_WEIGHT;
public Boid(float x, float y, float SEP_WEIGHT, float ALI_WEIGHT, float COH_WEIGHT, float INS_WEIGHT) {
//hard-coded traits
this.SIZE = 3;
this.maxForce = 0.1;
this.maxSpeed = 3;
//param-defined traits
this.SEP_WEIGHT = SEP_WEIGHT;
this.ALI_WEIGHT = ALI_WEIGHT;
this.COH_WEIGHT = COH_WEIGHT;
this.INS_WEIGHT = INS_WEIGHT;
//initialization
isAlive = true;
acc = new PVector(0,0);
float angle = random(TWO_PI);
vel = new PVector(cos(angle), sin(angle));
pos = new PVector(x,y);
} //Constructor
void flock (ArrayList<Boid> curr, ArrayList <Boid> other) {
//left for individual boid implementation
} //flock
void run (ArrayList <Boid> curr, ArrayList <Boid> other) {
flock(curr, other); //implemented in children
update(); //update pos, vel, acc
render(); //draw to screen
} //run
void applyForce(PVector force) {
acc.add(force);
} //applyForce
void update() {
//update vel, pos
vel.add(acc);
vel.limit(maxSpeed);
pos.add(vel);
//inter-wall movement
if (pos.x >= width || pos.x <= 0)
vel.x = -1 * vel.x;
if (pos.y >= height || pos.y <= 0)
vel.y = -1 * vel.y;
//acceleration reset
acc.mult(0);
} //update
void render() {
float theta = vel.heading() + radians(90);
noFill();
pushMatrix();
translate(pos.x, pos.y);
rotate(theta);
beginShape(TRIANGLES);
vertex(0, -SIZE * 2);
vertex( -SIZE, SIZE * 2);
vertex(SIZE, SIZE * 2);
endShape();
popMatrix();
} //render
PVector sep(ArrayList <Boid> boids) {
float desiredSep = 25.0f;
PVector steer = new PVector(0,0,0);
int count = 0;
for (Boid other : boids) {
float d = PVector.dist(pos, other.pos);
if (d > 0 && d < desiredSep) {
PVector diff = PVector.sub(pos, other.pos);
diff.normalize();
diff.div(d);
steer.add(diff);
count++;
} //if
} //for
if (count > 0)
steer.div((float)count);
if (steer.mag() > 0) {
steer.normalize();
steer.mult(maxSpeed);
steer.sub(vel);
steer.limit(maxForce);
} //if
return steer;
} //sep
PVector ali (ArrayList <Boid> boids) {
float neighborDist = 50;
PVector sum = new PVector (0,0);
int count = 0;
for (Boid other : boids) {
float d = PVector.dist(pos, other.pos);
if ((d > 0) && (d < neighborDist)) {
sum.add(other.vel);
count++;
} //if
} //for
if (count > 0) {
sum.div((float) count);
sum.normalize();
sum.mult(maxSpeed);
PVector steer = PVector.sub(sum, vel);
steer.limit(maxForce);
return steer;
} else {
return new PVector (0,0);
} //if
} //ali
private PVector seek (PVector target) {
PVector desired = PVector.sub(target, pos);
desired.normalize();
desired.mult(maxSpeed);
PVector steer = PVector.sub(desired,vel);
steer.limit(maxForce);
return steer;
} //seek
PVector coh (ArrayList <Boid> boids) {
float neighborDist = 50;
PVector sum = new PVector (0,0);
int count = 0;
for (Boid other : boids) {
float d = PVector.dist(pos, other.pos);
if ((d > 0) && (d < neighborDist)) {
sum.add(other.pos);
count++;
} //if
} //for
if (count > 0) {
sum.div(count);
return seek(sum);
} else {
return new PVector(0,0);
} //if
} //coh
} //Boid