So I'm trying to have a sprite face my mouse cursor, but with my current code, the sprite is -close-, but it's not fully rotating to face my cursor. I've pasted my Player.cs class below. Right now, I initialize the direction and rotation variables within my Update(GameTime gameTime) class. I've already set the origin to be in the center of the sprite, so I don't think that's causing the issue. Thanks!
[source lang="csharp"]using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.Graphics;namespace MyTerribleGame{ class Player { #region Data Fields private Texture2D texture; private Rectangle position; private int health; private int armor; private int lives; private int velocity; private Vector2 direction; private float rotation; #endregion #region Properties public Texture2D Texture { get { return texture; } set { texture = value; } } public Rectangle Position { get { return position; } set { position = value; } } public int Health { get { return health; } set { health = value; } } public int Armor { get { return armor; } set { armor = value; } } public int Lives { get { return lives; } set { lives = value; } } public int Velocity { get { return velocity; } set { velocity = value; } } #endregion #region Constructors // Default Constructor. Will be removed when there's more implementation. public Player() { health = 100; armor = 100; lives = 100; velocity = 10; } // Overloaded Constructor. Possibly will become the new default. public Player(Rectangle position, int health, int armor, int lives, int velocity) { this.position = position; this.health = health; this.armor = armor; this.lives = lives; this.velocity = velocity; } #endregion #region Methods // Load the content of the player class. public void LoadContent(ContentManager Content) { texture = Content.Load<Texture2D>("Player/playerRed"); } // Update the player, such as input, collision detection, etc. public void Update(GameTime gameTime) { direction.X = Input.MousePos.X - position.X; direction.Y = Input.MousePos.Y - position.Y; direction.Normalize(); if (Input.IsKeyDown(Keys.W)) { position.Y -= velocity; } if (Input.IsKeyDown(Keys.S)) { position.Y += velocity; } if (Input.IsKeyDown(Keys.D)) { position.X += velocity; } if (Input.IsKeyDown(Keys.A)) { position.X -= velocity; } rotation = (float)Math.Atan2(direction.Y, direction.X); } // Draw the player to the screen. public void Draw(SpriteBatch spriteBatch) { spriteBatch.Draw(texture, position, null, Color.White, rotation, new Vector2(position.Width / 2, position.Height / 2), SpriteEffects.None, 0); } // This version of CheckCollision is used to check for collision between the screen and the player. public void CheckCollision(GraphicsDeviceManager graphics) { if (position.X <= 0) position.X += velocity; if (this.position.Y <= 0) position.Y += velocity; if (this.position.X + this.position.Width >= graphics.PreferredBackBufferWidth) position.X -= velocity; if (this.position.Y + this.position.Height >= graphics.PreferredBackBufferHeight) position.Y -= velocity; } // 2D Bounding Box collision b/w the player and an object. public void CheckCollision(Rectangle obj) { // Player -> Object // Right -> Left if (position.Right > obj.Left &amp;&amp; position.Left < obj.Left &amp;&amp; position.Bottom > obj.Top &amp;&amp; position.Top < obj.Bottom) { position.X -= velocity; } // Left -> Right if (position.Left < obj.Right &amp;&amp; position.Right > obj.Right &amp;&amp; position.Bottom > obj.Top &amp;&amp; position.Top < obj.Bottom) { position.X += velocity; } // Top -> Bottom if (position.Top < obj.Bottom &amp;&amp; position.Bottom > obj.Bottom &amp;&amp; position.Right > obj.Left &amp;&amp; position.Left < obj.Right) { position.Y += velocity; } // Bottom -> Top if (position.Bottom > obj.Top &amp;&amp; position.Top < obj.Top &amp;&amp; position.Right > obj.Left &amp;&amp; position.Left < obj.Right) { position.Y -= velocity; } } #endregion }}[/source]
Edited by nooblet, 13 November 2012 - 01:46 PM.






