# Check direction for a object

**URL:** <https://discourse.processing.org/t/check-direction-for-a-object/9139>\
**Category:** Processing\
**Created:** [March 9, 2019, 10:08am UTC](https://discourse.processing.org/t/check-direction-for-a-object/9139 "2019-03-09T10:08:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [March 9, 2019, 10:08am UTC](https://discourse.processing.org/t/check-direction-for-a-object/9139/1 "2019-03-09T10:08:16Z")

</div>

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:

```auto

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;
}

```

```auto

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 ?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [March 9, 2019, 10:11am UTC](https://discourse.processing.org/t/check-direction-for-a-object/9139/2 "2019-03-09T10:11:34Z")

</div>

actually PVector makes it easy  
[https://processing.org/reference/PVector\_heading\_.html](https://processing.org/reference/PVector_heading_.html)

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [March 9, 2019, 10:12am UTC](https://discourse.processing.org/t/check-direction-for-a-object/9139/3 "2019-03-09T10:12:47Z")

</div>

you already have

```auto
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

```auto
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 🙂

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

---

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [March 9, 2019, 10:20am UTC](https://discourse.processing.org/t/check-direction-for-a-object/9139/4 "2019-03-09T10:20:36Z")

</div>

thanks guys i gonna check 🙂

---

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [March 9, 2019, 10:27am UTC](https://discourse.processing.org/t/check-direction-for-a-object/9139/5 "2019-03-09T10:27:19Z")

</div>

tanks a lot, here is the proper directions 🙂

```auto
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 ?

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [March 9, 2019, 10:31am UTC](https://discourse.processing.org/t/check-direction-for-a-object/9139/6 "2019-03-09T10:31:03Z")

</div>

probs just change the 4s to 8s 😄

---

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [March 9, 2019, 10:45am UTC](https://discourse.processing.org/t/check-direction-for-a-object/9139/7 "2019-03-09T10:45:19Z")

</div>

dha ! 😃 thanks a lot for yours times

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 9, 2019, 3:45pm UTC](https://discourse.processing.org/t/check-direction-for-a-object/9139/8 "2019-03-09T15:45:07Z")

</div>

> [@hotfooted](#):
>
> `int dir = round(4 * (velocity.heading2D() + radians(90)) / TWO_PI + 4) % 4;`

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

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

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

And of course, we can replace `8/TAU` w/ `4/PI`. 😉

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 12, 2019, 8:39pm UTC](https://discourse.processing.org/t/check-direction-for-a-object/9139/9 "2019-03-12T20:39:27Z")

</div>

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.

```auto
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.

```auto
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.

```auto
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;
}

```
