Skip to content

Commit 02ebef9

Browse files
committed
Changes to examples to add PVector to Dan's examples
1 parent 7fd1a0d commit 02ebef9

File tree

25 files changed

+162
-1422
lines changed

25 files changed

+162
-1422
lines changed

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
class Boid {
44

5-
Vector3D loc;
6-
Vector3D vel;
7-
Vector3D acc;
5+
PVector loc;
6+
PVector vel;
7+
PVector acc;
88
float r;
99
float maxforce; // Maximum steering force
1010
float maxspeed; // Maximum speed
1111

12-
Boid(Vector3D l, float ms, float mf) {
13-
acc = new Vector3D(0,0);
14-
vel = new Vector3D(random(-1,1),random(-1,1));
12+
Boid(PVector l, float ms, float mf) {
13+
acc = new PVector(0,0);
14+
vel = new PVector(random(-1,1),random(-1,1));
1515
loc = l.copy();
1616
r = 2.0;
1717
maxspeed = ms;
@@ -27,9 +27,9 @@ class Boid {
2727

2828
// We accumulate a new acceleration each time based on three rules
2929
void flock(ArrayList boids) {
30-
Vector3D sep = separate(boids); // Separation
31-
Vector3D ali = align(boids); // Alignment
32-
Vector3D coh = cohesion(boids); // Cohesion
30+
PVector sep = separate(boids); // Separation
31+
PVector ali = align(boids); // Alignment
32+
PVector coh = cohesion(boids); // Cohesion
3333
// Arbitrarily weight these forces
3434
sep.mult(2.0);
3535
ali.mult(1.0);
@@ -48,23 +48,23 @@ class Boid {
4848
vel.limit(maxspeed);
4949
loc.add(vel);
5050
// Reset accelertion to 0 each cycle
51-
acc.setXYZ(0,0,0);
51+
acc.mult(0);
5252
}
5353

54-
void seek(Vector3D target) {
54+
void seek(PVector target) {
5555
acc.add(steer(target,false));
5656
}
5757

58-
void arrive(Vector3D target) {
58+
void arrive(PVector target) {
5959
acc.add(steer(target,true));
6060
}
6161

6262
// A method that calculates a steering vector towards a target
6363
// Takes a second argument, if true, it slows down as it approaches the target
64-
Vector3D steer(Vector3D target, boolean slowdown) {
65-
Vector3D steer; // The steering vector
66-
Vector3D desired = target.sub(target,loc); // A vector pointing from the location to the target
67-
float d = desired.magnitude(); // Distance from the target is the magnitude of the vector
64+
PVector steer(PVector target, boolean slowdown) {
65+
PVector steer; // The steering vector
66+
PVector desired = target.sub(target,loc); // A vector pointing from the location to the target
67+
float d = desired.mag(); // Distance from the target is the magnitude of the vector
6868
// If the distance is greater than 0, calc steering (otherwise return zero vector)
6969
if (d > 0) {
7070
// Normalize desired
@@ -76,7 +76,7 @@ class Boid {
7676
steer = target.sub(desired,vel);
7777
steer.limit(maxforce); // Limit to maximum steering force
7878
} else {
79-
steer = new Vector3D(0,0);
79+
steer = new PVector(0,0);
8080
}
8181
return steer;
8282
}
@@ -107,18 +107,18 @@ class Boid {
107107

108108
// Separation
109109
// Method checks for nearby boids and steers away
110-
Vector3D separate (ArrayList boids) {
110+
PVector separate (ArrayList boids) {
111111
float desiredseparation = 25.0;
112-
Vector3D sum = new Vector3D(0,0,0);
112+
PVector sum = new PVector(0,0,0);
113113
int count = 0;
114114
// For every boid in the system, check if it's too close
115115
for (int i = 0 ; i < boids.size(); i++) {
116116
Boid other = (Boid) boids.get(i);
117-
float d = loc.distance(loc,other.loc);
117+
float d = loc.dist(other.loc);
118118
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
119119
if ((d > 0) && (d < desiredseparation)) {
120120
// Calculate vector pointing away from neighbor
121-
Vector3D diff = loc.sub(loc,other.loc);
121+
PVector diff = loc.sub(loc,other.loc);
122122
diff.normalize();
123123
diff.div(d); // Weight by distance
124124
sum.add(diff);
@@ -134,13 +134,13 @@ class Boid {
134134

135135
// Alignment
136136
// For every nearby boid in the system, calculate the average velocity
137-
Vector3D align (ArrayList boids) {
137+
PVector align (ArrayList boids) {
138138
float neighbordist = 50.0;
139-
Vector3D sum = new Vector3D(0,0,0);
139+
PVector sum = new PVector(0,0,0);
140140
int count = 0;
141141
for (int i = 0 ; i < boids.size(); i++) {
142142
Boid other = (Boid) boids.get(i);
143-
float d = loc.distance(loc,other.loc);
143+
float d = loc.dist(other.loc);
144144
if ((d > 0) && (d < neighbordist)) {
145145
sum.add(other.vel);
146146
count++;
@@ -155,13 +155,13 @@ class Boid {
155155

156156
// Cohesion
157157
// For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
158-
Vector3D cohesion (ArrayList boids) {
158+
PVector cohesion (ArrayList boids) {
159159
float neighbordist = 50.0;
160-
Vector3D sum = new Vector3D(0,0,0); // Start with empty vector to accumulate all locations
160+
PVector sum = new PVector(0,0); // Start with empty vector to accumulate all locations
161161
int count = 0;
162162
for (int i = 0 ; i < boids.size(); i++) {
163163
Boid other = (Boid) boids.get(i);
164-
float d = loc.distance(loc,other.loc);
164+
float d = loc.dist(other.loc);
165165
if ((d > 0) && (d < neighbordist)) {
166166
sum.add(other.loc); // Add location
167167
count++;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void setup() {
1616
flock = new Flock();
1717
// Add an initial set of boids into the system
1818
for (int i = 0; i < 150; i++) {
19-
flock.addBoid(new Boid(new Vector3D(width/2,height/2),2.0,0.05));
19+
flock.addBoid(new Boid(new PVector(width/2,height/2),2.0,0.05));
2020
}
2121
smooth();
2222
}
@@ -28,5 +28,5 @@ void draw() {
2828

2929
// Add a new boid into the System
3030
void mousePressed() {
31-
flock.addBoid(new Boid(new Vector3D(mouseX,mouseY),2.0f,0.05f));
31+
flock.addBoid(new Boid(new PVector(mouseX,mouseY),2.0f,0.05f));
3232
}

content/examples/Topics/Simulate/Flocking/Vector3D.pde

Lines changed: 0 additions & 162 deletions
This file was deleted.

0 commit comments

Comments
 (0)