Hi, i wonder how to check and know the diretions (left,right,up and down) for an object in motion.
Here is the exemple from Daniel Shiffman:
Vehicle v;
boolean debug = true;
float d = 25;
void setup() {
size(640, 360);
v = new Vehicle(width/2, height/2);
}
void draw() {
background(255);
if (debug) {
stroke(175);
noFill();
rectMode(CENTER);
rect(width/2, height/2, width-d*2, height-d*2);
}
v.boundaries();
v.run();
}
void mousePressed() {
debug = !debug;
}
class Vehicle {
PVector position;
PVector velocity;
PVector acceleration;
float r;
float maxspeed;
float maxforce;
Vehicle(float x, float y) {
acceleration = new PVector(0, 0);
velocity = new PVector(3, -2);
velocity.mult(5);
position = new PVector(x, y);
r = 6;
maxspeed = 3;
maxforce = 0.15;
}
void run() {
update();
display();
}
// Method to update position
void update() {
// Update velocity
velocity.add(acceleration);
// Limit speed
velocity.limit(maxspeed);
position.add(velocity);
// Reset accelertion to 0 each cycle
acceleration.mult(0);
}
void boundaries() {
PVector desired = null;
if (position.x < d) {
desired = new PVector(maxspeed, velocity.y);
}
else if (position.x > width -d) {
desired = new PVector(-maxspeed, velocity.y);
}
if (position.y < d) {
desired = new PVector(velocity.x, maxspeed);
}
else if (position.y > height-d) {
desired = new PVector(velocity.x, -maxspeed);
}
if (desired != null) {
desired.normalize();
desired.mult(maxspeed);
PVector steer = PVector.sub(desired, velocity);
steer.limit(maxforce);
applyForce(steer);
}
}
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(127);
stroke(0);
pushMatrix();
translate(position.x, position.y);
rotate(theta);
beginShape(TRIANGLES);
vertex(0, -r*2);
vertex(-r, r*2);
vertex(r, r*2);
endShape();
popMatrix();
}
}
so how do i check if the vehicle go to the left(or an other directions) and display it with a text ?


thanks a lot for yours times