# Set sprite to face direction of mouse

I'm building a html5 canvas game and I need a way to get my sprite to face the mouse cursor. I have the X and Y co-ordinates for the sprite, and also the X and Y co-ordinates of the pointer. All I'm stuck with is the math involved to make the sprite face the pointer. It's all in a 2D world, so I can't imagine it'll be too complicated, but I have very little experience with this stuff.

To add, i should note that I need an angle in degrees that will point me towards the cursor using its X and Y position.

How can i know the co-ordinates of the cursor and where do i put all of this stuff ?

angle = math.atan2(y2 - y1, x2 - x1) * 180 / math.pi;

The * 180 / math.pi; converts it from radians to degrees.
• Note that the angle produced by atan2 assumes that the coordinate system is +x right and +y up. If yours goes down, then you will need to negate the angle. It also assumes that the object will be facing the (1, 0) direction (right) when the angle is 0. If it faces a different direction, then you need to offset the angle. My answer here explains how to do that. – Nicol Bolas Nov 9 '11 at 5:50