Jump to content
• Advertisement

# 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

##### 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

##### Share on other sites
How would I know if I have atan2 function and what is it?

#### Share this post

##### Share on other sites
It's a standard trigonometry function:

http://en.wikipedia.org/wiki/Atan2

you have it. it's part of just about every programming language's standard function set (like sin, cos, tan)

-me

#### Share this 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

##### 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

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5
• Advertisement

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633736
• Total Posts
3013600
×

## 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!