How can I rotate Texture2D in XNA game studio?

Hi, I need to rotate Texture2D, but I don't know why. it has an image and I need to rotate it 180 ° because it is on the head and should be right.

the problem is I need to do this before using it spriteBatch.Draw

, is it possible?

+3


source to share


2 answers


The texture will be rotated 180 degrees.

    protected override void Draw(GameTime gameTime)
    {
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearWrap, DepthStencilState.None, RasterizerState.CullCounterClockwise, null);
        spriteBatch.Draw(texture, Position, null, Color.White, (float)Math.PI, new Vector2(texture.Width, texture.Height), 1, SpriteEffects.None, 0);
        spriteBatch.End();
        base.Draw(gameTime);
    }

      



As you can, you can flip the texture vertically using SpriteEffects:

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearWrap, DepthStencilState.None, RasterizerState.CullCounterClockwise, null);
        spriteBatch.Draw(texture, Position, null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.FlipVertically, 0);
        spriteBatch.End();
        base.Draw(gameTime);
    }

      

+3


source


The method Draw

has an overload that will allow you to rotate the texture. I'm not sure I understand why you need to rotate the texture before painting it. Scaling and rotation are usually done while drawing. As Joel points out, the rotation is done using radians and you also need to consider the origin, which is the point at which you rotate.



+3


source







All Articles