SFML community forums

Help => General => Topic started by: Makuto on March 22, 2011, 05:26:02 pm

Title: Player pointing at mouse?
Post by: Makuto on March 22, 2011, 05:26:02 pm
How do I make my player sprite point at the mouse?  Originally I used the equation atan((mousey-playery)/(mousex-playerx)).  I looked on the internet, and this equation is supposed to return the angle of the player, in degrees.  I was then planning on putting the result in for the player.SetRotation.  However, this doesn't return the angle, just a weird decimal negative number.  What should I do?
Title: Player pointing at mouse?
Post by: Lo-X on March 22, 2011, 08:16:55 pm
I think the return is in radian, try to multiplicate by 180 and divide by PI
Title: Player pointing at mouse?
Post by: Makuto on March 23, 2011, 03:56:59 am
Because the Y-axis goes upwards while the object goes downwards in relation to the screen, and the X-axis is at the top, the window is basically in the fourth quadrant, which has a negative Y and a positive X.  Do I need to convert the Y-coordinates to negative numbers to get the correct angle?
Title: Player pointing at mouse?
Post by: kalgon on March 23, 2011, 02:48:23 pm
try atan2 ?
Title: Player pointing at mouse?
Post by: Walker on March 23, 2011, 04:23:06 pm
cmath functions use radians, you will need to invert/negate/thingy (y = 0 - y) the y coordinate and atan2 is more suited for this. Everyone is right! Gold stars!
Title: Player pointing at mouse?
Post by: Makuto on March 23, 2011, 04:32:47 pm
I got it working.  Thanks, guys!
Title: Player pointing at mouse?
Post by: Jove on March 24, 2011, 07:21:58 am
Hey Makuto.

Would you mind sharing the solution to this? It might be useful for something I would like to do.

(I'm lousy at maths  :? )
Title: Player pointing at mouse?
Post by: mimipim on March 24, 2011, 08:56:21 am
To get the angle you must know your player position and mouse position:

float angle = atan2(mouseY-plrY,mouseX-plrX)*(180/3.1415f);
spr_myplr.SetRotation(angle);
Title: Player pointing at mouse?
Post by: Jove on March 24, 2011, 09:25:01 am
Many thanks.
Title: Player pointing at mouse?
Post by: Makuto on March 24, 2011, 01:29:07 pm
Great code!