Replacing heading2D with heading for all Nature of Code examples. #1627

This commit is contained in:
Yong Bakos
2013-02-14 15:24:37 -07:00
parent 4ff94a3f16
commit 77c2b109d9
27 changed files with 119 additions and 119 deletions
@@ -4,10 +4,10 @@
// The "Vehicle" class
class Vehicle {
// Vehicle now has a brain!
Perceptron brain;
PVector location;
PVector velocity;
PVector acceleration;
@@ -34,7 +34,7 @@ class Vehicle {
location.add(velocity);
// Reset accelerationelertion to 0 each cycle
acceleration.mult(0);
location.x = constrain(location.x,0,width);
location.y = constrain(location.y,0,height);
}
@@ -43,48 +43,48 @@ class Vehicle {
// We could add mass here if we want A = F / M
acceleration.add(force);
}
// Here is where the brain processes everything
void steer(ArrayList<PVector> targets) {
// Make an array of forces
PVector[] forces = new PVector[targets.size()];
// Steer towards all targets
for (int i = 0; i < forces.length; i++) {
forces[i] = seek(targets.get(i));
}
// That array of forces is the input to the brain
PVector result = brain.feedforward(forces);
// Use the result to steer the vehicle
applyForce(result);
// Train the brain according to the error
PVector error = PVector.sub(desired, location);
brain.train(forces,error);
}
// A method that calculates a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
PVector seek(PVector target) {
PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target
// Normalize desired and scale to maximum speed
desired.normalize();
desired.mult(maxspeed);
// Steering = Desired minus velocity
PVector steer = PVector.sub(desired,velocity);
steer.limit(maxforce); // Limit to maximum steering force
return steer;
}
void display() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + PI/2;
float theta = velocity.heading() + PI/2;
fill(175);
stroke(0);
strokeWeight(1);
@@ -12,9 +12,9 @@ class Crawler {
PVector vel;
PVector acc;
float mass;
Oscillator osc;
Crawler() {
acc = new PVector();
vel = new PVector(random(-1,1),random(-1,1));
@@ -22,9 +22,9 @@ class Crawler {
mass = random(8,16);
osc = new Oscillator(mass*2);
}
void applyForce(PVector force) {
PVector f = force.get();
PVector f = force.get();
f.div(mass);
acc.add(f);
}
@@ -35,13 +35,13 @@ class Crawler {
loc.add(vel);
// Multiplying by 0 sets the all the components to 0
acc.mult(0);
osc.update(vel.mag()/10);
}
// Method to display
void display() {
float angle = vel.heading2D();
float angle = vel.heading();
pushMatrix();
translate(loc.x,loc.y);
rotate(angle);
@@ -49,10 +49,10 @@ class Crawler {
stroke(0);
fill(175,100);
ellipse(0,0,mass*2,mass*2);
osc.display(loc);
popMatrix();
}
}
@@ -35,7 +35,7 @@ class Mover {
}
void display() {
float theta = velocity.heading2D();
float theta = velocity.heading();
stroke(0);
strokeWeight(2);
@@ -52,14 +52,14 @@ class Mover {
if (location.x > width) {
location.x = 0;
}
}
else if (location.x < 0) {
location.x = width;
}
if (location.y > height) {
location.y = 0;
}
}
else if (location.y < 0) {
location.y = height;
}
@@ -22,7 +22,7 @@ void setup() {
void draw() {
background(0);
// Calculate a "wind" force based on mouse horizontal position
float dx = map(mouseX,0,width,-0.2,0.2);
PVector wind = new PVector(dx,0);
@@ -31,7 +31,7 @@ void draw() {
for (int i = 0; i < 2; i++) {
ps.addParticle();
}
// Draw an arrow representing the wind force
drawVector(wind, new PVector(width/2,50,0),500);
@@ -45,7 +45,7 @@ void drawVector(PVector v, PVector loc, float scayl) {
translate(loc.x,loc.y);
stroke(255);
// Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate
rotate(v.heading2D());
rotate(v.heading());
// Calculate length of vector & scale it to be bigger or smaller if necessary
float len = v.mag()*scayl;
// Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
@@ -22,7 +22,7 @@ void setup() {
void draw() {
background(0);
// Calculate a "wind" force based on mouse horizontal position
float dx = map(mouseX,0,width,-0.2,0.2);
PVector wind = new PVector(dx,0);
@@ -31,7 +31,7 @@ void draw() {
for (int i = 0; i < 2; i++) {
ps.addParticle();
}
// Draw an arrow representing the wind force
drawVector(wind, new PVector(width/2,50,0),500);
@@ -45,7 +45,7 @@ void drawVector(PVector v, PVector loc, float scayl) {
translate(loc.x,loc.y);
stroke(255);
// Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate
rotate(v.heading2D());
rotate(v.heading());
// Calculate length of vector & scale it to be bigger or smaller if necessary
float len = v.mag()*scayl;
// Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
@@ -9,7 +9,7 @@ void drawVector(PVector v, PVector loc, float scayl) {
translate(loc.x,loc.y);
stroke(0);
// Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate
rotate(v.heading2D());
rotate(v.heading());
// Calculate length of vector & scale it to be bigger or smaller if necessary
float len = v.mag()*scayl;
// Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
@@ -52,16 +52,16 @@ class Vehicle {
circleloc.normalize(); // Normalize to get heading
circleloc.mult(wanderD); // Multiply by distance
circleloc.add(location); // Make it relative to boid's location
float h = velocity.heading2D(); // We need to know the heading to offset wandertheta
float h = velocity.heading(); // We need to know the heading to offset wandertheta
PVector circleOffSet = new PVector(wanderR*cos(wandertheta+h),wanderR*sin(wandertheta+h));
PVector target = PVector.add(circleloc,circleOffSet);
seek(target);
// Render wandering circle, etc.
// Render wandering circle, etc.
if (debug) drawWanderStuff(location,circleloc,target,wanderR);
}
}
void applyForce(PVector force) {
// We could add mass here if we want A = F / M
@@ -86,7 +86,7 @@ class Vehicle {
void display() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + radians(90);
float theta = velocity.heading() + radians(90);
fill(127);
stroke(0);
pushMatrix();
@@ -112,7 +112,7 @@ class Vehicle {
// A method just to draw the circle associated with wandering
void drawWanderStuff(PVector location, PVector circle, PVector target, float rad) {
stroke(0);
stroke(0);
noFill();
ellipseMode(CENTER);
ellipse(circle.x,circle.y,rad*2,rad*2);
@@ -15,7 +15,7 @@ void draw() {
// A "vector" (really a point) to store the mouse location and screen center location
PVector mouseLoc = new PVector(mouseX, mouseY);
PVector centerLoc = new PVector(width/2, height/2);
PVector centerLoc = new PVector(width/2, height/2);
// Aha, a vector to store the displacement between the mouse and center
PVector v = PVector.sub(mouseLoc, centerLoc);
@@ -43,10 +43,10 @@ void drawVector(PVector v, PVector loc, float scayl) {
stroke(0);
strokeWeight(2);
// Call vector heading function to get direction (pointing up is a heading of 0)
rotate(v.heading2D());
rotate(v.heading());
// Calculate length of vector & scale it to be bigger or smaller if necessary
float len = v.mag()*scayl;
// Draw three lines to make an arrow
// Draw three lines to make an arrow
line(0, 0, len, 0);
line(len, 0, len-arrowsize, +arrowsize/2);
line(len, 0, len-arrowsize, -arrowsize/2);
@@ -7,7 +7,7 @@
// The "Vehicle" class
class Vehicle {
PVector location;
PVector velocity;
PVector acceleration;
@@ -44,20 +44,20 @@ class Vehicle {
// STEER = DESIRED MINUS VELOCITY
void seek(PVector target) {
PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target
// Normalize desired and scale to maximum speed
desired.normalize();
desired.mult(maxspeed);
// Steering = Desired minus velocity
PVector steer = PVector.sub(desired,velocity);
steer.limit(maxforce); // Limit to maximum steering force
applyForce(steer);
}
void display() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + PI/2;
float theta = velocity.heading() + PI/2;
fill(127);
stroke(0);
strokeWeight(1);
@@ -70,8 +70,8 @@ class Vehicle {
vertex(r, r*2);
endShape(CLOSE);
popMatrix();
}
}
@@ -32,7 +32,7 @@ class Vehicle {
location.add(velocity);
// Reset accelerationelertion to 0 each cycle
acceleration.mult(0);
history.add(location.get());
if (history.size() > 100) {
history.remove(0);
@@ -48,17 +48,17 @@ class Vehicle {
// STEER = DESIRED MINUS VELOCITY
void seek(PVector target) {
PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target
// Normalize desired and scale to maximum speed
desired.normalize();
desired.mult(maxspeed);
// Steering = Desired minus velocity
PVector steer = PVector.sub(desired,velocity);
steer.limit(maxforce); // Limit to maximum steering force
applyForce(steer);
}
void display() {
beginShape();
stroke(0);
@@ -68,10 +68,10 @@ class Vehicle {
vertex(v.x,v.y);
}
endShape();
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + PI/2;
float theta = velocity.heading() + PI/2;
fill(127);
stroke(0);
strokeWeight(1);
@@ -84,8 +84,8 @@ class Vehicle {
vertex(r, r*2);
endShape(CLOSE);
popMatrix();
}
}
@@ -5,7 +5,7 @@
// The "Vehicle" class
class Vehicle {
PVector location;
PVector velocity;
PVector acceleration;
@@ -57,11 +57,11 @@ class Vehicle {
steer.limit(maxforce); // Limit to maximum steering force
applyForce(steer);
}
void display() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + PI/2;
float theta = velocity.heading() + PI/2;
fill(127);
stroke(0);
strokeWeight(1);
@@ -74,8 +74,8 @@ class Vehicle {
vertex(r, r*2);
endShape(CLOSE);
popMatrix();
}
}
@@ -13,7 +13,7 @@ class Vehicle {
float maxspeed;
float maxforce;
Vehicle(float x, float y) {
acceleration = new PVector(0, 0);
velocity = new PVector(3, -2);
@@ -46,17 +46,17 @@ class Vehicle {
if (location.x < d) {
desired = new PVector(maxspeed, velocity.y);
}
}
else if (location.x > width -d) {
desired = new PVector(-maxspeed, velocity.y);
}
}
if (location.y < d) {
desired = new PVector(velocity.x, maxspeed);
}
}
else if (location.y > height-d) {
desired = new PVector(velocity.x, -maxspeed);
}
}
if (desired != null) {
desired.normalize();
@@ -65,7 +65,7 @@ class Vehicle {
steer.limit(maxforce);
applyForce(steer);
}
}
}
void applyForce(PVector force) {
// We could add mass here if we want A = F / M
@@ -75,7 +75,7 @@ class Vehicle {
void display() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + radians(90);
float theta = velocity.heading() + radians(90);
fill(127);
stroke(0);
pushMatrix();
@@ -14,7 +14,7 @@ class Vehicle {
float maxspeed;
float maxforce;
Vehicle(float x, float y) {
acceleration = new PVector(0, 0);
velocity = new PVector(3, -2);
@@ -39,7 +39,7 @@ class Vehicle {
location.add(velocity);
// Reset accelertion to 0 each cycle
acceleration.mult(0);
history.add(location.get());
if (history.size() > 500) {
history.remove(0);
@@ -52,17 +52,17 @@ class Vehicle {
if (location.x < d) {
desired = new PVector(maxspeed, velocity.y);
}
}
else if (location.x > width -d) {
desired = new PVector(-maxspeed, velocity.y);
}
}
if (location.y < d) {
desired = new PVector(velocity.x, maxspeed);
}
}
else if (location.y > height-d) {
desired = new PVector(velocity.x, -maxspeed);
}
}
if (desired != null) {
desired.normalize();
@@ -71,7 +71,7 @@ class Vehicle {
steer.limit(maxforce);
applyForce(steer);
}
}
}
void applyForce(PVector force) {
// We could add mass here if we want A = F / M
@@ -88,10 +88,10 @@ class Vehicle {
vertex(v.x,v.y);
}
endShape();
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + radians(90);
float theta = velocity.heading() + radians(90);
fill(127);
stroke(0);
pushMatrix();
@@ -49,7 +49,7 @@ class FlowField {
pushMatrix();
//translate(i*resolution+arrow.width/2,j*resolution+arrow.height/2);
translate(i*resolution,j*resolution);
rotate(field[i][j].heading2D());
rotate(field[i][j].heading());
imageMode(CENTER);
//scale(0.2);
image(a,0,0);
@@ -69,7 +69,7 @@ class FlowField {
translate(x,y);
stroke(0,100);
// Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate
rotate(v.heading2D());
rotate(v.heading());
// Calculate length of vector & scale it to be bigger or smaller if necessary
float len = v.mag()*scayl;
// Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
@@ -61,7 +61,7 @@ class Vehicle {
void display() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + radians(90);
float theta = velocity.heading() + radians(90);
fill(175);
stroke(0);
pushMatrix();
@@ -54,7 +54,7 @@ class FlowField {
translate(x,y);
stroke(0,100);
// Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate
rotate(v.heading2D());
rotate(v.heading());
// Calculate length of vector & scale it to be bigger or smaller if necessary
float len = v.mag()*scayl;
// Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
@@ -61,7 +61,7 @@ class Vehicle {
void display() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + radians(90);
float theta = velocity.heading() + radians(90);
fill(175);
stroke(0);
pushMatrix();
@@ -136,7 +136,7 @@ class Vehicle {
void render() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + radians(90);
float theta = velocity.heading() + radians(90);
fill(175);
stroke(0);
pushMatrix();
@@ -167,7 +167,7 @@ class Vehicle {
void render() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + radians(90);
float theta = velocity.heading() + radians(90);
fill(175);
stroke(0);
pushMatrix();
@@ -73,10 +73,10 @@ class Boid {
steer.limit(maxforce); // Limit to maximum steering force
return steer;
}
void render() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + radians(90);
float theta = velocity.heading() + radians(90);
fill(175);
stroke(0);
pushMatrix();
@@ -11,7 +11,7 @@ class Vehicle {
float maxspeed;
float maxforce;
Vehicle(float x, float y) {
acceleration = new PVector(0, 0);
velocity = new PVector(1,0);
@@ -41,13 +41,13 @@ class Vehicle {
void boundaries() {
PVector desired = null;
// Predict location 25 (arbitrary choice) frames ahead
PVector predict = velocity.get();
predict.mult(25);
PVector futureLocation = PVector.add(location, predict);
float distance = PVector.dist(futureLocation,circleLocation);
if (distance > circleRadius) {
PVector toCenter = PVector.sub(circleLocation,location);
toCenter.normalize();
@@ -62,11 +62,11 @@ class Vehicle {
steer.limit(maxforce);
applyForce(steer);
}
fill(255,0,0);
ellipse(futureLocation.x,futureLocation.y,4,4);
}
}
void applyForce(PVector force) {
// We could add mass here if we want A = F / M
@@ -76,7 +76,7 @@ class Vehicle {
void display() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + radians(90);
float theta = velocity.heading() + radians(90);
fill(175);
stroke(0);
pushMatrix();
@@ -82,10 +82,10 @@ class Boid {
return steer;
}
void render() {
// Draw a triangle rotated in the direction of velocity
float theta = vel.heading2D() + radians(90);
float theta = vel.heading() + radians(90);
fill(175);
stroke(0);
pushMatrix();
@@ -7,7 +7,7 @@
// A class for one branch in the system
class Branch {
// Each has a location, velocity, and timer
// Each has a location, velocity, and timer
// We could implement this same idea with different data
PVector loc;
PVector vel;
@@ -20,12 +20,12 @@ class Branch {
timerstart = n;
timer = timerstart;
}
// Move location
void update() {
loc.add(vel);
}
// Draw a dot at location
void render() {
fill(0);
@@ -33,7 +33,7 @@ class Branch {
ellipseMode(CENTER);
ellipse(loc.x,loc.y,2,2);
}
// Did the timer run out?
boolean timeToBranch() {
timer--;
@@ -47,7 +47,7 @@ class Branch {
// Create a new branch at the current location, but change direction by a given angle
Branch branch(float angle) {
// What is my current heading
float theta = vel.heading2D();
float theta = vel.heading();
// What is my current speed
float mag = vel.mag();
// Turn me
@@ -57,5 +57,5 @@ class Branch {
// Return a new Branch
return new Branch(loc,newvel,timerstart*0.66f);
}
}
@@ -7,7 +7,7 @@
// A class for one branch in the system
class Branch {
// Each has a location, velocity, and timer
// Each has a location, velocity, and timer
// We could implement this same idea with different data
PVector start;
PVector end;
@@ -44,7 +44,7 @@ class Branch {
if (timer < 0 && growing) {
growing = false;
return true;
}
}
else {
return false;
}
@@ -53,7 +53,7 @@ class Branch {
// Create a new branch at the current location, but change direction by a given angle
Branch branch(float angle) {
// What is my current heading
float theta = vel.heading2D();
float theta = vel.heading();
// What is my current speed
float mag = vel.mag();
// Turn me
@@ -34,7 +34,7 @@ class Rocket {
recordDist = width;
}
// FITNESS FUNCTION
// FITNESS FUNCTION
// distance = distance from target
// finish = what order did i finish (first, second, etc. . .)
// f(distance,finish) = (1.0f / finish^1.5) * (1.0f / distance^6);
@@ -121,7 +121,7 @@ class Rocket {
//fill(0,150);
//stroke(0);
//ellipse(location.x,location.y,r,r);
float theta = velocity.heading2D() + PI/2;
float theta = velocity.heading() + PI/2;
fill(200,100);
stroke(0);
pushMatrix();
@@ -133,8 +133,8 @@ class Rocket {
vertex(r, r*2);
endShape();
popMatrix();
}
float getFitness() {
@@ -24,7 +24,7 @@ class Rocket {
int geneCounter = 0;
boolean hitTarget = false; // Did I reach the target
//constructor
Rocket(PVector l, DNA dna_) {
acceleration = new PVector();
@@ -58,7 +58,7 @@ class Rocket {
float d = dist(location.x, location.y, target.x, target.y);
if (d < 12) {
hitTarget = true;
}
}
}
void applyForce(PVector f) {
@@ -72,7 +72,7 @@ class Rocket {
}
void display() {
float theta = velocity.heading2D() + PI/2;
float theta = velocity.heading() + PI/2;
fill(200, 100);
stroke(0);
pushMatrix();
@@ -39,7 +39,7 @@ class Rocket {
recordDist = 10000; // Some high number that will be beat instantly
}
// FITNESS FUNCTION
// FITNESS FUNCTION
// distance = distance from target
// finish = what order did i finish (first, second, etc. . .)
// f(distance,finish) = (1.0f / finish^1.5) * (1.0f / distance^6);
@@ -80,7 +80,7 @@ class Rocket {
if (target.contains(location) && !hitTarget) {
hitTarget = true;
}
}
else if (!hitTarget) {
finishTime++;
}
@@ -108,7 +108,7 @@ class Rocket {
void display() {
//background(255,0,0);
float theta = velocity.heading2D() + PI/2;
float theta = velocity.heading() + PI/2;
fill(200, 100);
stroke(0);
strokeWeight(1);