fix boid incorrect rotation

This commit is contained in:
JH159753 2022-09-26 18:10:59 -07:00
parent 6bf5b1c544
commit a066cdcf78
1 changed files with 13 additions and 6 deletions

View File

@ -49,31 +49,38 @@ class Boid
//println(atan2(direction.y, direction.x) + " " + normalize_angle_left_right(kinematic.getHeading())); //println(atan2(direction.y, direction.x) + " " + normalize_angle_left_right(kinematic.getHeading()));
float directionalThreshold = .1; float directionalThreshold = .1;
float angleToTarget = atan2(direction.y, direction.x) - normalize_angle_left_right(kinematic.getHeading()); //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 arrivalThreshold = 60.0; float arrivalThreshold = 60.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);
//prints the angle to the target //prints the angle to the target
//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) { if (angleToTarget > directionalThreshold && direction.mag() > 30) {
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) { } else if (angleToTarget < -directionalThreshold && direction.mag() > 30) {
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, -1); kinematic.increaseSpeed(0.0, -kinematic.getRotationalVelocity());
} }
else if (kinematic.getRotationalVelocity() < 0) { else if (kinematic.getRotationalVelocity() < 0) {
kinematic.increaseSpeed(0.0, 1); kinematic.increaseSpeed(0.0, kinematic.getRotationalVelocity());
} }
} }