Skip to content

Commit e68bd6f

Browse files
committed
New JAR for flocking example
1 parent d039035 commit e68bd6f

File tree

4 files changed

+93
-217
lines changed

4 files changed

+93
-217
lines changed

content/examples/Topics/Simulate/Flocking/applet/Boid.pde

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ class Boid {
99
float maxforce; // Maximum steering force
1010
float maxspeed; // Maximum speed
1111

12-
Boid(PVector l, float ms, float mf) {
12+
Boid(PVector l, float ms, float mf) {
1313
acc = new PVector(0,0);
1414
vel = new PVector(random(-1,1),random(-1,1));
15-
loc = l.copy();
15+
loc = l.get();
1616
r = 2.0;
1717
maxspeed = ms;
1818
maxforce = mf;
1919
}
20-
20+
2121
void run(ArrayList boids) {
2222
flock(boids);
2323
update();
@@ -31,15 +31,15 @@ class Boid {
3131
PVector ali = align(boids); // Alignment
3232
PVector coh = cohesion(boids); // Cohesion
3333
// Arbitrarily weight these forces
34-
sep.mult(2.0);
34+
sep.mult(1.5);
3535
ali.mult(1.0);
3636
coh.mult(1.0);
3737
// Add the force vectors to acceleration
3838
acc.add(sep);
3939
acc.add(ali);
4040
acc.add(coh);
4141
}
42-
42+
4343
// Method to update location
4444
void update() {
4545
// Update velocity
@@ -54,7 +54,7 @@ class Boid {
5454
void seek(PVector target) {
5555
acc.add(steer(target,false));
5656
}
57-
57+
5858
void arrive(PVector target) {
5959
acc.add(steer(target,true));
6060
}
@@ -75,12 +75,13 @@ class Boid {
7575
// Steering = Desired minus Velocity
7676
steer = target.sub(desired,vel);
7777
steer.limit(maxforce); // Limit to maximum steering force
78-
} else {
78+
}
79+
else {
7980
steer = new PVector(0,0);
8081
}
8182
return steer;
8283
}
83-
84+
8485
void render() {
8586
// Draw a triangle rotated in the direction of velocity
8687
float theta = vel.heading2D() + PI/2;
@@ -96,7 +97,7 @@ class Boid {
9697
endShape();
9798
popMatrix();
9899
}
99-
100+
100101
// Wraparound
101102
void borders() {
102103
if (loc.x < -r) loc.x = width+r;
@@ -108,55 +109,72 @@ class Boid {
108109
// Separation
109110
// Method checks for nearby boids and steers away
110111
PVector separate (ArrayList boids) {
111-
float desiredseparation = 25.0;
112-
PVector sum = new PVector(0,0,0);
112+
float desiredseparation = 20.0;
113+
PVector steer = new PVector(0,0,0);
113114
int count = 0;
114115
// For every boid in the system, check if it's too close
115116
for (int i = 0 ; i < boids.size(); i++) {
116117
Boid other = (Boid) boids.get(i);
117-
float d = loc.dist(other.loc);
118+
float d = PVector.dist(loc,other.loc);
118119
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
119120
if ((d > 0) && (d < desiredseparation)) {
120121
// Calculate vector pointing away from neighbor
121-
PVector diff = loc.sub(loc,other.loc);
122+
PVector diff = PVector.sub(loc,other.loc);
122123
diff.normalize();
123124
diff.div(d); // Weight by distance
124-
sum.add(diff);
125+
steer.add(diff);
125126
count++; // Keep track of how many
126127
}
127128
}
128129
// Average -- divide by how many
129130
if (count > 0) {
130-
sum.div((float)count);
131+
steer.div((float)count);
131132
}
132-
return sum;
133+
134+
// As long as the vector is greater than 0
135+
if (steer.mag() > 0) {
136+
// Implement Reynolds: Steering = Desired - Velocity
137+
steer.normalize();
138+
steer.mult(maxspeed);
139+
steer.sub(vel);
140+
steer.limit(maxforce);
141+
}
142+
return steer;
133143
}
134-
144+
135145
// Alignment
136146
// For every nearby boid in the system, calculate the average velocity
137147
PVector align (ArrayList boids) {
138-
float neighbordist = 50.0;
139-
PVector sum = new PVector(0,0,0);
148+
float neighbordist = 25.0;
149+
PVector steer = new PVector(0,0,0);
140150
int count = 0;
141151
for (int i = 0 ; i < boids.size(); i++) {
142152
Boid other = (Boid) boids.get(i);
143-
float d = loc.dist(other.loc);
153+
float d = PVector.dist(loc,other.loc);
144154
if ((d > 0) && (d < neighbordist)) {
145-
sum.add(other.vel);
155+
steer.add(other.vel);
146156
count++;
147157
}
148158
}
149159
if (count > 0) {
150-
sum.div((float)count);
151-
sum.limit(maxforce);
160+
steer.div((float)count);
152161
}
153-
return sum;
162+
163+
// As long as the vector is greater than 0
164+
if (steer.mag() > 0) {
165+
// Implement Reynolds: Steering = Desired - Velocity
166+
steer.normalize();
167+
steer.mult(maxspeed);
168+
steer.sub(vel);
169+
steer.limit(maxforce);
170+
}
171+
return steer;
154172
}
155173

156174
// Cohesion
157175
// For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
158176
PVector cohesion (ArrayList boids) {
159-
float neighbordist = 50.0;
177+
float neighbordist = 25.0;
160178
PVector sum = new PVector(0,0); // Start with empty vector to accumulate all locations
161179
int count = 0;
162180
for (int i = 0 ; i < boids.size(); i++) {
@@ -175,3 +193,4 @@ class Boid {
175193
}
176194
}
177195

196+
1.74 KB
Binary file not shown.

0 commit comments

Comments
 (0)