3d 向量的全局角度

发布于 2024-08-05 22:22:41 字数 240 浏览 7 评论 0原文

我有 3 个向量,右上和前代表一个方向。

我需要将它们转换为 XYZ 角度(3 个浮点数),以便我可以与 glRotatef() 一起使用,

请帮助

[编辑]

它无法正确渲染。你能看看这里是否有什么明目张胆的事情吗: pastebin.com/f6683492d

I have 3 Vectors, Up Right and Front that represent a direction.

I need to convert these into an XYZ angle (3 floats) so i can use with glRotatef()

Please help

[EDIT]

Its not rendering properly. can you see if its anything blatant here: pastebin.com/f6683492d

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

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

发布评论

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

评论(2

意中人 2024-08-12 22:22:41

我假设你的意思是“向上、向右和向前”,因为向下与向上相反,并且实际上不会提供任何新信息。
您的问题不是很清楚,但我认为您的意思是您想要创建一个到新坐标基的转换,该坐标基由您描述的向量定义。如果这些向量是正交的(它们之间有 90 度),那么您不需要计算角度和使用 glRotate() 的所有麻烦。相反,您可以直接使用新基数的向量作为变换。

假设您拥有的向量是 A(a1,a2,a3) - 向上,B(b1,b2,b3) - 向右,C(c1,c2,c3) - 向前。
首先,如果它们不完全正交,您需要确保它们变得正交,可能需要一些叉积。其次,您需要确保它们的长度为 1。现在创建以下矩阵:

a1 b1 c1 0
a2 b2 c2 0
a3 b3 c3 0
0  0  0  1

这是旋转矩阵,它将把您从单位基数带到由 A,B,C 定义的基数
有了这个矩阵,您需要做的就是使用 glMultMatrix() 就完成了。
如果第一次尝试不起作用,转置矩阵可能会修复它。


编辑再次检查后,矩阵的正确顺序应如下所示:
对于向量 A(ax,ay,az), B(bx,by,bz), C(cx,cy,cz)

ax ay az 0
bx by bz 0
cx cy cz 0
0  0  0  1

这是上述答案的转置。
另外,我建议您首先尝试看看它是否可以在没有翻译的情况下工作。
然后您可以通过简单地将其添加到矩阵中来添加翻译,如下所示:

ax     ay     az     0
bx     by     bz     0
cx     cy     cz     0
pos.x  pos.y  pos.z  1

I assume you mean "up, right and forward" because down is the opposite of up and doesn't relly contribute any new information.
Your question isn't very clear but I think you mean that you want to create a transformation to the new coordinate base which is defined by the vectors you describe. If these vectors are orthogonal (have 90 degrees between them) then you don't need to go through all the bother of calculating angles and using glRotate(). Instead you can use the vectors of the new base directly as the transformation.

Say the vectors you have are A(a1,a2,a3) - up, B(b1,b2,b3) - right and C(c1,c2,c3) - forward.
First, If the are not completely orthogonal you need to make sure that they become orthogonal, possibly with a few cross products. secondly, you need to make sure their length is 1. Now create the following matrix:

a1 b1 c1 0
a2 b2 c2 0
a3 b3 c3 0
0  0  0  1

This is the rotation matrix which will bring you from the unit base to the base defined by A,B,C
With this matrix all you need to do is to use glMultMatrix() and you're done.
If the first try doesn't work, transposing the matrix would probably fix it.


EDIT After checking again, the right order of the matrix should be like so:
for vector A(ax,ay,az), B(bx,by,bz), C(cx,cy,cz)

ax ay az 0
bx by bz 0
cx cy cz 0
0  0  0  1

This is the transpose of the above answer.
Also, I recommend that you first try to see if it works without translation.
And then you can add translation by simply adding it to the matrix like so:

ax     ay     az     0
bx     by     bz     0
cx     cy     cz     0
pos.x  pos.y  pos.z  1
七婞 2024-08-12 22:22:41
x = acos( dp3( nrm( up ), new vec3( 0, 1, 0 ) ) );
y = acos( dp3( nrm( dir ), new vec3( 0, 0, 1 ) ) );
z = acos( dp3( nrm( right ), new vec3( 1, 0, 0 ) ) );

其中 dp3 是 3 分量点积,nrm 标准化 3 分量向量,vec3 按照定义构造一个。

这将为您提供向量与默认坐标基础之间的角度。

编辑:当然,正如上面指出的,您很可能已经有了一个可以应用的基础矩阵。也很容易正交归一化。实际上,我想不出什么时候你需要做我上面所做的事情..但是..嘿...这就是你所要求的;)

x = acos( dp3( nrm( up ), new vec3( 0, 1, 0 ) ) );
y = acos( dp3( nrm( dir ), new vec3( 0, 0, 1 ) ) );
z = acos( dp3( nrm( right ), new vec3( 1, 0, 0 ) ) );

where dp3 is a 3 component dot product, nrm normalises a 3 component vector and vec3 constructs one as defined.

This will give you the angle between the vectr you have and a default coordinate basis.

Edit: Of course, as pointed out above, you most likely already have a basis matrix which you can apply. Very easy to Orthonormalise as well. Realistically I can't think of a time where you'd need to do what I've done above .. but .. hey ... its what you asked for ;)

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