Check direction for a object

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 ?

1 Like

actually PVector makes it easy
https://processing.org/reference/PVector_heading_.html

1 Like

you already have

float theta = velocity.heading2D() + radians(90);

which you can use and if you just want to know if it’s travelling NESW you can do

int dir = round(4 * (velocity.heading2D() + radians(90)) /  TWO_PI + 4) % 4;
switch(dir) {
{	
	case 0:
		//east
		break;
	case 1:
		//south
		break;
	case 2:
		//west
		break;
	case 3:
		//north
		break;
}

or something like that :slight_smile:

edit: i didn’t test this so you might have to fiddle with the degree/radians to sort it out.

3 Likes

thanks guys i gonna check :slight_smile:

tanks a lot, here is the proper directions :slight_smile:

int dir = round(4 * (velocity.heading2D() + radians(90)) /  TWO_PI + 4) % 4;
    switch(dir) {
 
  case 0:
    text("north",10,10);
    break;
  case 1:
    text("east",10,10);
    break;
  case 2:
    text("south",10,10);
    break;
  case 3:
    text("west",10,10);
    break;
}

and now if i want the 8 directions ? like north/east ect ?

1 Like

probs just change the 4s to 8s :smile:

2 Likes

dha ! :smiley: thanks a lot for yours times

A slightly performance boost version: :racing_car:
final int dir = round(8/TAU * (velocity.heading2D() + HALF_PI) + 8) % 8;

Also a slightly modified version w/ the + 8 outside the round() function: :man_scientist:
final int dir = (round(8/TAU * (velocity.heading2D() + HALF_PI)) + 8) % 8;

Plus a Python Mode adapted version too. 'Cuz why not?! :snake:
dir = (this.round(8/TAU * (velocity.heading2D() + HALF_PI)) + 8) % 8

And of course, we can replace 8/TAU w/ 4/PI. :wink:

2 Likes

If you want an arbitrary heading map – four directions, eight directions… six directions (hex map), three directions, etc. etc.

…then you can divide TWO_PI by the number of directions you want, then find the case.

int dirs = 4;
int dir = round(dirs/TAU * (velocity.heading2D() + HALF_PI) + dirs) % dirs;
switch(dir) {

Depending on space geometry and interface, if you wanted arcs of different lengths, it is easier to just make a degree map. Then to find your heading index, loop through the map until the entry is greater than your current heading angle.

int heading(float angle, float[] directions){
  for (int i=0; i<directions.length; i++) {
    if (directions[i] > angle) {
      return i;
    }
  }
  return -1;
}

Custom maps could be based on degrees or radians – the logic is the same, each arc (beginning from 0) corresponds to an index.

float[] directions = new float[]{45, 135, 180, 225, 315, 360};
float[] directions2 = new float[]{QUARTER_PI, HALF_PI, PI, TWO_PI};

void setup() { 
  frameRate(1);
}
void draw() {
  int angle = (int)random(360);
  println(angle, heading(angle%360, directions));

  float angle2 = random(TWO_PI);
  println(angle2, heading(angle2%TWO_PI, directions2));
  
  println();
}

int heading(float angle, float[] directions){
  for (int i=0; i<directions.length; i++) {
    if (directions[i] > angle) {
      return i;
    }
  }
  return -1;
}
2 Likes