XNA 4.0:2D 相机 Y 和 X 方向错误
所以我知道有一些关于为 XNA 构建 2D 相机的问题/答案,但是人们似乎只是很乐意发布他们的代码而无需解释。 我正在寻找更多关于我做错了什么的解释。
首先,我了解了整个世界->查看->投影->画面变换。
我的目标是拥有一个位于视口中心的相机对象,当相机的位置向上移动时,它与在视口中向上移动相关联,当它向右移动时,它与在视口中向右移动相关联。
我在实现该功能时遇到困难,因为视口的 Y 值是反转的。
//In Camera Class
private void UpdateViewTransform()
{
//My thinking here was that I would create a projection matrix to center the camera and then flip the Y axis appropriately
Matrix proj = Matrix.CreateTranslation(new Vector3(_viewport.Width * 0.5f, _viewport.Height * 0.5f, 0)) *
Matrix.CreateScale(new Vector3(1f, -1f, 1f));
//Here is the camera Matrix. I have to give the Inverse of this matrix to the Spritebatch I believe since I want to go from World Coordinates to Camera Coordinates
_viewMatrix = Matrix.CreateRotationZ(_rotation) *
Matrix.CreateScale(new Vector3(_zoom, _zoom, 1.0f)) *
Matrix.CreateTranslation(_position.X, _position.Y, 0.0f);
_viewMatrix = proj * _viewMatrix;
}
有人可以帮助我了解如何构建视图转换以传递到 SpriteBatch 中,以便实现我想要的目标。
编辑
这作为一种转变似乎有效,但我不确定为什么。有人可以帮我解释一下吗:
Matrix proj = Matrix.CreateTranslation(new Vector3(_viewport.Width * 0.5f, _viewport.Height * 0.5f, 0));
_viewMatrix = Matrix.CreateRotationZ(_rotation) *
Matrix.CreateScale(new Vector3(_zoom, _zoom, 1.0f)) *
Matrix.CreateTranslation(-1 * _position.X, _position.Y, 0.0f);
_viewMatrix = proj * _viewMatrix;
我以前构建过一个光线追踪器,所以我应该理解你的理解,我的困惑在于它是 2D 的,而 SpriteBatch 对我隐藏了它在做什么。 谢谢! 法里德
So I know there are a few questions/answers regarding building a 2D Camera for XNA however people seem to just be happy posting their code without explanation.
I'm looking for more of an explanation of what I'm doing wrong.
First off, I understand the whole World -> View - > Projection - > Screen transformation.
My goal is to have a camera object that is centered in the center of the viewport and that when the camera's position moves up it correlates to moving up in the viewport and when it moves to the right it correlates moving right in the viewport.
I'm having difficulty implementing that functionality because the Y value of the viewport is inverted.
//In Camera Class
private void UpdateViewTransform()
{
//My thinking here was that I would create a projection matrix to center the camera and then flip the Y axis appropriately
Matrix proj = Matrix.CreateTranslation(new Vector3(_viewport.Width * 0.5f, _viewport.Height * 0.5f, 0)) *
Matrix.CreateScale(new Vector3(1f, -1f, 1f));
//Here is the camera Matrix. I have to give the Inverse of this matrix to the Spritebatch I believe since I want to go from World Coordinates to Camera Coordinates
_viewMatrix = Matrix.CreateRotationZ(_rotation) *
Matrix.CreateScale(new Vector3(_zoom, _zoom, 1.0f)) *
Matrix.CreateTranslation(_position.X, _position.Y, 0.0f);
_viewMatrix = proj * _viewMatrix;
}
Can someone help me understand how I can build my view transformation to pass into the SpriteBatch so that I achieve what I'm looking for.
EDIT
This as a transformation seems to work however I am unsure why. Can someone perhaps break it down for me in understanding:
Matrix proj = Matrix.CreateTranslation(new Vector3(_viewport.Width * 0.5f, _viewport.Height * 0.5f, 0));
_viewMatrix = Matrix.CreateRotationZ(_rotation) *
Matrix.CreateScale(new Vector3(_zoom, _zoom, 1.0f)) *
Matrix.CreateTranslation(-1 * _position.X, _position.Y, 0.0f);
_viewMatrix = proj * _viewMatrix;
I've built a raytracer before so I should understand your understanding, my confusion lies with the fact it's 2D and that SpriteBatch is hiding what it's doing from me.
Thanks!
Farid
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用比例矩阵翻转 Y 轴上的所有内容,则意味着您正在翻转
SpriteBatch
正在绘制的模型(纹理四边形)。这意味着您还必须更改缠绕顺序(即:背面剔除解释为您正在绘制面向相机的三角形的背面,因此它会剔除它们,因此您必须更改它使用的规则)。默认情况下,
SpriteBatch
使用RasterizerState.CullCounterClockwise
。当您调用SpriteBatch.Begin
时,您需要传入RasterizerState.CullClockwise
。当然,
Begin
是您传递变换矩阵的地方。我没有仔细检查你的矩阵运算 - 尽管我怀疑顺序不正确。我建议您创建一个非常简单的测试应用程序,并一次构建一个转换。
If you flip everything on the Y axis with your scale matrix, that means you are flipping the models that
SpriteBatch
is drawing (textured quads). This means you also have to change your winding-order (ie: backface culling is interpreting that you are drawing the backs of the triangles facing the camera, so it culls them, so you have to change the rule that it uses).By default,
SpriteBatch
usesRasterizerState.CullCounterClockwise
. When you callSpriteBatch.Begin
you need to pass inRasterizerState.CullClockwise
instead.And, of course,
Begin
is where you pass in your transformation matrix.I haven't carefully checked your matrix operations - although I have a suspicion that the order is incorrect. I recommend you create a very simple testing app and build up your transformations one at a time.
我与 XNA 进行了很多斗争,试图让它像我以前使用过的其他引擎一样......我的建议,它不值得..只需遵循 XNA 标准并使用 Matrix 辅助方法来创建透视/视口矩阵。
I've fought with XNA a lot trying to get it to be like other engines I have worked with before... my reccomendation, it isn't worth it... Just go with the XNA standards and use the Matrix helper methods for creating your Perspective / Viewport matrices.
因此,经过逻辑思考后,我能够推断出正确的转换。
我将在这里概述这些步骤,以防有人想要真正的细分:
了解什么是相机转换或视图转换非常重要。
视图变换通常是从相机的相对坐标到世界空间坐标所需的。然后,视图变换的逆过程将创建相对于您的相机的世界坐标!
创建视图矩阵
现在矩阵的逆将做世界坐标 - >查看坐标。
我还选择将相机置于屏幕中央,因此我添加了预先附加的翻译。
So after just thinking logically, I was able to deduce the proper transformation.
I'll outline the steps here incase anyone wants the real breakdown:
It is important to understand what is a Camera Transformation or View Transformation.
A View Transformation is normally what is needed to go from Coordinates relative to your Camera to World-Space coordinates. The inverse of the View Transformation would then make a world coordinate relative to your Camera!
Creating a View Matrix
Now the inverse of the matrix will do World Coordinate -> View Cordinates.
I also chose to center my camera in the center of the screen, so I included a pre-appended translation.