jiangqiu Posted July 5, 2016 Share Posted July 5, 2016 Hi everyone, There are a question about rotate a node, There is the default material in the new C++ project's scene: After rotate the material_ball about Y-axis by these code: node = Editor::get()->getNodeByName("material_ball"); Mat4 transform = node.get()->getTransform(); mat4 rot = rotateY(90.0f); node.get()->setTransform(transform * Mat4(rot)); The material_ball become this: And then I rotate it about Z-axis by these code: node = Editor::get()->getNodeByName("material_ball"); Mat4 transform = node.get()->getTransform(); mat4 rot = rotateY(90.0f); mat4 rot2 = rotateZ(-30.0f); node.get()->setTransform(transform * Mat4(rot) * Mat4(rot2)); The material_ball become this: Form the above view, I find that the rotate function is rote the node about it's own axis, How can I rotate it about the world axis ? I have tried the setWorldTransform function like this: node = Editor::get()->getNodeByName("material_ball"); Mat4 transform = node.get()->getWorldTransform(); mat4 rot = rotateY(90.0f); mat4 rot2 = rotateZ(-30.0f); node.get()->setWorldTransform(transform * Mat4(rot) * Mat4(rot2)); But it's result is the same with setTransform function. Link to comment
ded Posted July 5, 2016 Share Posted July 5, 2016 Hi, I think you multiply matrices in the wrong order. Here's what happening in your last snippet: the node is rotated by -30 degrees along Z axis, then by 90 degrees along Y axis and then the original transformation is applied. By the way, Ptr<T> has overloaded operator->, so you can invoke methods without ".get()". Please try the following code: mat4 rot_y = rotateY(90.0f); mat4 rot_z = rotateY(-30.0f); node->setWorldTransform(transform * Mat4(rot_z) * Mat4(rot_y)); 1 Link to comment
Recommended Posts