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() {  
 size(500,500);   
}  

void draw() {  
  background(255);  

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

  ellipse(pX,pY,20,20);  
  line(cX,cY,pX,pY);  
}

Answers

  • 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.