-2
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ 2D or 3D? Guess 3D? \$\endgroup\$ Commented Dec 19, 2012 at 20:06
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Dec 19, 2012 at 20:40

1 Answer 1

2
\$\begingroup\$

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);
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged .