How to translate a sprite and a vertex geometry by the same distance?

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.

SpriteBatch is 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 a transformMatrix matrix to Begin which you can generally think of like the View matrix. And then the parameters you pass to Draw (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 your transformMatrix . It may just involve some maths to "undo" the built-in transformations.

In XNA 4 you can also use an Effect (like BasicEffect ) directly in SpriteBatch , which you can provide with arbitrary matrices (details).

链接地址: http://www.djcxy.com/p/95510.html

上一篇: 在基本的XNA引擎中优化绘图调用

下一篇: 如何将Sprite和顶点几何体转换相同的距离?