SFML community forums
Help => General => Topic started 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?
-
I think the return is in radian, try to multiplicate by 180 and divide by PI
-
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?
-
try atan2 ?
-
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!
-
I got it working. Thanks, guys!
-
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 :? )
-
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);
-
Many thanks.
-
Great code!