Skip to main content
gamedev.net
🔒 Locked

Computing mouse pointer angles based on central object

Started by billmason 4 replies 900+ views
Original Post
billmason
billmason
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?
Zakwayda
Zakwayda
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);
billmason
billmason
How would I know if I have atan2 function and what is it?
Palidine
Palidine
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
SiCrane
SiCrane
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.
nplex
nplex
try
c++: include
c: include

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


...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.