Ok, I have this ship object which the user controls using the arrow keys.
if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
mAngle -= 0.1f;
else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
mAngle += 0.1f;
if (aCurrentKeyboardState.IsKeyDown(Keys.Up) ==开发者_高级运维 true)
{
// mSpeed.Y = SHIP_SPEED;
// mDirection.Y = MOVE_UP;
velocity.X = (float)Math.Cos(mAngle) * SHIP_SPEED;
velocity.Y = (float)Math.Sin(mAngle) * SHIP_SPEED;
}
else if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true)
{
mSpeed.Y = SHIP_SPEED;
mDirection.Y = MOVE_DOWN;
}
The following 2 methods are in another class.
//Update the Sprite and change it's position based on the passed in speed, direction and elapsed time.
public void Update(GameTime theGameTime, Vector2 velocity, float theAngle)
{
Position += velocity * (float)theGameTime.ElapsedGameTime.TotalSeconds;
shipRotation = theAngle;
}
//Draw the sprite to the screen
public void Draw(SpriteBatch theSpriteBatch)
{
Vector2 Origin = new Vector2(mSpriteTexture.Width / 2, mSpriteTexture.Height / 2);
theSpriteBatch.Draw(mSpriteTexture, Position, new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height),
Color.White, shipRotation, Origin, Scale, SpriteEffects.None, 0);
}
The problem is that: the program thinks the front of the ship is one of the side wings, so in a way it's going sideways when I press up. I don't know why this is.
Suggestions?
Easy, play around with these two lines:
velocity.X = (float)Math.Cos(mAngle) * SHIP_SPEED;
velocity.Y = (float)Math.Sin(mAngle) * SHIP_SPEED;
For example switch the sin and cos, and make one of them negative:
velocity.X = -(float)Math.Sin(mAngle) * SHIP_SPEED;
velocity.Y = (float)Math.Cos(mAngle) * SHIP_SPEED;
If it goes backward now, just switch the negative over:
velocity.X = (float)Math.Sin(mAngle) * SHIP_SPEED;
velocity.Y = -(float)Math.Cos(mAngle) * SHIP_SPEED;
If I understand your problem correctly, you could solve your problem just by rotating your ship texture. Just try adding 90 or -10 to your shipRotation in the Draw Method:
theSpriteBatch.Draw(mSpriteTexture, Position, new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height),
Color.White, shipRotation + 90, Origin, Scale, SpriteEffects.None, 0);
or
theSpriteBatch.Draw(mSpriteTexture, Position, new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height),
Color.White, shipRotation - 90, Origin, Scale, SpriteEffects.None, 0);
Hope that helps!
Then change your original sprite to match what your program "thinks" is the front of the ship.
精彩评论