Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spritebatch.Begin() Transform Matrix

Tags:

xna

I have been wondering for a while about how the transform matrix in spriteBatch is implemented. I've created a 2D camera, and the transform matrix is as follows:

            if (needUpdate)
            transformMatrix =
               Matrix.CreateTranslation(-Position.X, -Position.Y, 0) *
                Matrix.CreateScale(curZoom, curZoom, 1) ; needUpdate = false;

The camera works as good as I want, but I just want to know how the transformation is applied: Does the transformation only affects the axis of the sprites, or the screen co-ordinates too?

Thanks in advance!

like image 756
Ahmed Mohamed Avatar asked Sep 10 '26 18:09

Ahmed Mohamed


1 Answers

I see you've answered your own question, but to provide complete information - SpriteBatch provides a similar interface to the traditional world-view-projection system of transformations.

The SpriteBatch class has an implicit projection matrix that takes coordinates in the "client space" of the viewport ((0,0) at the top left, one unit per pixel) and puts them on screen.

The Begin call has an overload that accepts a transformation matrix, which is the equivalent of a view matrix used for moving the camera around.

And the Draw call, while not actually using a matrix, allows you to specify position, rotation, scale, etc - equivalent to a world matrix used for positioning a model in the scene (model space to world space).

So you start with your "model" equivalent - which for SpriteBatch is a quad (sprite) of the size of the texture (or source rectangle). When drawn, that quad is transformed to its world coordinates, then that is transformed to its view coordinates, and then finally that is transformed to its projection coordinates.

like image 76
Andrew Russell Avatar answered Sep 16 '26 15:09

Andrew Russell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!