在OpenGL c ++中旋转子对象

问题描述 投票:3回答:1

我正在为我的OpenGL应用程序添加一个带有多个DoG的机器人(后来我想为这个机器人实现反向运动),我需要移动“连接”在一起的对象。

到目前为止,我将3个STL对象添加到场景中,它看起来像this

有base,joint1和joint2。

基座是静止的,关节1和关节2现在基于键盘输入绕Z轴旋转,但它们都只围绕它们的原点旋转,这对于关节1是好的,但是对于关节2我需要它们连接并相应地平移。

Here是轮换后的输出。

对于物体定位和旋转我使用常规MVP矩阵和glm。

joint1M = glm::translate(joint1M, glm::vec3(-50.0f, -10.4f, 0.0f));
joint1M = glm::rotate(joint1M, glm::radians(rotation), glm::vec3(0.0f, 1.0f, 0.0f));
joint2M = glm::translate(joint2M, glm::vec3(-50.0f, 2.85f, 0.0f));
joint2M = glm::rotate(joint2M, glm::radians(rotation), glm::vec3(0.0f, 1.0f, 0.0f));

有没有办法在OpenGL中创建像父母一样的Unity关系?或者我应该以联合1旋转的方式改变关节2的位置?

c++ opengl rotation glm-math robot
1个回答
2
投票

感谢评论和更多谷歌搜索我设法得到这样的工作:

joint1保持不变:

joint1M = glm::translate(joint1M, glm::vec3(-50.0f, -10.4f, 0.0f));
joint1M = glm::rotate(joint1M, glm::radians(rotation), glm::vec3(0.0f, 1.0f, 0.0f));

joint2移动到joint1的位置,旋转,移动到它应该的位置

joint2M = glm::translate(joint2M, glm::vec3(-50.0f, -10.4f, 0.0f));
joint2M = glm::rotate(joint2M, glm::radians(rotation), glm::vec3(0.0f, 1.0f, 0.0f));
joint2M = glm::translate(joint2M, glm::vec3(0.0f, 13.25f, 0.0f));
© www.soinside.com 2019 - 2024. All rights reserved.