Advertisement

Getting Angle with two points?

Started by January 24, 2001 02:44 PM
0 comments, last by Peddler 24 years ago
Hey, I am having a bit of trouble trying to remember how to get an angle in radians with two points. I have an object that I want to rotate to face whereever the mouse is currently at. So what I have to work with is the Mouse XY Postion and the Objects XY Position. I assume you just handle it like a right triangle? And get the angle. Here is something I tried using vectors, but I guess I am messing something up as it isn''t working right: (The +18''s are just to get to the center of the object)
  
//Vector U

Ux = mouse_PosX - (Object->GetPosX()+18);
Uy = mouse_PosY - (Object->GetPosY()+18);

//Vector V

Vx = mouse_PosX - (Object->GetPosX()+18);
Vy = (Object->GetPosY()+18) - (Object->GetPosY()+18);

Ulength = sqrt(pow(Ux,2) + pow(Uy,2));
Vlength = sqrt(pow(Vx,2) + pow(Vy,2));

Object->SetAngle( acos( ( (Ux*Vx)+(Uy*Vy) )/(Ulength*Vlength)  ) );
  
Any ideas what I am doing wrong or other suggestions how to do this. Thanks, -Peddler
This should work.
  uX = mouse_PosX - (Object -> GetXPos() + 18);uY = mouse_PosY - (Object -> GetYPos() + 18);angle = atan2(uY, uX);  

This topic is closed to new replies.

Advertisement