I am drawing a Texture2D onto the screen and have the need to rotate the texture forward, in a 3D way. I'm using the SpriteBatch Draw() method and it only allows limited 2D rotation around the Z axis. From tons of research online I have boiled down the solution to passing either an effect or matrix to SpriteBatch's Begin() method; however, after many hours of trial and error I haven't come up with anything working.
I sort of got it. Maybe with some tweaking you can get it the rest of the way.
first in the Update, I get the amount to rotate by
var ks = Keyboard.GetState(); if (ks.IsKeyDown(Keys.Down)) xAxis += 1f; if (ks.IsKeyDown(Keys.Up)) xAxis -= 1f; Xtransform = Matrix.CreateRotationX(MathHelper.ToRadians(xAxis));
then create an effect and apply the transformation. The
Matrix.CreateOrthographicOffCenter is just to position our camera. It is assuming a 800x600 screen. We then multiply it by our rotation transform.
BasicEffect e = new BasicEffect(GraphicsDevice); e.World = Matrix.Identity; e.View = Matrix.Identity; e.TextureEnabled = true; e.Texture = sprite; e.Projection = Matrix.CreateOrthographicOffCenter(0, 800, 600, 0, 0, 1) * Xtransform;
Now in Draw just apply the effect to the SpriteBatch.Begin method (using the 4th overload). I say it sort of works because it only rotates TOWARDS the screen. It disappears when you rotate away. The picture below shows what it looks like before (top) and after (bottom). This is what you wanted, right? I know it just looks squished, but it is a 2d texture. It isn't meant to bend that way :)