Jump to content
  • Advertisement
Sign in to follow this  
billmason

Computing mouse pointer angles based on central object

This topic is 3973 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
try
c++: include <cmath>
c: include <math.c>

uhm, java;
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html


...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!