misc PVector and example cleanup

This commit is contained in:
Daniel Shiffman
2013-03-18 22:12:27 -04:00
parent 0c35ad10a5
commit 9a466e1866
6 changed files with 10 additions and 21 deletions
@@ -11,7 +11,7 @@ class Boid {
Boid(float x, float y) {
acceleration = new PVector(0,0);
velocity = new PVector(random(-1,1),random(-1,1));
velocity = PVector.random2D();
location = new PVector(x,y);
r = 2.0;
maxspeed = 2;
@@ -60,9 +60,8 @@ class Boid {
// STEER = DESIRED MINUS VELOCITY
PVector seek(PVector target) {
PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target
// Normalize desired and scale to maximum speed
desired.normalize();
desired.mult(maxspeed);
// Scale to maximum speed
desired.setMag(maxspeed);
// Steering = Desired minus Velocity
PVector steer = PVector.sub(desired,velocity);
steer.limit(maxforce); // Limit to maximum steering force
@@ -120,8 +119,7 @@ class Boid {
// As long as the vector is greater than 0
if (steer.mag() > 0) {
// Implement Reynolds: Steering = Desired - Velocity
steer.normalize();
steer.mult(maxspeed);
steer.setMag(maxspeed);
steer.sub(velocity);
steer.limit(maxforce);
}
@@ -143,8 +141,7 @@ class Boid {
}
if (count > 0) {
sum.div((float)count);
sum.normalize();
sum.mult(maxspeed);
sum.setMag(maxspeed);
PVector steer = PVector.sub(sum,velocity);
steer.limit(maxforce);
return steer;
@@ -18,10 +18,9 @@ class Sun {
PVector attract(Planet m) {
PVector force = PVector.sub(location,m.location); // Calculate direction of force
float d = force.mag(); // Distance between objects
d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction)
d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
float strength = (G * mass * m.mass) / (d * d); // Calculate gravitional force magnitude
force.mult(strength); // Get force vector --> magnitude * direction
force.setMag(strength); // Get force vector --> magnitude * direction
return force;
}