曲线轨迹物体方向

发布于 2024-10-05 13:21:37 字数 905 浏览 6 评论 0原文

我有一艘处于 3D 环境中的宇宙飞船。我使用控制点绘制了 B 样条曲线(转换为贝塞尔曲线以便能够绘制它)。每次调用绘制事件时,船舶位于点 (0,0,0) 并转换到曲线上的下一个坐标。现在,在进行此转换之前,我需要进行旋转,以便船舶的方向沿着该点的曲线切线。

通过这样做,我可以通过在曲线上找到靠近当前点的点来近似切线。我在C++中使用OpenGL,

float mDifference[1][3] = {nearPoint[0][0] - currentPosition[0][0],
                           nearPoint[0][1] - currentPosition[0][1],
                           nearPoint[0][2] - currentPosition[0][2]};

float norm = sqrt(mDifference[0][0] * mDifference[0][0] 
             + mDifference[0][1] * mDifference[0][1]
             + mDifference[0][2] * mDifference[0][2]);

float tangent[1][3] = { mDifference[0][0] / norm,
                        mDifference[0][1] / norm,
                        mDifference[0][2] / norm};

//tangent = rotationVector?
spacecraftTransformGroup->setRotationVector(tangent[0][0],tangent[0][1],tangent[0][2]);

我认为旋转矢量是切线,但找不到旋转船所需的角度。如何旋转船舶使其与切线对齐?

I have a spaceship in a 3D environnement. I drew a B-Spline curve using control points (converted to a Bezier curve to be able to draw it). Each time the draw event is called, the ship is at the point (0,0,0) and is translated to the next coordinate on the curve. Now, before doing this translation, I would need to make a rotation so that the ship's orientation is along the curve's tangent at that point.

I'm able to approximate the tangent by finding a point on the curve that is near the current one by doing this. I use OpenGL in C++

float mDifference[1][3] = {nearPoint[0][0] - currentPosition[0][0],
                           nearPoint[0][1] - currentPosition[0][1],
                           nearPoint[0][2] - currentPosition[0][2]};

float norm = sqrt(mDifference[0][0] * mDifference[0][0] 
             + mDifference[0][1] * mDifference[0][1]
             + mDifference[0][2] * mDifference[0][2]);

float tangent[1][3] = { mDifference[0][0] / norm,
                        mDifference[0][1] / norm,
                        mDifference[0][2] / norm};

//tangent = rotationVector?
spacecraftTransformGroup->setRotationVector(tangent[0][0],tangent[0][1],tangent[0][2]);

I think the rotation vector is the tangent, but can't find the angle needed to rotate the ship. How can I rotate the ship to align it with the tangent?

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

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

发布评论

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

评论(2

我喜欢麦丽素 2024-10-12 13:21:37

正交(即方向)矩阵由以下部分组成:

方向,
切线,
双切线(向上向量)。

因此,如果您有方向和切线,则可以通过叉积获得双切线,并根据 3 个方向向量 (D、T、B) 和位置向量 (P) 形成矩阵,如下所示

 Tx, Bx, Dx, Px
 Ty, By, Dy, Py
 Tz, Bz, Dz, Pz
  0,  0,  0,  1

。您的对象现在沿着方向向量 定向。 ..

Well an orthonormal (ie orientation) matrix is made up of the following:

Direction,
Tangent,
BiTangent (Up vector).

So if you have your direction and tangent you can cross product to get your bitangent and form your matrix from the 3 direction vectors (D, T, B) and position vector (P) as follows

 Tx, Bx, Dx, Px
 Ty, By, Dy, Py
 Tz, Bz, Dz, Pz
  0,  0,  0,  1

Your object is now oriented along the direction vector ...

安静被遗忘 2024-10-12 13:21:37

首先,请参阅 此站点 了解有关沿 B 样条曲线查找给定点处的实际切线的信息。

其次,您可以使用 atan(y/x) 获取正切向量的角度(以弧度为单位)。使用该角度旋转您的飞船,假设“零”角度使您的飞船指向 x 轴。

First off, see this site for information on finding the actual tangent at a given point along the B-Spline curve.

Second, you can use atan(y/x) to obtain the angle (in radians) of the tangent vector. Use that angle to rotate your ship, assuming the 'zero' angle has your ship pointing along the x-axis.

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