Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question

marked as duplicate by Byte56, Trevor Powell, Josh Petrie, Sean Middleditch, Nick Wiggill Dec 30 '12 at 12:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
2D or 3D? Guess 3D? –  Aufziehvogel Dec 19 '12 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. –  Aufziehvogel Dec 19 '12 at 20:40
add comment

1 Answer 1

Suppose you had

Vector2 characterPosition; //on viewport
float characterRotation; //in radians

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);
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.