在两个坐标空间之间变换对象

发布于 2024-07-13 18:21:22 字数 738 浏览 9 评论 0原文

所以我正在阅读《图形和游戏开发的 3D 数学入门》一书,我几乎没有数学背景,我终于开始掌握矢量/矩阵数学 - 这是一种解脱。

但是,是的,总有一个但是,我无法理解对象从一个坐标空间到另一个坐标空间的转换。 在书中,作者举了一个例子,向一辆转向的汽车开枪(图片) “世界空间”中的 20 度(为了简单起见,只是一个 2D 空间)。 所以我们有三个空间:世界空间、枪对象空间和汽车对象空间 - 对吗? 书中接着写道:

“在这张图中,我们引入了一支步枪,它正在向汽车发射子弹。如图所示 左边的坐标空间,我们通常会从了解枪和轨迹开始 世界空间中的子弹。 现在,想象一下将坐标空间变换为与 汽车的物体空间,同时保持汽车、枪和子弹的轨迹静止。 现在我们 知道枪的位置和子弹在汽车物体空间中的轨迹,我们 可以进行交叉测试,看看子弹是否会击中汽车以及击中汽车的位置。”

我遵循这个解释,当我事先知道汽车在世界空间中旋转 20* 度时,这不是问题 -但是,当我在游戏中让弓箭手从山上向其他人射击时,我不知道那里的所有物体都被移动的角度,那么这会如何转化为

世界的旋转角度呢?或者枪空间?是的,你可以看到我有点困惑,

我认为理想的反应是使用汽车和枪的例子,使用任意变量来表示位置、角度等。

So I'm reading the "3D Math Primer For Graphics And Game Development" book, coming from pretty much a non-math background I'm finally starting to grasp vector/matrix math - which is a relief.

But, yes there's always a but, I'm having trouble understand the translation of an object from one coordinate space to another. In the book the author takes an example with gun shooting at a car (image) that is turned 20 degrees (just a 2D space for simplicity) in "world space". So we have three spaces: World Space, Gun Object Space and Car Object Space - correct? The book then states this:

"In this figure, we have introduced a rifle that is firing a bullet at the car. As indicated by the
coordinate space on the left, we would normally begin by knowing about the gun and the trajectory
of the bullet in world space. Now, imagine transforming the coordinate space in line with the
car’s object space while keeping the car, the gun, and the trajectory of the bullet still. Now we
know the position of the gun and the trajectory of the bullet in the object space of the car, and we
could perform intersection tests to see if and where the bullet would hit the car."

And I follow this explanation, and when I beforehand know that the car is rotated 20* degrees in world space this isn't a problem - but how does this translate into a situation say when I have an archer in a game shooting from a hill down on someone else? I don't know the angle at which everything is displaced there?

And which object space is rotated here? The World or Gun space? Yeah as you can see I'm a bit confused.

I think the ideal response would be using the car and gun example using arbitrary variables for positions, angle, etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无尽的现实 2024-07-20 18:21:22

您应该阅读如何更改基础并以向量的方式思考,而不是数组,但数学数组:P

You should read how to change basis and think in vector, not arrays but the math ones :P

纵山崖 2024-07-20 18:21:22

我曾经是一名游戏程序员,我一次又一次地这样做。 最终,我放弃了使用角度。 对于每个对象,我都有一个前向向量和一个向上向量。 然后,您可以从叉积中获得面向右的向量。 而空间之间的所有转换都变成了点积。

I used to be a game programmer and I did that time after time. Eventually, I got away from using angles. For every object, I had a forward-facing vector and an up vector. You can get the right-facing vector, then, from a cross-product. And all the conversions between spaces become dot products.

失退 2024-07-20 18:21:22

您了解坐标空间和变换在 2D 中如何工作的概念吗? 我发现在尝试转向 3D 之前,坐标空间和变换在 2D 中更容易可视化。 这样你就可以在纸上写出“假设”场景,并帮助你理解主要概念。

在您发布的图像中,我认为解释是汽车本身的内部坐标系没有改变,但它的系统已经相对于世界系统旋转了。

你必须明白汽车有自己的局部坐标系。 汽车的几何形状是根据其局部坐标系定义的。 因此,无论汽车在世界中的方向如何,汽车的长度始终沿着其本地系统中的 x 轴延伸。 小车可以通过变换其局部坐标系来定向。

坐标系始终是相对于另一个坐标系定义的,根坐标系除外,在本例中是世界坐标系。 所以枪有自己的系统,汽车有自己的系统,它们都嵌入到世界的系统中。 如果我相对于世界旋转或移动汽车的系统,那么即使几何形状不变,汽车也会出现旋转。

如果无法绘制出视觉场景,这是很难解释的,而且我的谷歌搜索无法找到对基础知识的良好描述。

Do you understand how the notion of how coordinate spaces and transforms work in 2D? I find that coordinate spaces and transforms are a lot easier to visualize in 2D before trying to move to 3D. That way you can work "what-if" scenarios out on paper, and helps you to just grok the major concepts.

In the image you posted I think the interpretation is that the car itself has not changed in its internal coordinate system, but that its system has been rotated with respect to the World's system.

You have to understand that the car has its own local coordinate system. The geometry of the car is defined in terms of its local coordinate system. So the length of the car always extends along the x-axis in its own local system regardless of its orientation in the World. The car can be oriented by transforming its local coordinate system.

Coordinate systems are always defined relative to another system, except for the root, in this case the World. So the gun has its own system, the car has its own system and they are both embedded into the World's system. If I rotate or move the car's system with respect to the World then the car will appear to rotate even though the geometry is unchanged.

This is something that is very hard to explain without being able to draw out visual scenarios and my google-fu is failing to find good descriptions of the basics.

寄居人 2024-07-20 18:21:22

正如之前的回复所建议的,保持向上、向前和向右的向量是定义(欧几里得)坐标空间的好方法。 如果您还添加原点,那就更好了,因为您可以表示更广泛的空间。

假设我们有两个空间 A 和 B,在 A 中,向上、向前和向右分别是 (0,1,0)、(0,0,1) 和 (1,0,0),原点为零这给出了 A 的通常左手 xyz 坐标。假设 B 我们有 u=(ux,uy,uz)、f=(fx,fy,fz) 和 r=(rx,ry,rz),原点 o = (牛、oy、盎司)。 然后对于 B 中 p = (x,y,z) 处的点,我们在 A 中得到 (x*rx + y*ux + z*fx + ox, x*ry + y*uy + z*fy + oy, x *rz + y*uz + z*fz + 盎司)。

这可以通过检查得出。 观察到,由于 B 的右、上和前向量在 A 的每个轴上都有分量,因此 B 中某些坐标的分量必须对 A 中坐标的所有三个分量都有贡献。即,因为 (0,1,0) 在B 等于 (ux,uy,uz),则 (x,y,z) = y*u + (一些其他东西)。 如果我们对每个坐标都这样做,我们就会得到 (x,y,z) = x*r + y*u + z*f + (一些其他东西)。 如果我们观察到除了(一些其他东西)之外这些项在原点消失,那么我们意识到(一些其他东西)实际上必须是 o,这使得 A 中的坐标为 x*r + y*u + z*f + o,即 (x*rx + y*ux + z*fx + ox, x*ry + y*uy + z*fy + oy, x*rz + y*uz + z*fz + oz )一旦向量运算被扩展。

这个操作也可以反过来,我们只需在 A 中设置坐标并求解方程即可在 B 中找到它们。例如,A 中的 (1,1,1) 等于 x*r + y*u + z*f + o B 中。这给出了三个未知数的三个方程,可以通过联立方程组的方法求解。 我不会在这里解释这一点...但如果您遇到困难,这里有一个链接:链接

所有这些与您最初的子弹和汽车示例有何关系? 好吧,如果您随汽车旋转一组上/右/前矢量,并在汽车平移时更新原点,您可以从世界空间移动到汽车的本地空间,并使一些测试变得更容易。 例如,您可以将子弹转换为“汽车本地”空间并使用本地坐标,而不是转换碰撞模型的顶点。 如果您要转换汽车的顶点以在 GPU 上进行渲染,但又不想承受读回该信息以用于 CPU 上的物理计算的开销,那么这会很方便。

在其他用途​​中,它可以通过变换三个点并执行这些操作来节省您对 x 点的变换,这允许您在大量点上组合 x 变换,而不会比跨相同数量的点的单个变换显着影响性能。

As a previous reply suggests, keeping an up, forward and right vector is a good way to define a (Euclidean) coordinate space. Its even better if you add an origin as well, since you can represent a wider range of spaces.

Lets say we have two spaces A and B, in A, up, forward and right are (0,1,0), (0,0,1) and (1,0,0) respectively, and the origin is at zero this gives the usual left-handed xyz coordinates for A. Say for B we have u=(ux,uy,uz), f=(fx,fy,fz) and r=(rx,ry,rz) with origin o = (ox,oy,oz). Then for a point at p = (x,y,z) in B we have in A (x*rx + y*ux + z*fx + ox, x*ry + y*uy + z*fy + oy, x*rz + y*uz + z*fz + oz).

This can be arrived at by inspection. Observe that, since the right, up and forward vectors for B have components in each axis of A, a component of some coordinates in B must contribute to all three components of the coordinates in A. i.e. since (0,1,0) in B is equal to (ux,uy,uz), then (x,y,z) = y*u + (some other stuff). If we do this for each coordinate we have that (x,y,z) = x*r + y*u + z*f + (some other stuff). If we make the observation that the at the origin these terms vanish except for (some other stuff) then we realise that (some other stuff) must in fact be o, which gives the coordinates in A as x*r + y*u + z*f + o, which is (x*rx + y*ux + z*fx + ox, x*ry + y*uy + z*fy + oy, x*rz + y*uz + z*fz + oz) once the vector operations are expanded.

This operation can be reversed as well, we just set the coordinates in A and solve equations to find them in B. e.g. (1,1,1) in A is equal to x*r + y*u + z*f + o in B. This gives three equations in three unknowns and can be solved by the method of simultaneous equations. I won't bother explaining that here... but here is a link if you get stuck: link

How does all of this relate to your original example of a bullet and a car? Well, if you rotate a set of up/right/forward vectors with the car, and update the origin as the car is translated you can move from world space to the car's local space and make some tests easier. e.g instead of transforming vertices for a collision model, you can transform the bullet into 'car local' space and use the local coordinates. This is handy if you are going to transform the car's vertices for rendering on a GPU, but don't want to suffer the overhead of reading that information back to use for physics calculations on the CPU.

In other uses it can save you transforming x points by transforming three points and performing these operations instead, this allows you to combine x transformations on a large number of points without a significant performance hit over a single transformation across the same number of points.

甜妞爱困 2024-07-20 18:21:22

在游戏情况下,通常您不会知道汽车本身旋转了 20 度; 相反,您的汽车定位信息将隐式包含该知识。 因此,在这个二维示例中,您需要知道汽车中心的 x,y 坐标以及汽车指向的 x,y 向量(这两条信息都位于世界空间中)——否则您将无法来画它。 这两条信息是您找到在世界空间和汽车的物体空间之间转换的矩阵所需的全部信息。 (然后一个人可以在这个例子中查看该矩阵并说,哦,看,旋转 20 度 - 但这不是您通常在游戏中担心的信息。)

枪的问题和汽车可以在三个空间中的任何一个中解决。 那么问题来了,哪个最容易呢? 据推测,枪的空间是为了让子弹沿着 X 轴发射而设置的。 所以很容易将其转化为其他空间。 一辆 2D 汽车可能会在它自己的对象空间中表示——可能是一组 2D 线段或 2D 像素或其他东西。 您当然可以将它们转换为世界空间或枪的对象空间,但如果您在汽车对象空间中解决问题,则根本不需要转换它们,因此这是解决此问题的最简单的方法。

这有点像相对论:从它自己的角度来看,没有一个空间是旋转的。 然而,与相对论不同的是,我们将世界空间视为一个优先的固定参考系。 因此,对象的模型空间相对于世界空间进行旋转、镜像、缩放、平移等。

In a game situation generally you wouldn't know the car was rotated 20 degrees, per se; instead your positioning information for the car would implicitly contain that knowledge. So in this two dimensional example, you'd know the x,y coordinates of the center of the car and x,y vector the car is pointing (both pieces of information in the world space) -- otherwise you wouldn't be able to draw it. Those two pieces of information are all you need to find the matrix to transform between world space and the car's object space. (And then a person could look at that matrix in this example and say, oh, look, rotation by 20 degrees -- but that's not a piece of information you'd normally worry about in the game.)

The problem of the gun and the car can be solved in any of the three spaces. So the question is, which is it easiest in? Presumably the gun's space is set up so that the bullet is fired down the X axis. So it's easy to translate that into either of the other spaces. A 2D car is probably going to be represented in its own object space -- maybe as a set of 2D line segments or 2D pixels or something. You certainly could translate those into world space or the gun's object space, but if you solve the problem in car object space you don't have to translate them at all, so that's the easiest one to work in for this problem.

It's sort of like relativity: from its own perspective, none of the spaces are rotated. Unlike relativity, though, we treat the world space as a privileged fixed frame of reference. So the objects' model spaces are rotated, mirrored, scaled, translated, etc with respect to the world space.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文