【问题标题】:Rotating child object in OpenGL c++在OpenGL c ++中旋转子对象
【发布时间】:2019-08-17 02:24:30
【问题描述】:

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

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

有base、joint1和joint2。

基座是静止的,joint1 和joint2 现在根据键盘输入围绕Z 轴旋转,但它们都只是围绕它们的原点旋转,这对于joint1 来说很好,但是对于joint2,我需要将它们连接起来并进行相应的平移.

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 关系的东西?或者我应该根据joint1旋转以某种方式改变joint2的位置?

【问题讨论】:

  • 我建议您阅读有关场景图和分层树的内容。简而言之,它可以帮助您建立亲子关系。每当父级在场景中发生变化时,其子级的位置也会相应地改变
  • 问题OpenGL translation before and after a rotation是关于legacy OpenGL(固定函数管道矩阵栈)的,但说明了原理。

标签: c++ opengl rotation glm-math robot


【解决方案1】:

感谢 cmets 和更多的谷歌搜索,我设法让它像这样工作:

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));

【讨论】:

    猜你喜欢
    • 2016-07-22
    • 1970-01-01
    • 2017-08-24
    • 2012-02-16
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多