FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

temporarily reverting some PVector methods to be compatible with proc… · biddy/processing@fc10ac8 · GitHub

Repository navigation

Commit fc10ac8

Browse files
committed
temporarily reverting some PVector methods to be compatible with processingjs.
1 parent 7498da7 commit fc10ac8

1 file changed

Lines changed: 52 additions & 25 deletions

File tree

  • java/examples/Topics/Simulate/Flocking

‎java/examples/Topics/Simulate/Flocking/Boid.pde‎

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

12-
Boid(float x, float y) {
13-
acceleration = new PVector(0,0);
14-
velocity = PVector.random2D();
15-
location = new PVector(x,y);
12+
Boid(float x, float y) {
13+
acceleration = new PVector(0, 0);
14+
15+
// This is a new PVector method not yet implemented in JS
16+
// velocity = PVector.random2D();
17+
18+
// Leaving the code temporarily this way so that this example runs in JS
19+
float angle = random(TWO_PI);
20+
velocity = new PVector(cos(angle), sin(angle));
21+
22+
location = new PVector(x, y);
1623
r = 2.0;
1724
maxspeed = 2;
1825
maxforce = 0.03;
@@ -59,22 +66,30 @@ class Boid {
5966
// A method that calculates and applies a steering force towards a target
6067
// STEER = DESIRED MINUS VELOCITY
6168
PVector seek(PVector target) {
62-
PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target
69+
PVector desired = PVector.sub(target, location); // A vector pointing from the location to the target
6370
// Scale to maximum speed
64-
desired.setMag(maxspeed);
71+
desired.normalize();
72+
desired.mult(maxspeed);
73+
74+
// Above two lines of code below could be condensed with new PVector setMag() method
75+
// Not using this method until Processing.js catches up
76+
// desired.setMag(maxspeed);
77+
6578
// Steering = Desired minus Velocity
66-
PVector steer = PVector.sub(desired,velocity);
79+
PVector steer = PVector.sub(desired, velocity);
6780
steer.limit(maxforce); // Limit to maximum steering force
6881
return steer;
6982
}
7083

7184
void render() {
7285
// Draw a triangle rotated in the direction of velocity
73-
float theta = velocity.heading() + radians(90);
74-
fill(200,100);
86+
float theta = velocity.heading2D() + radians(90);
87+
// heading2D() above is now heading() but leaving old syntax until Processing.js catches up
88+
89+
fill(200, 100);
7590
stroke(255);
7691
pushMatrix();
77-
translate(location.x,location.y);
92+
translate(location.x, location.y);
7893
rotate(theta);
7994
beginShape(TRIANGLES);
8095
vertex(0, -r*2);
@@ -96,15 +111,15 @@ class Boid {
96111
// Method checks for nearby boids and steers away
97112
PVector separate (ArrayList<Boid> boids) {
98113
float desiredseparation = 25.0f;
99-
PVector steer = new PVector(0,0,0);
114+
PVector steer = new PVector(0, 0, 0);
100115
int count = 0;
101116
// For every boid in the system, check if it's too close
102117
for (Boid other : boids) {
103-
float d = PVector.dist(location,other.location);
118+
float d = PVector.dist(location, other.location);
104119
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
105120
if ((d > 0) && (d < desiredseparation)) {
106121
// Calculate vector pointing away from neighbor
107-
PVector diff = PVector.sub(location,other.location);
122+
PVector diff = PVector.sub(location, other.location);
108123
diff.normalize();
109124
diff.div(d); // Weight by distance
110125
steer.add(diff);
@@ -118,8 +133,13 @@ class Boid {
118133

119134
// As long as the vector is greater than 0
120135
if (steer.mag() > 0) {
136+
// First two lines of code below could be condensed with new PVector setMag() method
137+
// Not using this method until Processing.js catches up
138+
// steer.setMag(maxspeed);
139+
121140
// Implement Reynolds: Steering = Desired - Velocity
122-
steer.setMag(maxspeed);
141+
steer.normalize();
142+
steer.mult(maxspeed);
123143
steer.sub(velocity);
124144
steer.limit(maxforce);
125145
}
@@ -130,34 +150,41 @@ class Boid {
130150
// For every nearby boid in the system, calculate the average velocity
131151
PVector align (ArrayList<Boid> boids) {
132152
float neighbordist = 50;
133-
PVector sum = new PVector(0,0);
153+
PVector sum = new PVector(0, 0);
134154
int count = 0;
135155
for (Boid other : boids) {
136-
float d = PVector.dist(location,other.location);
156+
float d = PVector.dist(location, other.location);
137157
if ((d > 0) && (d < neighbordist)) {
138158
sum.add(other.velocity);
139159
count++;
140160
}
141161
}
142162
if (count > 0) {
143163
sum.div((float)count);
144-
sum.setMag(maxspeed);
145-
PVector steer = PVector.sub(sum,velocity);
164+
// First two lines of code below could be condensed with new PVector setMag() method
165+
// Not using this method until Processing.js catches up
166+
// sum.setMag(maxspeed);
167+
168+
// Implement Reynolds: Steering = Desired - Velocity
169+
sum.normalize();
170+
sum.mult(maxspeed);
171+
PVector steer = PVector.sub(sum, velocity);
146172
steer.limit(maxforce);
147173
return steer;
148-
} else {
149-
return new PVector(0,0);
174+
}
175+
else {
176+
return new PVector(0, 0);
150177
}
151178
}
152179

153180
// Cohesion
154181
// For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
155182
PVector cohesion (ArrayList<Boid> boids) {
156183
float neighbordist = 50;
157-
PVector sum = new PVector(0,0); // Start with empty vector to accumulate all locations
184+
PVector sum = new PVector(0, 0); // Start with empty vector to accumulate all locations
158185
int count = 0;
159186
for (Boid other : boids) {
160-
float d = PVector.dist(location,other.location);
187+
float d = PVector.dist(location, other.location);
161188
if ((d > 0) && (d < neighbordist)) {
162189
sum.add(other.location); // Add location
163190
count++;
@@ -166,10 +193,10 @@ class Boid {
166193
if (count > 0) {
167194
sum.div(count);
168195
return seek(sum); // Steer towards the location
169-
} else {
170-
return new PVector(0,0);
196+
}
197+
else {
198+
return new PVector(0, 0);
171199
}
172200
}
173201
}
174202

175-

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL