我正在尝试使用 XNA 构建 2.5 引擎。基本上,我想在 3D 背景中显示 2D 精灵(主要英雄和其他怪物)。游戏将是一个平台。
现在,在精灵上使用平移矩阵不会产生与在世界空间中平移顶点几何体相同的结果。
我的意思是,如果我应用
Matrix.CreateTranslation(new Vector3(viewportWidth / 2, viewportHeight / 2, 0));
精灵,它将在屏幕中间平移(从显示左上角原点开始)。但是,如果我对世界空间中的立方体应用相同的变换,它将平移很远。这并不让我感到惊讶,但我想知道如何将精灵和 3D 对象平移相同的距离,忽略所有项目/非项目坐标内容。
谢谢!
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!
发布评论
评论(1)
传统上有三个矩阵:世界、视图和项目。
BasicEffect
和大多数其他 3D 效果都只有这些矩阵。您可以使用 Project 来定义点如何从 3D 世界投影到 2D 视口(视口左下角的 (-1,-1) 到右上角的 (1,1))。您设置“视图”以在世界空间中移动相机。您可以使用 World 在世界空间中移动模型。SpriteBatch
有点不同。它有一个隐式的项目矩阵,使您的世界空间与视口的客户空间匹配(左上角的(0,0)和右下角的(宽度,高度))。您可以将transformMatrix
矩阵传递给Begin
,您通常可以将其视为视图矩阵。然后传递给 Draw 的参数(位置、旋转、缩放等)就像世界矩阵一样。如果您需要在
SpriteBatch
中对世界或项目矩阵执行“奇怪”的操作,您只需将这些转换构建到transformMatrix
中即可。它可能只涉及一些数学来“撤消”内置转换。在 XNA 4 中,您还可以直接在
SpriteBatch
中使用Effect
(如BasicEffect
),您可以为它提供任意矩阵 (详细信息)。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 atransformMatrix
matrix toBegin
which 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).