# Can I use SpriteBatch when drawing sprites on a rotating 3D plane

I'd like to have my plane of sprites rotate similarly as in this game (YouTube Video). So basically, everything is drawn in 2D, but the 2D plane is then "rotated in 3D" (or camera is rotated). When rotated, sprites further away should look like they are further away.

Question:

Can I use SpriteBatch with this? IIRC SpriteBatch uses Orthographic view matrix, so probably not? If I can, how? If not, how would you suggest I draw the sprites?

PS. I am really bad at explaining problems so feel free to ask any questions.

• I fixed up your question to mention that you're rotating the 2D plane of sprites - as per the video, rather than rotating individual sprites. – Andrew Russell Mar 1 '12 at 9:10

Yes!

Let's have a look at how SpriteBatch works:

SpriteBatch draws sprites onto a 2D plane. So if you had wanted to rotate your sprites so that they come "off" this 2D plane, then you'd probably have to find another solution. But, looking at your video, it looks like you want to rotate the entire 2D plane.

SpriteBatch's normal mode of operation uses a built-in orthographic projection matrix into client space. It also lets you pass in an optional global transformation matrix. You could use this global transformation to get your effect, but you'd also have to "pre-undo" the projection matrix that comes afterwards, and the maths is annoying.

But fear not - SpriteBatch has another mode of operation, where it can use any Effect, using that effect's vertex shader and transformation matrices. BasicEffect will do nicely here. Take a look at this article to get the basic principles.

Simply set BasicEffect.Projection matrix to a suitable perspective projection. And then set BasicEffect.World matrix so that your plane of sprites appears in the correct location (and the right way up!).

Take a look at this article as well. It describes the process in more detail.

• Just to add a note for those needing to write a custom vertex shader for SpriteBatch - on the second linked article, there's a comment by Remi Gillig which might be helpful too. But in this case you won't need a custom vertex shader since just a regular BasicEffect instance with the correct parameters will do. – David Gouveia Mar 1 '12 at 9:17

Can I use SpriteBatch when drawing sprites with 3D rotation

I'm afraid not.

You can however render the sprites you want to rotate together to a RenderTarget2D. Then skin a quad with the previously mentioned RenderTarget.

This describes the skin and draw a quad part.

Edit: watched the video on my phone :).

Anyways to achieve a similar effect you just need 1 quad and one render target. Draw you whole scene to a rendertarget and apply it to a single quad. you can then either move your camera or quad, whichever you find more convenient.

• "and then draw it using an orthogonal camera." I thought I had to use normal ( perspective ) -camera? I want sprites futher away to actually look like they are furthe, like in that video. I thought that isn't possible with orthogonal camera :| – user9790 Feb 29 '12 at 20:31
• My bad I can't view youtube at work :/ and was assuming that you were going for a super paper mario effect (2d camera w/ 3d sprite transformations). But anyways you can use what ever type of camera you like. – ClassicThunder Feb 29 '12 at 20:40

"Technically", no. SpriteBatch exists for 2D sprites.

However, you can supply transformation matrices to the SpriteBatch, and unless I'm mistaken this allows you to skew and stretch your sprite to your heart's content. You would, of course, have to manually calculate everything, but you could fake 3D by clever transformation of 2D.

• Can you represent skew using a XNA matrix? I don't think you can. – ClassicThunder Feb 29 '12 at 22:36
• senocular.com/flash/tutorials/transformmatrix – Superbest Feb 29 '12 at 23:08
• My bad I meant taper and you can't. "Things they cannot do include tapering or distorting with perspective" So using the transformation matrix isn't going to work. – ClassicThunder Feb 29 '12 at 23:11
• Excellent reference though. I appreciate you sharing it. – ClassicThunder Feb 29 '12 at 23:13
• @ClassicThunder Actually, you can. That reference Superbest linked describes 2D affine transformation matrices. XNA's matrices are 4x4 matrices and should be able to perform tapering operations. (You should be able to cancel out the built-in ortho matrix completely - see my answer. But I'll leave the maths for someone else ;) – Andrew Russell Mar 1 '12 at 9:09