Merge branch 'main' of https://github.com/JH159753/GameAILab1
This commit is contained in:
commit
ce71745c1d
89
Boid.pde
89
Boid.pde
|
@ -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;
|
||||||
|
|
||||||
if (kinematic.getSpeed() < idealSpeed) {
|
float maxSpeed = 100 * pow(((PI - abs(angleToTarget)) / PI), 10);
|
||||||
kinematic.increaseSpeed(1,0);
|
//println(maxSpeed);
|
||||||
} else if (kinematic.getSpeed() > idealSpeed) {
|
|
||||||
kinematic.increaseSpeed(-1,0);
|
if (idealSpeed > maxSpeed) {
|
||||||
|
idealSpeed = maxSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
@ -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.
|
kinematic.increaseSpeed(1, 0);
|
||||||
float speedOffset = (idealSpeed - kinematic.getSpeed());
|
} else if (kinematic.getSpeed() > idealSpeed) {
|
||||||
|
kinematic.increaseSpeed(-1, 0);
|
||||||
if (abs(speedOffset) < 1) {
|
|
||||||
kinematic.increaseSpeed(speedOffset, 0);
|
|
||||||
} else if (idealSpeed < speedOffset) {
|
|
||||||
kinematic.increaseSpeed(1,0);
|
|
||||||
} else if (idealSpeed > speedOffset) {
|
|
||||||
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) {
|
||||||
|
@ -200,13 +188,11 @@ class Boid
|
||||||
if (abs(speedOffset) < 1) {
|
if (abs(speedOffset) < 1) {
|
||||||
kinematic.increaseSpeed(speedOffset, 0);
|
kinematic.increaseSpeed(speedOffset, 0);
|
||||||
} else if (idealSpeed < speedOffset) {
|
} else if (idealSpeed < speedOffset) {
|
||||||
kinematic.increaseSpeed(1,0);
|
kinematic.increaseSpeed(1, 0);
|
||||||
} 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));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
2
lab1.pde
2
lab1.pde
|
@ -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);
|
||||||
|
|
Loading…
Reference in New Issue