add waypoints code
This commit is contained in:
parent
6bf5b1c544
commit
fff97d36b5
70
Boid.pde
70
Boid.pde
|
@ -49,7 +49,7 @@ 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());
|
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
|
||||||
|
@ -59,11 +59,12 @@ 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) {
|
if (angleToTarget >= .1) {
|
||||||
kinematic.increaseSpeed(0.0, +1);
|
println("positive angle");
|
||||||
|
kinematic.increaseSpeed(0.0, 2);
|
||||||
|
|
||||||
//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 < -.1) {
|
||||||
kinematic.increaseSpeed(0.0, -1);
|
kinematic.increaseSpeed(0.0, -1);
|
||||||
|
|
||||||
//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
|
||||||
|
@ -71,8 +72,7 @@ class Boid
|
||||||
|
|
||||||
if (kinematic.getRotationalVelocity() > 0) {
|
if (kinematic.getRotationalVelocity() > 0) {
|
||||||
kinematic.increaseSpeed(0.0, -1);
|
kinematic.increaseSpeed(0.0, -1);
|
||||||
}
|
} else if (kinematic.getRotationalVelocity() < 0) {
|
||||||
else if (kinematic.getRotationalVelocity() < 0) {
|
|
||||||
kinematic.increaseSpeed(0.0, 1);
|
kinematic.increaseSpeed(0.0, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -84,39 +84,35 @@ class Boid
|
||||||
//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);
|
//println("main if");
|
||||||
|
kinematic.increaseSpeed(.5, 0);
|
||||||
} else if (direction.mag() < arrivalThreshold) {
|
} else if (direction.mag() < arrivalThreshold) {
|
||||||
//Need more specific code here to handle arrivals correctly
|
//Need more specific code here to handle arrivals correctly
|
||||||
|
|
||||||
if (kinematic.getSpeed() < 40 && direction.mag() > 30) {
|
if (kinematic.getSpeed() < 40 && direction.mag() > 30) {
|
||||||
kinematic.increaseSpeed(1,0);
|
//println("if 1");
|
||||||
|
kinematic.increaseSpeed(1, 0);
|
||||||
} else if (kinematic.getSpeed() < 20 && direction.mag() > 15) {
|
} else if (kinematic.getSpeed() < 20 && direction.mag() > 15) {
|
||||||
kinematic.increaseSpeed(.75,0);
|
//println("if .75");
|
||||||
|
kinematic.increaseSpeed(.75, 0);
|
||||||
} else if (kinematic.getSpeed() < 10 && direction.mag() > 5) {
|
} else if (kinematic.getSpeed() < 10 && direction.mag() > 5) {
|
||||||
kinematic.increaseSpeed(.5,0);
|
//println("if .5");
|
||||||
|
kinematic.increaseSpeed(.5, 0);
|
||||||
} else if (kinematic.getSpeed() < 5 && direction.mag() < 5) {
|
} else if (kinematic.getSpeed() < 5 && direction.mag() < 5) {
|
||||||
|
//println("if -kin");
|
||||||
//This should ensure that the boid's speed can be dropped to exactly 0 so we don't have stuttering
|
//This should ensure that the boid's speed can be dropped to exactly 0 so we don't have stuttering
|
||||||
kinematic.increaseSpeed(-kinematic.getSpeed(),0);
|
|
||||||
|
kinematic.increaseSpeed(-kinematic.getSpeed(), 0);
|
||||||
} else {
|
} else {
|
||||||
kinematic.increaseSpeed(-1,0);
|
println("else");
|
||||||
|
kinematic.increaseSpeed(-1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//drawing a line for testing purposes
|
//drawing a line for testing purposes
|
||||||
//line(kinematic.position.x, kinematic.position.y, kinematic.position.x + direction.x, kinematic.position.y + direction.y);
|
//line(kinematic.position.x, kinematic.position.y, kinematic.position.x + direction.x, kinematic.position.y + direction.y);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// place crumbs, do not change
|
// place crumbs, do not change
|
||||||
|
@ -164,13 +160,37 @@ class Boid
|
||||||
void seek(PVector target)
|
void seek(PVector target)
|
||||||
{
|
{
|
||||||
this.target = target;
|
this.target = target;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
//void follow(ArrayList<PVector> waypoints)
|
||||||
|
//{
|
||||||
|
|
||||||
|
// //println("func count " + count);
|
||||||
|
// if(count > waypoints.size() - 1){
|
||||||
|
// this.target = waypoints.get(0);
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// else {
|
||||||
|
// // TODO: change to follow *all* waypoints
|
||||||
|
// println("count " + count);
|
||||||
|
// this.target = waypoints.get(count);
|
||||||
|
// PVector temp = waypoints.remove(count);
|
||||||
|
// count++;
|
||||||
|
// //count--;
|
||||||
|
|
||||||
|
// follow(waypoints);
|
||||||
|
// }
|
||||||
|
|
||||||
|
//}
|
||||||
void follow(ArrayList<PVector> waypoints)
|
void follow(ArrayList<PVector> waypoints)
|
||||||
{
|
{
|
||||||
// TODO: change to follow *all* waypoints
|
|
||||||
this.target = waypoints.get(0);
|
this.target = waypoints.get(0);
|
||||||
|
|
||||||
|
for (int i = 1; i < waypoints.size(); i++){
|
||||||
|
if(PVector.sub(this.target,this.kinematic.position).mag() = 0)
|
||||||
|
this.target = waypoints.get(i);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
17
Flocking.pde
17
Flocking.pde
|
@ -1,7 +1,24 @@
|
||||||
|
|
||||||
/// called when "f" is pressed; should instantiate additional boids and start flocking
|
/// called when "f" is pressed; should instantiate additional boids and start flocking
|
||||||
|
Boid[] billies = new Boid[8];
|
||||||
void flock()
|
void flock()
|
||||||
{
|
{
|
||||||
|
int lasttr = 0;
|
||||||
|
println("flock called");
|
||||||
|
float dt = (millis() - lasttr)/1000.0;
|
||||||
|
lasttr = millis();
|
||||||
|
PVector target = new PVector(mouseX, mouseY);
|
||||||
|
|
||||||
|
for(int i = 0; i < 7; i++)
|
||||||
|
{
|
||||||
|
billies[i] = new Boid(new PVector(100 + i*100, 500), BILLY_START_HEADING, BILLY_MAX_SPEED, BILLY_MAX_ROTATIONAL_SPEED, BILLY_MAX_ACCELERATION, BILLY_MAX_ROTATIONAL_ACCELERATION);
|
||||||
|
println("billy " + billies[i].toString());
|
||||||
|
|
||||||
|
billies[i].update(dt);
|
||||||
|
billies[i].seek(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// called when "f" is pressed again; should remove the flock
|
/// called when "f" is pressed again; should remove the flock
|
||||||
|
|
Loading…
Reference in New Issue