-2

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

Ady
  • 73
  • 1
  • 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

1 Answers1

2

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);
neeKo
  • 222
  • 1
  • 7