3d 向量的全局角度
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设你的意思是“向上、向右和向前”,因为向下与向上相反,并且实际上不会提供任何新信息。
您的问题不是很清楚,但我认为您的意思是您想要创建一个到新坐标基的转换,该坐标基由您描述的向量定义。如果这些向量是正交的(它们之间有 90 度),那么您不需要计算角度和使用 glRotate() 的所有麻烦。相反,您可以直接使用新基数的向量作为变换。
假设您拥有的向量是 A(a1,a2,a3) - 向上,B(b1,b2,b3) - 向右,C(c1,c2,c3) - 向前。
首先,如果它们不完全正交,您需要确保它们变得正交,可能需要一些叉积。其次,您需要确保它们的长度为 1。现在创建以下矩阵:
这是旋转矩阵,它将把您从单位基数带到由 A,B,C 定义的基数
有了这个矩阵,您需要做的就是使用 glMultMatrix() 就完成了。
如果第一次尝试不起作用,转置矩阵可能会修复它。
编辑再次检查后,矩阵的正确顺序应如下所示:
对于向量 A(ax,ay,az), B(bx,by,bz), C(cx,cy,cz)
这是上述答案的转置。
另外,我建议您首先尝试看看它是否可以在没有翻译的情况下工作。
然后您可以通过简单地将其添加到矩阵中来添加翻译,如下所示:
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:
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)
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:
其中 dp3 是 3 分量点积,nrm 标准化 3 分量向量,vec3 按照定义构造一个。
这将为您提供向量与默认坐标基础之间的角度。
编辑:当然,正如上面指出的,您很可能已经有了一个可以应用的基础矩阵。也很容易正交归一化。实际上,我想不出什么时候你需要做我上面所做的事情..但是..嘿...这就是你所要求的;)
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 ;)