How To: Rotate a Sprite

Demonstrates how to rotate a sprite around its center. The source code in this example assumes that the texture being loaded is 64×64 pixels.

The Complete Sample

The code in this topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Drawing a Rotated Sprite

To draw a rotated sprite on screen

  1. Follow the procedures of How To: Draw a Sprite.

  2. Determine the screen location of the sprite, and the point within the texture that will serve as the origin.

    By default, the origin of a texture is (0,0), the upper-left corner. When you draw a sprite, the origin point in the texture is placed on the screen coordinate specified by the at parameter. In this example, the origin is the center of the texture, and the screen position is the center of the screen.

    private Texture2D SpriteTexture;
    private Vector2 origin;
    private Vector2 screenpos;
    protected override void LoadContent()
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);
        SpriteTexture = Content.Load<Texture2D>("ship");
        Viewport viewport = graphics.GraphicsDevice.Viewport;
        origin.X = SpriteTexture.Width / 2;
        origin.Y = SpriteTexture.Height / 2;
        screenpos.X = viewport.Width / 2;
        screenpos.Y = viewport.Height / 2;
  3. In your Update method, determine the rotation angle to use for the sprite.

    The angle is specified in radians, and it can be greater than two times π, but does not need to be.

    private float RotationAngle = 0f;
    protected override void Update(GameTime gameTime)
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
        // The time since Update was called last.
        float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
        // TODO: Add your game logic here.
        RotationAngle += elapsed;
        float circle = MathHelper.Pi * 2;
        RotationAngle = RotationAngle % circle;
  4. In your own Draw method, call SpriteBatch.Draw with the texture, angle, screen position, and origin of the texture.

    protected override void Draw(GameTime gameTime)
        // TODO: Add your drawing code here
        spriteBatch.Draw(SpriteTexture, screenpos, null, Color.White, RotationAngle,
            origin, 1.0f, SpriteEffects.None, 0f);
  5. When all the sprites have been drawn, call End on your SpriteBatch object.

Community Additions