c++ - 在OpenGL c ++中旋转子对象
问题描述
我正在向我的 OpenGL 应用程序中添加一个具有多个 DoG 的机器人(稍后我想为这个机器人实现逆运动学),我需要移动“连接”在一起的对象。
到目前为止,我在场景中添加了 3 个 STL 对象,它看起来像这样
有base、joint1和joint2。
底座是静止的,joint1 和joint2 现在根据键盘输入围绕Z 轴旋转,但它们都只是围绕它们的原点旋转,这对于joint1 来说很好,但是对于joint2,我需要将它们连接起来并进行相应的平移。
这里是旋转后的输出。
对于对象定位和旋转,我使用常规 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 关系的东西?或者我应该基于joint1旋转以某种方式改变joint2的位置?
解决方案
感谢评论和更多的谷歌搜索,我设法让它像这样工作:
关节 1 保持不变:
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));
关节2移动到关节1的位置,旋转,移动到它应该在的位置
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));
推荐阅读
- python - 唯一字符串值的累积聚合
- javascript - React 组件多次重新渲染
- python-2.7 - 如何从子图中完全删除图并正确调整大小?
- azure - 数据湖 Blob 存储
- stored-procedures - ADF 复制数据活动 - 从动态表达式中为 Sink 存储过程参数值引用 Source 列
- python - 检测类属性值更改,然后更改另一个类属性
- python - 将 wav 数据的左通道读入 numpy 数组
- vb.net - 当我在我的线程中添加一个列表框项目时,我需要发出哔哔声。它在一个线程中,线程子程序不会让我做任何事情,而是添加项目
- lua - 即使值不正确,Roblox Lua if 语句仍在执行
- json - PowerShell:无论深度如何,如何扫描每个 PSObject 属性名称并替换句点字符?