c++ opengl将模型坐标转换为世界坐标以进行碰撞检测

发布于 2024-08-07 12:27:24 字数 1280 浏览 3 评论 0原文

(这都是正交模式,原点位于左上角,x向右为正,y沿着y轴为正)

我在世界空间中有一个矩形,它可以有一个旋转m_rotation(以度为单位)。

我可以很好地处理矩形,它可以旋转、缩放,以及你想要它做的一切。

我真正困惑的部分是从本地坐标计算矩形的世界坐标。

我一直在尝试使用这个公式:

x' = x*cos(t) - y*sin(t) 
y' = x*sin(t) + y*cos(t)

where (x, y) are the original points,
(x', y') are the rotated coordinates,
and t is the angle measured in radians

from the x-axis. The rotation is
counter-clockwise as written. 
-credits duffymo

我尝试实现这样的公式:

//GLfloat Ax = getLocalVertices()[BOTTOM_LEFT].x * cosf(DEG_TO_RAD( m_orientation )) - getLocalVertices()[BOTTOM_LEFT].y * sinf(DEG_TO_RAD( m_orientation ));
//GLfloat Ay = getLocalVertices()[BOTTOM_LEFT].x * sinf(DEG_TO_RAD( m_orientation )) + getLocalVertices()[BOTTOM_LEFT].y * cosf(DEG_TO_RAD( m_orientation ));

//Vector3D BL = Vector3D(Ax,Ay,0);

我创建一个到翻译点的向量,将其存储在矩形 world_vertice 成员变量中。没关系。然而,在我的主绘制循环中,我从 (0,0,0) 到向量 BL 绘制一条线,看起来这条线从矩形上的点(矩形左下角)开始绕圈。围绕世界坐标原点。

基本上,随着 m_orientation 变大,它会围绕 (0,0,0) 世界坐标系原点绘制一个巨大的圆。编辑:当 m_orientation = 360 时,它会设置回 0。

我觉得我做错了这部分:

t 是以弧度为单位测量的角度 从 x 轴。

可能我不应该在这个公式中使用 m_orientation (矩形旋转角度)?

谢谢!

编辑:我这样做的原因是为了碰撞检测。我需要知道矩形(很快将成为刚体)的坐标位于世界坐标中的位置以进行碰撞检测。

(This is all in ortho mode, origin is in the top left corner, x is positive to the right, y is positive down the y axis)

I have a rectangle in world space, which can have a rotation m_rotation (in degrees).

I can work with the rectangle fine, it rotates, scales, everything you could want it to do.

The part that I am getting really confused on is calculating the rectangles world coordinates from its local coordinates.

I've been trying to use the formula:

x' = x*cos(t) - y*sin(t) 
y' = x*sin(t) + y*cos(t)

where (x, y) are the original points,
(x', y') are the rotated coordinates,
and t is the angle measured in radians

from the x-axis. The rotation is
counter-clockwise as written. 
-credits duffymo

I tried implementing the formula like this:

//GLfloat Ax = getLocalVertices()[BOTTOM_LEFT].x * cosf(DEG_TO_RAD( m_orientation )) - getLocalVertices()[BOTTOM_LEFT].y * sinf(DEG_TO_RAD( m_orientation ));
//GLfloat Ay = getLocalVertices()[BOTTOM_LEFT].x * sinf(DEG_TO_RAD( m_orientation )) + getLocalVertices()[BOTTOM_LEFT].y * cosf(DEG_TO_RAD( m_orientation ));

//Vector3D BL = Vector3D(Ax,Ay,0);

I create a vector to the translated point, store it in the rectangles world_vertice member variable. That's fine. However, in my main draw loop, I draw a line from (0,0,0) to the vector BL, and it seems as if the line is going in a circle from the point on the rectangle (the rectangles bottom left corner) around the origin of the world coordinates.

Basically, as m_orientation gets bigger it draws a huge circle around the (0,0,0) world coordinate system origin. edit: when m_orientation = 360, it gets set back to 0.

I feel like I am doing this part wrong:

and t is the angle measured in radians
from the x-axis.

Possibly I am not supposed to use m_orientation (the rectangles rotation angle) in this formula?

Thanks!

edit: the reason I am doing this is for collision detection. I need to know where the coordinates of the rectangles (soon to be rigid bodies) lie in the world coordinate place for collision detection.

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

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

发布评论

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

评论(2

人│生佛魔见 2024-08-14 12:27:24

你所做的就是在 2d 上对具有角度 Q 的向量进行旋转[特殊线性变换]。它保持向量长度并围绕原点改变其方向。

alt text

[线性变换:加法 L(m + n) = L(m) + L(n) 其中 {m , n} € 向量,齐次 L(k.m) = k.L(m) 其中 m € 向量和 k € 标量 ] 所以:

您将向量分为两块。就像 m[1, 0] + n[0, 1] = 你的向量。
然后,正如您在图像中看到的那样,对这两块进行旋转,之后您的矢量将取
形式:

m[cosQ, sinQ] + n[-sinQ, cosQ] = [mcosQ - nsinQ, msinQ + ncosQ]

你也可以看看Wiki 旋转

如果您尝试获取与对象坐标相对应的眼睛坐标,你应该将你的对象坐标乘以opengl中的模型视图矩阵。

对于M=>模型视图矩阵和 [xyzw] 的转置是您所做的对象坐标:

M[xyzw]T = [xyzw]T 的眼睛坐标

What you do is rotation [ special linear transformation] of a vector with angle Q on 2d.It keeps vector length and change its direction around the origin.

alt text

[linear transformation : additive L(m + n) = L(m) + L(n) where {m, n} € vector , homogeneous L(k.m) = k.L(m) where m € vector and k € scalar ] So:

You divide your vector into two pieces. Like m[1, 0] + n[0, 1] = your vector.
Then as you see in the image, rotation is made on these two pieces, after that your vector take
the form:

m[cosQ, sinQ] + n[-sinQ, cosQ] = [mcosQ - nsinQ, msinQ + ncosQ]

you can also look at Wiki Rotation

If you try to obtain eye coordinates corresponding to your object coordinates, you should multiply your object coordinates by model-view matrix in opengl.

For M => model view matrix and transpose of [x y z w] is your object coordinates you do:

M[x y z w]T = Eye Coordinate of [x y z w]T

转身泪倾城 2024-08-14 12:27:24

这似乎有点让事情变得过于复杂:通常,您会将对象的世界位置和方向与其自己的本地坐标集分开存储。旋转对象是在模型空间中完成的,因此位置不变。无论是否进行旋转,每个坐标的世界位置都是相同的 - 将世界位置添加到本地位置以将本地坐标转换到世界空间。

任何旋转都会围绕特定原点发生,典型的 sin/cos 公式假定 (0,0) 是原点。如果使用的坐标系当前没有 (0,0) 作为原点,则必须将其转换为原点,执行旋转,然后变换回来。通常定义模型空间,使 (0,0) 成为模型的原点,从而使此步骤变得微不足道。

This seems to be overcomplicating things somewhat: typically you would store an object's world position and orientation separately from its set of own local coordinates. Rotating the object is done in model space and therefore the position is unchanged. The world position of each coordinate is the same whether you do a rotation or not - add the world position to the local position to translate the local coordinates to world space.

Any rotation occurs around a specific origin, and the typical sin/cos formula presumes (0,0) is your origin. If the coordinate system in use doesn't currently have (0,0) as the origin, you must translate it to one that does, perform the rotation, then transform back. Usually model space is defined so that (0,0) is the origin for the model, making this step trivial.

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