Created
July 17, 2012 06:47
-
-
Save marvin/3127678 to your computer and use it in GitHub Desktop.
xna move play to mouse direction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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