# If I have a character on the screen, how do I make it look in the direction my mouse is pointing? [duplicate]

Possible Duplicate:
Set sprite to face direction of mouse

I'm drawing a character on the screen, and I would like it so that the character is looking where my mouse is pointing. It's a birds eye view, and the character moves as well. Also any pathfinding collision tutorials would be appreciated

• 2D or 3D? Guess 3D? Dec 19, 2012 at 20:06
• And what do you mean with "pathfinding collision tutorials"? I googled for it and pathfinding seems to be stuff like DFS, BFS or A*. I remember something about vector-surface-cut when you need to solve the problem of where a mouse-pointer shows to in 3D, do you mean that? Somehow the question for the pathfinding does not seem to match the rest of the question at all. Dec 19, 2012 at 20:40

Vector2 characterPosition; //on viewport


Suppose you also had the current mouse position

MouseState mouseState = Mouse.GetState();
Vector2 mousePosition = new Vector2(mouseState.X, mouseState.Y);


Then you could calculate the vector that points from character to mouse position

Vector2 dPos = mousePosition - characterPosition;


You could then use Math.Atan2 to get the angle that this vector forms with positive x axis. That would be the needed angle for the character, so you set it for your characters rotation:

characterRotation = (float)Math.Atan2(dPos.Y, dPos.X);