add a max speed instead
Messing with the acceleration was not the way; we needed larger arrival threshold and a max speed
This commit is contained in:
parent
4f5e668b3a
commit
7851363a56
54
Boid.pde
54
Boid.pde
|
@ -54,7 +54,7 @@ class Boid
|
||||||
float directionalThreshold = .1;
|
float directionalThreshold = .1;
|
||||||
//You have to normalize this too or the boid goes the wrong way sometimes
|
//You have to normalize this too or the boid goes the wrong way sometimes
|
||||||
float angleToTarget = normalize_angle_left_right(atan2(direction.y, direction.x) - normalize_angle_left_right(kinematic.getHeading()));
|
float angleToTarget = normalize_angle_left_right(atan2(direction.y, direction.x) - normalize_angle_left_right(kinematic.getHeading()));
|
||||||
float arrivalThreshold = 60.0;
|
float arrivalThreshold = 150.0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -77,8 +77,7 @@ class Boid
|
||||||
|
|
||||||
if (kinematic.getRotationalVelocity() > 0) {
|
if (kinematic.getRotationalVelocity() > 0) {
|
||||||
kinematic.increaseSpeed(0.0, -kinematic.getRotationalVelocity());
|
kinematic.increaseSpeed(0.0, -kinematic.getRotationalVelocity());
|
||||||
}
|
} else if (kinematic.getRotationalVelocity() < 0) {
|
||||||
else if (kinematic.getRotationalVelocity() < 0) {
|
|
||||||
kinematic.increaseSpeed(0.0, kinematic.getRotationalVelocity());
|
kinematic.increaseSpeed(0.0, kinematic.getRotationalVelocity());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -88,7 +87,6 @@ class Boid
|
||||||
//if the target is inside its arrival threshold, accelerate backwards until the speed is 0.
|
//if the target is inside its arrival threshold, accelerate backwards until the speed is 0.
|
||||||
if (direction.mag() > arrivalThreshold) {
|
if (direction.mag() > arrivalThreshold) {
|
||||||
kinematic.increaseSpeed(1, 0);
|
kinematic.increaseSpeed(1, 0);
|
||||||
|
|
||||||
} else if (direction.mag() < arrivalThreshold) {
|
} else if (direction.mag() < arrivalThreshold) {
|
||||||
|
|
||||||
|
|
||||||
|
@ -129,23 +127,20 @@ class Boid
|
||||||
|
|
||||||
//use an ideal speed for our boid, to tell it to either speed up or slow down whether it's going faster than this or not
|
//use an ideal speed for our boid, to tell it to either speed up or slow down whether it's going faster than this or not
|
||||||
//Square the dot product that's been normalized; it'll change the curve so it slows down more
|
//Square the dot product that's been normalized; it'll change the curve so it slows down more
|
||||||
float idealSpeed = (dotProductOfTargets) * 80 + 10;
|
float idealSpeed = (dotProductOfTargets) * 80 + 15;
|
||||||
|
|
||||||
//Also use an acceleration factor, dependent on the direction our boid is facing vs the direction it needs to go
|
float maxSpeed = 100 * pow(((PI - abs(angleToTarget)) / PI), 10);
|
||||||
//if angleToTarget is high, low acceleration. If it's low, high acceleration
|
println(maxSpeed);
|
||||||
//angleToTarget can be from neg pi to pi, so use its absolute value
|
|
||||||
|
|
||||||
float acceleration = pow((PI - abs(angleToTarget)) / PI, 10);
|
if (idealSpeed > maxSpeed) {
|
||||||
|
idealSpeed = maxSpeed;
|
||||||
|
|
||||||
|
|
||||||
if (kinematic.getSpeed() < idealSpeed) {
|
|
||||||
kinematic.increaseSpeed(acceleration,0);
|
|
||||||
} else if (kinematic.getSpeed() > idealSpeed) {
|
|
||||||
kinematic.increaseSpeed(-acceleration,0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (kinematic.getSpeed() < idealSpeed) {
|
||||||
|
kinematic.increaseSpeed(1, 0);
|
||||||
|
} else if (kinematic.getSpeed() > idealSpeed) {
|
||||||
|
kinematic.increaseSpeed(-1, 0);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
//if no more targets to check, do the normal calculation
|
//if no more targets to check, do the normal calculation
|
||||||
|
@ -172,11 +167,7 @@ class Boid
|
||||||
} else if (idealSpeed > speedOffset) {
|
} else if (idealSpeed > speedOffset) {
|
||||||
kinematic.increaseSpeed(-1, 0);
|
kinematic.increaseSpeed(-1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
//if waypoints is null, do normal things
|
//if waypoints is null, do normal things
|
||||||
|
@ -208,9 +199,7 @@ class Boid
|
||||||
} else if (idealSpeed > speedOffset) {
|
} else if (idealSpeed > speedOffset) {
|
||||||
kinematic.increaseSpeed(-1, 0);
|
kinematic.increaseSpeed(-1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -241,18 +230,7 @@ class Boid
|
||||||
} else {
|
} else {
|
||||||
stillInRadius = false;
|
stillInRadius = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// place crumbs, do not change
|
// place crumbs, do not change
|
||||||
|
@ -300,7 +278,6 @@ class Boid
|
||||||
void seek(PVector target)
|
void seek(PVector target)
|
||||||
{
|
{
|
||||||
this.target = target;
|
this.target = target;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void follow(ArrayList<PVector> waypoints)
|
void follow(ArrayList<PVector> waypoints)
|
||||||
|
@ -309,12 +286,5 @@ class Boid
|
||||||
this.waypoints = waypoints;
|
this.waypoints = waypoints;
|
||||||
|
|
||||||
seek(waypoints.get(0));
|
seek(waypoints.get(0));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue