This commit is contained in:
Priyatham Sai Chand 2022-11-02 14:57:06 -07:00
commit ce71745c1d
2 changed files with 257 additions and 290 deletions

View File

@ -54,7 +54,9 @@ 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;
//This just draws a circle for visual debugging purposes //This just draws a circle for visual debugging purposes
circle(target.x, target.y, 3); circle(target.x, target.y, 3);
@ -63,38 +65,28 @@ class Boid
//println(angleToTarget); //println(angleToTarget);
//if the angle is larger than the threshold in the positive direction, rotate counterclockwise //if the angle is larger than the threshold in the positive direction, rotate counterclockwise
if (angleToTarget > directionalThreshold && direction.mag() > 30) { if (angleToTarget > directionalThreshold) {
kinematic.increaseSpeed(0.0, +1); kinematic.increaseSpeed(0.0, +1);
} else if (angleToTarget > directionalThreshold && direction.mag() > 15) {
kinematic.increaseSpeed(0.0, +.5);
//if the angle is smaller than the threshold in the negative direction, rotate clockwise //if the angle is smaller than the threshold in the negative direction, rotate clockwise
} else if (angleToTarget < -directionalThreshold && direction.mag() > 30) { } else if (angleToTarget < -directionalThreshold) {
kinematic.increaseSpeed(0.0, -1); kinematic.increaseSpeed(0.0, -1);
} else if (angleToTarget < -directionalThreshold && direction.mag() > 15) {
kinematic.increaseSpeed(0.0, -.5);
//if the angle is within our threshold, stop our rotational velocity by rotating opposite //if the angle is within our threshold, stop our rotational velocity by rotating opposite
} else if (directionalThreshold > angleToTarget) { } else if (directionalThreshold > angleToTarget) {
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());
} }
} }
//if the target is outside its arrival threshold, accelerate. //if the target is outside its arrival threshold, accelerate.
//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) {
@ -109,8 +101,9 @@ class Boid
//this checks if there's another target to go to //this checks if there's another target to go to
if (currentTarget + 1 < waypoints.size()) { if (currentTarget + 1 < waypoints.size()) {
//if so, change the speed depending on the angle to the next target //if so, change the speed depending on the angle to the next target
//This part isn't really implemented at all, and I need sleep
//We can calculate the angle to the next target with the use of two vectors: one from our location to current target, one from current target to next target //We can calculate the angle to the next target with the use of two vectors: one from our location to current target, one from current target to next target
//use the dot product of those two vectors; this gives us the angle between them, and we can use that to calculate how much we should slow down //use the dot product of those two vectors; this gives us the angle between them, and we can use that to calculate how much we should slow down
@ -133,15 +126,20 @@ class Boid
dotProductOfTargets = (dotProductOfTargets + 1) / 2; dotProductOfTargets = (dotProductOfTargets + 1) / 2;
//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
float idealSpeed = dotProductOfTargets * 80 + 10; float idealSpeed = (dotProductOfTargets) * 80 + 15;
float maxSpeed = 100 * pow(((PI - abs(angleToTarget)) / PI), 10);
//println(maxSpeed);
if (idealSpeed > maxSpeed) {
idealSpeed = maxSpeed;
}
if (kinematic.getSpeed() < idealSpeed) { if (kinematic.getSpeed() < idealSpeed) {
kinematic.increaseSpeed(1, 0); kinematic.increaseSpeed(1, 0);
} else if (kinematic.getSpeed() > idealSpeed) { } else if (kinematic.getSpeed() > idealSpeed) {
kinematic.increaseSpeed(-1, 0); 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
@ -150,29 +148,19 @@ class Boid
//Ideal speed here should be 80 at dist 85, and reduce linearly from there, hitting 0 at 5 units? //Ideal speed here should be 80 at dist 85, and reduce linearly from there, hitting 0 at 5 units?
//This can be changed later if it isn't good //This can be changed later if it isn't good
float idealSpeed = (1 * direction.mag() - 5); float idealSpeed = (1 * direction.mag() - 5);
//if idealSpeed is "negative" we should just set it to 0
if (idealSpeed < 0) { if (idealSpeed < 0) {
idealSpeed = 0; idealSpeed = 0;
} }
//use this to know how off the target speed we are, and slow down accordingly if (kinematic.getSpeed() < idealSpeed) {
//This will be positive if the ideal speed is higher than current speed, negative if ideal speed is lower.
float speedOffset = (idealSpeed - kinematic.getSpeed());
if (abs(speedOffset) < 1) {
kinematic.increaseSpeed(speedOffset, 0);
} else if (idealSpeed < speedOffset) {
kinematic.increaseSpeed(1, 0); kinematic.increaseSpeed(1, 0);
} else if (idealSpeed > speedOffset) { } else if (kinematic.getSpeed() > idealSpeed) {
kinematic.increaseSpeed(-1, 0); kinematic.increaseSpeed(-1, 0);
} }
} }
} else { } else {
//if waypoints is null, do normal things //if waypoints is null, do normal things
@ -184,7 +172,7 @@ class Boid
//Ideal speed here should be 80 at dist 85, and reduce linearly from there, hitting 0 at 5 units? //Ideal speed here should be 80 at dist 85, and reduce linearly from there, hitting 0 at 5 units?
//This can be changed later if it isn't good //This can be changed later if it isn't good
float idealSpeed = 1 * direction.mag() - 5; float idealSpeed = 1 * direction.mag() + 10;
//if idealSpeed is "negative" we should just set it to 0 //if idealSpeed is "negative" we should just set it to 0
if (idealSpeed < 0) { if (idealSpeed < 0) {
@ -204,9 +192,7 @@ class Boid
} else if (idealSpeed > speedOffset) { } else if (idealSpeed > speedOffset) {
kinematic.increaseSpeed(-1, 0); kinematic.increaseSpeed(-1, 0);
} }
} }
} }
@ -237,18 +223,7 @@ class Boid
} else { } else {
stillInRadius = false; stillInRadius = false;
} }
} }
} }
// place crumbs, do not change // place crumbs, do not change
@ -296,7 +271,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)
@ -305,12 +279,5 @@ class Boid
this.waypoints = waypoints; this.waypoints = waypoints;
seek(waypoints.get(0)); seek(waypoints.get(0));
} }
} }

View File

@ -22,7 +22,7 @@ boolean show_help = false;
boolean flocking_enabled = false; boolean flocking_enabled = false;
void setup() { void setup() {
size(800, 600); size(1000, 800);
billy = new Boid(BILLY_START, BILLY_START_HEADING, BILLY_MAX_SPEED, BILLY_MAX_ROTATIONAL_SPEED, BILLY_MAX_ACCELERATION, BILLY_MAX_ROTATIONAL_ACCELERATION); billy = new Boid(BILLY_START, BILLY_START_HEADING, BILLY_MAX_SPEED, BILLY_MAX_ROTATIONAL_SPEED, BILLY_MAX_ACCELERATION, BILLY_MAX_ROTATIONAL_ACCELERATION);
randomSeed(0); randomSeed(0);