Difficulties with rotation of a sprite

Posted by Andy on Game Development See other posts from Game Development or by Andy
Published on 2012-12-07T22:52:59Z Indexed on 2012/12/09 5:20 UTC
Read the original article Hit count: 199

Filed under:
|

I want to program a dolphin that jumps and rotates like a real dolphin. Jumping is not the problem, but I don't know how to make the rotation. My dolphin always rests in the same angle while it jumps. But I want that it changes the rotation during the jump, like a real dolphin does. How can I improve the rotation?

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D image, water;
    float Gravity = 5.0F;
    float Acceleration = 20.0F;
    Vector2 Position = new Vector2(1200,720);
    Vector2 Velocity;
    float rotation = 0;
    SpriteEffects flip;
    Vector2 Speed = new Vector2(0, 0);

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.PreferredBackBufferWidth = 1280;
        graphics.PreferredBackBufferHeight = 720;
    }

    protected override void Initialize()
    {
        base.Initialize();
    }

    protected override void LoadContent()
    {   
        spriteBatch = new SpriteBatch(GraphicsDevice);
        image = Content.Load<Texture2D>("cartoondolphin");
        water = Content.Load<Texture2D>("background");
        flip = SpriteEffects.None;
    }

    protected override void Update(GameTime gameTime)
    {
        float VelocityX = 0f;
        float VelocityY = 0f;

        float time = (float)gameTime.ElapsedGameTime.TotalSeconds;
        KeyboardState kbState = Keyboard.GetState();
        if(kbState.IsKeyDown(Keys.Left)) 
        {
            rotation = 0;
            flip = SpriteEffects.None;
            VelocityX += -5f;
        } 

        if(kbState.IsKeyDown(Keys.Right)) 
        {
            rotation = 0;
            flip = SpriteEffects.FlipHorizontally;
            VelocityX += 5f;
        } 

        // jump if the dolphin is under water
        if(Position.Y >= 670)
        {
            if (kbState.IsKeyDown(Keys.A))
            {     
                if (flip == SpriteEffects.None)
                { 
                  rotation = 45;
                  VelocityY += 40f;
                }
                else
                { 
                  rotation = -45;
                  VelocityY += 40f;
                }
            }
        } 
        else 
        {
            VelocityY += -10f;
        }

        float deltaY = 0;
        float deltaX = 0;

        deltaY = Gravity * (float)gameTime.ElapsedGameTime.TotalSeconds;

        deltaX += VelocityX * (float)gameTime.ElapsedGameTime.TotalSeconds * Acceleration;
        deltaY += -VelocityY * (float)gameTime.ElapsedGameTime.TotalSeconds * Acceleration;

        Speed = new Vector2(Speed.X + deltaX, Speed.Y + deltaY);

        Position += Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;

        Velocity.X = 0;

        if (Position.Y + image.Height/2 > graphics.PreferredBackBufferHeight)
            Position.Y = graphics.PreferredBackBufferHeight - image.Height/2;

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();
        spriteBatch.Draw(water, new Rectangle(0, graphics.PreferredBackBufferHeight -100, graphics.PreferredBackBufferWidth, 100), Color.White);
        spriteBatch.Draw(image, Position, null, Color.White, MathHelper.ToRadians(rotation), new Vector2(image.Width / 2, image.Height / 2), 1, flip, 1);
        spriteBatch.End();           

        base.Draw(gameTime);
    }
}

© Game Development or respective owner

Related posts about c#

Related posts about XNA