Help with Trig and circular movement

Hey everyone im just starting out on processing, and ive been trying to get circular movement to work but i cant quite get it. I'm trying to get the ball to move in line with the mouse but at a fixed distance. i think i might be calculating the angle wrong. any help would be greatly appreciated, cheers

float mX, mY;     
float cX, cY;  
float pX, pY;  
float angle;  
float distance = 100;  
float speed = 0.05;  

void setup() {  

void draw() {  

  mX = mouseX;  
  mY = mouseY;  

  cX = width/2;  
  cY = height/2;  

  angle = sin((mY-cY)/sqrt(sq(mX-cX)+sq(mY-cY)));  

  //angle += speed;  

  println("mX: " + mX + " mY: " + mY + " angle: " + angle);  

  pX = cX + distance * cos(angle);  
  pY = cY + distance * sin(angle);  



  • Answer ✓

    I'm not entirely sure... but perhaps you are looking for atan2(). With this solution, you would replace line 21 with:

    angle = atan2(mY - cY, mX - cX);

    Note: atan2() takes (y, x), which may cause some confusion...

  • thanks alot, works perfectly

Sign In or Register to comment.