2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 1, 2012 at 9:10

3 Answers 3

5
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 1, 2012 at 9:17
0
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ "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 :| \$\endgroup\$
    – user9790
    Commented Feb 29, 2012 at 20:31
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Feb 29, 2012 at 20:40
0
\$\begingroup\$

"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.

\$\endgroup\$
5
  • \$\begingroup\$ Can you represent skew using a XNA matrix? I don't think you can. \$\endgroup\$ Commented Feb 29, 2012 at 22:36
  • \$\begingroup\$ senocular.com/flash/tutorials/transformmatrix \$\endgroup\$
    – Superbest
    Commented Feb 29, 2012 at 23:08
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Feb 29, 2012 at 23:11
  • \$\begingroup\$ Excellent reference though. I appreciate you sharing it. \$\endgroup\$ Commented Feb 29, 2012 at 23:13
  • \$\begingroup\$ @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 ;) \$\endgroup\$ Commented Mar 1, 2012 at 9:09

You must log in to answer this question.