Skip to content

Instantly share code, notes, and snippets.

@marvin
Created July 17, 2012 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marvin/3127678 to your computer and use it in GitHub Desktop.
Save marvin/3127678 to your computer and use it in GitHub Desktop.
xna move play to mouse direction
// The spritebatch has a draw method that takes in a rotation angle, you can see the overloads for draw here.
//First, you would want to get the mouse's location and store it as a vector;
MouseState curMouse = Mouse.GetState();
Vector2 mouseLoc = new Vector2(curMouse.X, curMouse.Y);
// Next, subtract the mouse's position from the sprite's position to get the direction (as a vector) that the sprite should face and convert it to a angle measured in radians;
Vector2 direction = (sprite's position) - mouseLoc;
float angle = (float)(Math.Atan2(direction.Y, direction.X));
// Finally, you just have to draw your sprite using this angle in the direction argument. Just remember to set the proper origin argument too (the center of the sprite that should be rotated around (typically new Vector2(sprite.Width/2, sprite.Height/2)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment