Sign in to follow this  
billmason

Computing mouse pointer angles based on central object

Recommended Posts

I have a character in my game who should be able to fire in any direction. I will use the mouse to point in the direction, but I could not figure out a way to use the character x and y and the mouse x and y to determine the angle that mouse is from the character. In school I learned about sin, cos, and tan, so I thought this may be the way to go, but I am unsure. (this is not homework.) character position = c mouse position = m
      m
     /
    /
   / Angle
  /
 /
c

I need to calculate the angle that goes from c to m. Any ideas?

Share this post


Link to post
Share on other sites
To calculate the angle:
angle = atan2(mouse.y - character.y, mouse.x - character.x);
This is assuming you have an 'atan2' function available.

You can also compute the direction vector (and thereby compute the character's orientation) directly:
vector2 direction = normalize(mouse.pos - character.pos);

Share this post


Link to post
Share on other sites
Well, a good starting point would looking in the standard library for your programming language. Most IDEs would let you type out the function, put your cursor on it and hit F1 and if it's in the standard library, it'll bring you to a help page for it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this