Some minor changes to the Flock example.

This commit is contained in:
Jonathan Puckey 2011-04-26 16:01:41 +02:00
parent b73afe3fc0
commit 2184fa144b

View file

@ -35,7 +35,7 @@
this.head = new PlacedSymbol(head); this.head = new PlacedSymbol(head);
this.path = new Path(); this.path = new Path();
this.shortPath = new Path(); this.shortPath = new Path();
this.shortPath.strokeWidth = 3; this.shortPath.strokeWidth = 4;
for(var i = 0, l = strength * 10 + 10; i < l; i++) { for(var i = 0, l = strength * 10 + 10; i < l; i++) {
this.path.add(this.loc); this.path.add(this.loc);
if (i < 3) if (i < 3)
@ -49,7 +49,7 @@
run: function(boids) { run: function(boids) {
this.lastLoc = this.loc.clone(); this.lastLoc = this.loc.clone();
if(!groupTogether) { if (!groupTogether) {
this.flock(boids); this.flock(boids);
} else { } else {
this.align(boids); this.align(boids);
@ -70,7 +70,6 @@
segment.point += rotated; segment.point += rotated;
lastVector = vector; lastVector = vector;
} }
this.path.smooth();
this.head.position = this.loc; this.head.position = this.loc;
var vector = this.loc - this.lastLoc; var vector = this.loc - this.lastLoc;
var rot = vector.angle; var rot = vector.angle;
@ -78,9 +77,8 @@
this.lastRot = rot; this.lastRot = rot;
var shortSegments = this.shortPath.segments; var shortSegments = this.shortPath.segments;
for (var i = 0; i < 3; i++) { for (var i = 0; i < 3; i++)
shortSegments[i] = segments[i].clone(); shortSegments[i] = segments[i].clone();
}
}, },
// We accumulate a new acceleration each time based on three rules // We accumulate a new acceleration each time based on three rules
@ -139,7 +137,7 @@
if (loc.x > width + r) loc.x = -r; if (loc.x > width + r) loc.x = -r;
if (loc.y > height + r) loc.y = -r; if (loc.y > height + r) loc.y = -r;
var vector = this.loc - oldLoc; var vector = this.loc - oldLoc;
if(!vector.isZero()) if (!vector.isZero())
this.path.position += vector; this.path.position += vector;
}, },
@ -161,7 +159,7 @@
// Average -- divide by how many // Average -- divide by how many
if (count > 0) if (count > 0)
steer /= count; steer /= count;
if(steer.length > 0) { if (steer.length > 0) {
// Implement Reynolds: Steering = Desired - Velocity // Implement Reynolds: Steering = Desired - Velocity
steer.length = this.maxSpeed; steer.length = this.maxSpeed;
steer -= this.vel; steer -= this.vel;
@ -181,11 +179,11 @@
for (var i = 0, l = boids.length; i < l; i++) { for (var i = 0, l = boids.length; i < l; i++) {
var other = boids[i]; var other = boids[i];
var d = this.loc.getDistance(other.loc); var d = this.loc.getDistance(other.loc);
if(d > 0 && d < nearest) { if (d > 0 && d < nearest) {
closestPoint = other.loc; closestPoint = other.loc;
nearest = d; nearest = d;
} }
if(d > 0 && d < neighborDist) { if (d > 0 && d < neighborDist) {
steer += other.vel; steer += other.vel;
count++; count++;
} }
@ -211,7 +209,7 @@
for (var i = 0, l = boids.length; i < l; i++) { for (var i = 0, l = boids.length; i < l; i++) {
var other = boids[i]; var other = boids[i];
var d = this.loc.getDistance(other.loc); var d = this.loc.getDistance(other.loc);
if(d > 0 && d < neighborDist) { if (d > 0 && d < neighborDist) {
sum += other.loc; // Add location sum += other.loc; // Add location
count++; count++;
} }