XNA WorldMatrix 和 ViewMatrix [英] XNA WorldMatrix and ViewMatrix

查看:44
本文介绍了XNA WorldMatrix 和 ViewMatrix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 Camera 类,允许我以第一人称在场景中移动.在我决定将其用作向 3D 世界添加某些内容之前,相机一直运行良好.我想要做的是当我按下鼠标按钮时向世界添加一个立方体.我想立方体最终远离相机,但现在我只想在它前面创建它.有时它会起作用,有时它会在一侧或另一侧创建它.这完全取决于我旋转和平移相机的程度.

I have created a Camera class that allows me to move around a scene in first person. The camera has worked just fine until I decided to use it as a location to add something to the 3D world. What I am trying to do is add a cube to the world when I press a mouse button. I want to cube to eventually travel away from the camera, but for now I just want to create it right in front of it. Sometimes it works and sometimes it creates it to one side or the other. It all depends on how much I've rotated and translated the camera.

我正在尝试使用视图矩阵在我的相机前找到向量,如下所示:

I am tryinng to find the vector in front of my camera by using the View Matrix like so:

Vector3 inFront = Camera.ViewMatrix.Forward;

我计划使用向量在立方体后面添加一些物理特性,并让它远离相机.现在我只想得到一个正确的 Vector.

I plan to use the vector to add some physics behind the cube and have it travel away from the camera. For now I am just wanting to get a correct Vector.

我知道您通常使用 WorldMatrix 在世界上绘制东西,但我不知道如何将我的 ViewMatrix 转换为 WorldMatrix.还在学习:-)

I know you normally draw thing in the world using the WorldMatrix, but I can't figure out how to convert my ViewMatrix into a WorldMatrix. Still learing :-)

我做错了什么?

-斯科特

推荐答案

首先,世界矩阵"和视图矩阵",它们都是变换矩阵,区别有点随意.有些系统甚至将两者结合起来(OpenGL 只是有一个ModelView"矩阵).

First of all, there is no real difference between a "World Matrix" and a "View Matrix", they are both transformation matrices and the distinction is somewhat arbitrary. Some systems even combine the two (OpenGL simply has a "ModelView" matrix).

传统上,世界矩阵"用于将单个模型从模型空间"移动到世界空间".然后使用视图矩阵"将所有模型从世界空间移动到它们在相机前面的相对位置(实际上,这就是移动相机").最后,投影矩阵"将 3D 位置转换为它们在屏幕上的 2D 位置(通常使用透视投影).因为它们是矩阵,所以它们可以相乘成为一个矩阵,可以在一个步骤中转换点.

Traditionally the "world matrix" is used to move individual models from "model space" to "world space". Then the "view matrix" is used to move all the models from world space into their relative positions in front of the camera (which, in effect, "moves the camera"). And finally the "Projection Matrix" converts the 3D positions into their 2D positions on the screen (generally with a perspective projection). Because they are matrices, they can be multiplied together into a single matrix that can transform points in a single step.

首先看一下属性Matrix 结构体.

First of all, take a look at the properties of the Matrix struct.

您还需要意识到Matrix.Forward 返回一个Vector3.Vector3 可以表示位置 标量和方向.你需要两个来代表一个位置一个方向.

What you also need to realise is that Matrix.Forward returns a Vector3. A Vector3 can represent either a position or a scalar and a direction. You need two of them to represent a position and a direction.

现在,我的 3D 矩阵数学有点生疏,但我很确定你想要的是 Matrix.Translation 作为相机的 position世界空间.并且 Matrix.Forward 作为相机在世界空间中的方向.

Now, my 3D matrix maths is a bit rusty, but I'm pretty sure that what you want is the Matrix.Translation as the position of the camera in world space. And Matrix.Forward as the forward direction of the camera in world space.

除非您的相机/视图矩阵正在对世界执行缩放操作(实际上它不应该),否则您从 Matrix.Forward 返回的 Vector3将具有单位长度 - 换句话说只是一个方向(无标量).使用它来指示移动对象的方向.

Unless your camera/view matrix is performing a scaling operation on the world (and really it shouldn't), then the Vector3 you get back from Matrix.Forward will have unit length - in other words just a direction (no scalar). Use this to give a direction to move your object in.

这篇关于XNA WorldMatrix 和 ViewMatrix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆