I\'m working on an XNA game and I am using ViewPort.Project and ViewPort.Unproject to translate to and from world coordinates. Currently I use these for each object I draw w
I've written about SpriteBatch
and the various "spaces" (world, projection, client, etc) here, here and here. Those answers are probably worth reading.
SpriteBatch
assumes that your World space is the same thing as Client space - which is however many pixels tall and wide the viewport is, origin top-left, Y+ is down.
It looks like (based on your use of CreateOrthographic
) you want your World space to appear as 40 units tall, on screen, and however many units will fit widthways. You also want the World origin at the centre of the screen and Y+ is up.
So you have to stick another matrix in between to convert your World space to Client space. I believe the correct matrix is (in psudocode): scale(40/viewport.Height) * scale(1, -1) * translate(viewport.Width/2, viewport.Height/2)
. Scale, flip and translate to go from World to Client.
You must also remember that sprites assume that Y+ is down. So you have to pass a (1, -1)
scale into SpriteBatch.Draw
, otherwise they will be drawn inverted (and, due to backface culling, be invisible).