Finding joint angle with atan2()

August 8th, 2013

Here is a short Processing example how to use atan2(), it’s pretty simple, but I still find some people not using it correctly.


PVector v1 = new PVector(0,0);

void setup() {
  size(600,600);
}

void draw() {
  background(0);
  stroke(255);
  
  pushMatrix();
  translate(width/2, height/2);
  
  PVector mouseVector = new PVector(mouseX - width/2, mouseY - height/2);
  PVector nMouseVector = mouseVector.get();
  
  nMouseVector.normalize();
  
  float angle =degrees( atan2(nMouseVector.y, nMouseVector.x));
  
  if(angle < 0) {
    angle += 360; 
  }

  line(0,0,mouseVector.x, mouseVector.y);
  line(0,0,100,0);
 
  text(angle, mouseVector.x, mouseVector.y);
  popMatrix();

  
}

Leave a Reply

Your email address will not be published. Required fields are marked *