I’m trying to build up a 2.5 engine with XNA. Basically, I want to display a 2D sprites (the main hero and other monsters) in a 3D background. The game will be a platform.
Now, using a translation matrix on a sprite doesn’t yield the same result of translate a vertex geometry in world space.
I mean, if I apply
Matrix.CreateTranslation(new Vector3(viewportWidth / 2, viewportHeight / 2, 0));
the sprite will be translate at the middle of screen (starting from the display upper left origin). But, if I apply the same transform to a cube in world space, it will translate very far. This doesn’t suprising me, but I wonder of to translate a sprite and a 3D object by the same distance, ignoring all the project/unproject coord stuffs.
Thanks!
There are traditionally three matrices: World, View and Project.
BasicEffect, and most other 3D Effects, simply have those matrices. You use Project to define how points are projected from the 3D world onto the 2D viewport ((-1,-1) in the bottom-left of the viewport to (1,1) in the top-right). You set View to move your camera around in world space. And you use World to move your models around in world space.SpriteBatchis a bit different. It has an implicit Project matrix that causes your world space to match the viewport’s client space ((0,0) in the top-left and (width,height) in the bottom-right). You can pass atransformMatrixmatrix toBeginwhich you can generally think of like the View matrix. And then the parameters you pass toDraw(position, rotation, scale, etc) work like the World matrix would.If you need to do “weird” things to your World or Project matrices in
SpriteBatch, you can just build those transforms into yourtransformMatrix. It may just involve some maths to “undo” the built-in transformations.In XNA 4 you can also use an
Effect(likeBasicEffect) directly inSpriteBatch, which you can provide with arbitrary matrices (details).