mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
removing old nature of code examples before adding new ones
This commit is contained in:
@@ -1,43 +0,0 @@
|
||||
// Stay Within Walls
|
||||
// Daniel Shiffman <http://www.shiffman.net>
|
||||
// The Nature of Code
|
||||
|
||||
// "Made-up" Steering behavior to stay within walls
|
||||
|
||||
|
||||
Vehicle v;
|
||||
boolean debug = true;
|
||||
|
||||
|
||||
PVector circleLocation;
|
||||
float circleRadius;
|
||||
|
||||
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
v = new Vehicle(width/2, height/4);
|
||||
|
||||
circleLocation = new PVector(width/2,height/2);
|
||||
circleRadius = height/2-25;
|
||||
|
||||
smooth();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(255);
|
||||
|
||||
if (debug) {
|
||||
stroke(175);
|
||||
noFill();
|
||||
ellipse(circleLocation.x,circleLocation.y, circleRadius*2,circleRadius*2);
|
||||
}
|
||||
|
||||
v.boundaries();
|
||||
v.run();
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
debug = !debug;
|
||||
}
|
||||
|
||||
@@ -1,95 +0,0 @@
|
||||
// Wander
|
||||
// Daniel Shiffman <http://www.shiffman.net>
|
||||
// The Nature of Code
|
||||
|
||||
// The "Vehicle" class (for wandering)
|
||||
|
||||
class Vehicle {
|
||||
|
||||
PVector location;
|
||||
PVector velocity;
|
||||
PVector acceleration;
|
||||
float r;
|
||||
|
||||
float maxspeed;
|
||||
float maxforce;
|
||||
|
||||
Vehicle(float x, float y) {
|
||||
acceleration = new PVector(0, 0);
|
||||
velocity = new PVector(1,0);
|
||||
velocity.mult(5);
|
||||
location = new PVector(x, y);
|
||||
r = 3;
|
||||
maxspeed = 3;
|
||||
maxforce = 0.15;
|
||||
}
|
||||
|
||||
void run() {
|
||||
update();
|
||||
display();
|
||||
}
|
||||
|
||||
// Method to update location
|
||||
void update() {
|
||||
// Update velocity
|
||||
velocity.add(acceleration);
|
||||
// Limit speed
|
||||
velocity.limit(maxspeed);
|
||||
location.add(velocity);
|
||||
// Reset accelertion to 0 each cycle
|
||||
acceleration.mult(0);
|
||||
}
|
||||
|
||||
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();
|
||||
toCenter.mult(velocity.mag());
|
||||
desired = PVector.add(velocity,toCenter);
|
||||
desired.normalize();
|
||||
desired.mult(maxspeed);
|
||||
}
|
||||
|
||||
if (desired != null) {
|
||||
PVector steer = PVector.sub(desired, velocity);
|
||||
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
|
||||
acceleration.add(force);
|
||||
}
|
||||
|
||||
|
||||
void display() {
|
||||
// Draw a triangle rotated in the direction of velocity
|
||||
float theta = velocity.heading2D() + radians(90);
|
||||
fill(175);
|
||||
stroke(0);
|
||||
pushMatrix();
|
||||
translate(location.x, location.y);
|
||||
rotate(theta);
|
||||
beginShape(TRIANGLES);
|
||||
vertex(0, -r*2);
|
||||
vertex(-r, r*2);
|
||||
vertex(r, r*2);
|
||||
endShape();
|
||||
popMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user