Skip to content

Commit e9d3aaf

Browse files
committed
cleanup and fixes for Simulate examples processing#448
1 parent ce69398 commit e9d3aaf

File tree

17 files changed

+131
-147
lines changed

17 files changed

+131
-147
lines changed

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

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

33
class Boid {
44

5-
PVector location;
5+
PVector position;
66
PVector velocity;
77
PVector acceleration;
88
float r;
@@ -19,7 +19,7 @@ class Boid {
1919
float angle = random(TWO_PI);
2020
velocity = new PVector(cos(angle), sin(angle));
2121

22-
location = new PVector(x, y);
22+
position = new PVector(x, y);
2323
r = 2.0;
2424
maxspeed = 2;
2525
maxforce = 0.03;
@@ -52,21 +52,21 @@ class Boid {
5252
applyForce(coh);
5353
}
5454

55-
// Method to update location
55+
// Method to update position
5656
void update() {
5757
// Update velocity
5858
velocity.add(acceleration);
5959
// Limit speed
6060
velocity.limit(maxspeed);
61-
location.add(velocity);
61+
position.add(velocity);
6262
// Reset accelertion to 0 each cycle
6363
acceleration.mult(0);
6464
}
6565

6666
// A method that calculates and applies a steering force towards a target
6767
// STEER = DESIRED MINUS VELOCITY
6868
PVector seek(PVector target) {
69-
PVector desired = PVector.sub(target, location); // A vector pointing from the location to the target
69+
PVector desired = PVector.sub(target, position); // A vector pointing from the position to the target
7070
// Scale to maximum speed
7171
desired.normalize();
7272
desired.mult(maxspeed);
@@ -89,7 +89,7 @@ class Boid {
8989
fill(200, 100);
9090
stroke(255);
9191
pushMatrix();
92-
translate(location.x, location.y);
92+
translate(position.x, position.y);
9393
rotate(theta);
9494
beginShape(TRIANGLES);
9595
vertex(0, -r*2);
@@ -101,10 +101,10 @@ class Boid {
101101

102102
// Wraparound
103103
void borders() {
104-
if (location.x < -r) location.x = width+r;
105-
if (location.y < -r) location.y = height+r;
106-
if (location.x > width+r) location.x = -r;
107-
if (location.y > height+r) location.y = -r;
104+
if (position.x < -r) position.x = width+r;
105+
if (position.y < -r) position.y = height+r;
106+
if (position.x > width+r) position.x = -r;
107+
if (position.y > height+r) position.y = -r;
108108
}
109109

110110
// Separation
@@ -115,11 +115,11 @@ class Boid {
115115
int count = 0;
116116
// For every boid in the system, check if it's too close
117117
for (Boid other : boids) {
118-
float d = PVector.dist(location, other.location);
118+
float d = PVector.dist(position, other.position);
119119
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
120120
if ((d > 0) && (d < desiredseparation)) {
121121
// Calculate vector pointing away from neighbor
122-
PVector diff = PVector.sub(location, other.location);
122+
PVector diff = PVector.sub(position, other.position);
123123
diff.normalize();
124124
diff.div(d); // Weight by distance
125125
steer.add(diff);
@@ -153,7 +153,7 @@ class Boid {
153153
PVector sum = new PVector(0, 0);
154154
int count = 0;
155155
for (Boid other : boids) {
156-
float d = PVector.dist(location, other.location);
156+
float d = PVector.dist(position, other.position);
157157
if ((d > 0) && (d < neighbordist)) {
158158
sum.add(other.velocity);
159159
count++;
@@ -178,21 +178,21 @@ class Boid {
178178
}
179179

180180
// Cohesion
181-
// For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
181+
// For the average position (i.e. center) of all nearby boids, calculate steering vector towards that position
182182
PVector cohesion (ArrayList<Boid> boids) {
183183
float neighbordist = 50;
184-
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 positions
185185
int count = 0;
186186
for (Boid other : boids) {
187-
float d = PVector.dist(location, other.location);
187+
float d = PVector.dist(position, other.position);
188188
if ((d > 0) && (d < neighbordist)) {
189-
sum.add(other.location); // Add location
189+
sum.add(other.position); // Add position
190190
count++;
191191
}
192192
}
193193
if (count > 0) {
194194
sum.div(count);
195-
return seek(sum); // Steer towards the location
195+
return seek(sum); // Steer towards the position
196196
}
197197
else {
198198
return new PVector(0, 0);

content/examples/Topics/Simulate/ForcesWithVectors/ForcesWithVectors.pde

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ void setup() {
2727

2828
void draw() {
2929
background(0);
30-
30+
3131
// Draw water
3232
liquid.display();
3333

3434
for (Mover mover : movers) {
35-
35+
3636
// Is the Mover in the liquid?
3737
if (liquid.contains(mover)) {
3838
// Calculate drag force
@@ -45,16 +45,15 @@ void draw() {
4545
PVector gravity = new PVector(0, 0.1*mover.mass);
4646
// Apply gravity
4747
mover.applyForce(gravity);
48-
48+
4949
// Update and display
5050
mover.update();
5151
mover.display();
5252
mover.checkEdges();
5353
}
54-
54+
5555
fill(255);
56-
text("click mouse to reset",10,30);
57-
56+
text("click mouse to reset", 10, 30);
5857
}
5958

6059
void mousePressed() {
@@ -66,4 +65,4 @@ void reset() {
6665
for (int i = 0; i < movers.length; i++) {
6766
movers[i] = new Mover(random(0.5, 3), 40+i*70, 0);
6867
}
69-
}
68+
}

content/examples/Topics/Simulate/ForcesWithVectors/Liquid.pde

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
* Bodies experience gravity continuously
77
* Bodies experience fluid resistance when in "water"
88
*/
9-
10-
// Liquid class
11-
class Liquid {
129

13-
10+
// Liquid class
11+
class Liquid {
12+
13+
1414
// Liquid is a rectangle
15-
float x,y,w,h;
15+
float x, y, w, h;
1616
// Coefficient of drag
1717
float c;
1818

@@ -23,18 +23,17 @@
2323
h = h_;
2424
c = c_;
2525
}
26-
26+
2727
// Is the Mover in the Liquid?
2828
boolean contains(Mover m) {
29-
PVector l = m.location;
29+
PVector l = m.position;
3030
if (l.x > x && l.x < x + w && l.y > y && l.y < y + h) {
3131
return true;
32-
}
33-
else {
32+
} else {
3433
return false;
3534
}
3635
}
37-
36+
3837
// Calculate drag force
3938
PVector drag(Mover m) {
4039
// Magnitude is coefficient * speed squared
@@ -44,16 +43,15 @@
4443
// Direction is inverse of velocity
4544
PVector drag = m.velocity.copy();
4645
drag.mult(-1);
47-
46+
4847
// Scale according to magnitude
4948
drag.setMag(dragMagnitude);
5049
return drag;
5150
}
52-
51+
5352
void display() {
5453
noStroke();
5554
fill(127);
56-
rect(x,y,w,h);
55+
rect(x, y, w, h);
5756
}
58-
59-
}
57+
}

content/examples/Topics/Simulate/ForcesWithVectors/Mover.pde

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010

1111
class Mover {
1212

13-
// location, velocity, and acceleration
14-
PVector location;
13+
// position, velocity, and acceleration
14+
PVector position;
1515
PVector velocity;
1616
PVector acceleration;
17-
17+
1818
// Mass is tied to size
1919
float mass;
2020

2121
Mover(float m, float x, float y) {
2222
mass = m;
23-
location = new PVector(x, y);
23+
position = new PVector(x, y);
2424
velocity = new PVector(0, 0);
2525
acceleration = new PVector(0, 0);
2626
}
@@ -35,30 +35,28 @@ class Mover {
3535
}
3636

3737
void update() {
38-
38+
3939
// Velocity changes according to acceleration
4040
velocity.add(acceleration);
41-
// Location changes by velocity
42-
location.add(velocity);
41+
// position changes by velocity
42+
position.add(velocity);
4343
// We must clear acceleration each frame
4444
acceleration.mult(0);
4545
}
46-
46+
4747
// Draw Mover
4848
void display() {
4949
stroke(255);
5050
strokeWeight(2);
5151
fill(255, 200);
52-
ellipse(location.x, location.y, mass*16, mass*16);
52+
ellipse(position.x, position.y, mass*16, mass*16);
5353
}
54-
54+
5555
// Bounce off bottom of window
5656
void checkEdges() {
57-
if (location.y > height) {
57+
if (position.y > height) {
5858
velocity.y *= -0.9; // A little dampening when hitting the bottom
59-
location.y = height;
59+
position.y = height;
6060
}
6161
}
62-
}
63-
64-
62+
}

content/examples/Topics/Simulate/GravitationalAttraction3D/Planet.pde

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,30 @@
44
// A class for an orbiting Planet
55

66
class Planet {
7-
8-
// Basic physics model (location, velocity, acceleration, mass)
9-
PVector location;
7+
8+
// Basic physics model (position, velocity, acceleration, mass)
9+
PVector position;
1010
PVector velocity;
1111
PVector acceleration;
1212
float mass;
1313

1414
Planet(float m, float x, float y, float z) {
1515
mass = m;
16-
location = new PVector(x,y,z);
17-
velocity = new PVector(1,0); // Arbitrary starting velocity
18-
acceleration = new PVector(0,0);
16+
position = new PVector(x, y, z);
17+
velocity = new PVector(1, 0); // Arbitrary starting velocity
18+
acceleration = new PVector(0, 0);
1919
}
20-
20+
2121
// Newton's 2nd Law (F = M*A) applied
2222
void applyForce(PVector force) {
23-
PVector f = PVector.div(force,mass);
23+
PVector f = PVector.div(force, mass);
2424
acceleration.add(f);
2525
}
2626

2727
// Our motion algorithm (aka Euler Integration)
2828
void update() {
2929
velocity.add(acceleration); // Velocity changes according to acceleration
30-
location.add(velocity); // Location changes according to velocity
30+
position.add(velocity); // position changes according to velocity
3131
acceleration.mult(0);
3232
}
3333

@@ -36,10 +36,8 @@ class Planet {
3636
noStroke();
3737
fill(255);
3838
pushMatrix();
39-
translate(location.x,location.y,location.z);
39+
translate(position.x, position.y, position.z);
4040
sphere(mass*8);
4141
popMatrix();
4242
}
43-
}
44-
45-
43+
}

content/examples/Topics/Simulate/GravitationalAttraction3D/Sun.pde

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55

66
class Sun {
77
float mass; // Mass, tied to size
8-
PVector location; // Location
8+
PVector position; // position
99
float G; // Universal gravitational constant (arbitrary value)
1010

1111
Sun() {
12-
location = new PVector(0,0);
12+
position = new PVector(0, 0);
1313
mass = 20;
1414
G = 0.4;
1515
}
1616

1717

1818
PVector attract(Planet m) {
19-
PVector force = PVector.sub(location,m.location); // Calculate direction of force
19+
PVector force = PVector.sub(position, m.position); // Calculate direction of force
2020
float d = force.mag(); // Distance between objects
21-
d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
21+
d = constrain(d, 5.0, 25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
2222
float strength = (G * mass * m.mass) / (d * d); // Calculate gravitional force magnitude
2323
force.setMag(strength); // Get force vector --> magnitude * direction
2424
return force;
@@ -29,10 +29,8 @@ class Sun {
2929
stroke(255);
3030
noFill();
3131
pushMatrix();
32-
translate(location.x,location.y,location.z);
32+
translate(position.x, position.y, position.z);
3333
sphere(mass*2);
3434
popMatrix();
3535
}
36-
}
37-
38-
36+
}

content/examples/Topics/Simulate/MultipleParticleSystems/CrazyParticle.pde

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ class CrazyParticle extends Particle {
3030
super.display();
3131
// Then add a rotating line
3232
pushMatrix();
33-
translate(location.x,location.y);
33+
translate(position.x, position.y);
3434
rotate(theta);
35-
stroke(255,lifespan);
36-
line(0,0,25,0);
35+
stroke(255, lifespan);
36+
line(0, 0, 25, 0);
3737
popMatrix();
3838
}
39-
40-
}
39+
}

0 commit comments

Comments
 (0)