How can I rotate Texture2D in XNA game studio?
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 to share
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 to share