Jump to content

Question about node rotation


photo

Recommended Posts

Hi everyone,

    There are a question about rotate a node,  There is the default material in the new C++ project's scene:

post-2223-0-33185300-1467686985_thumb.png

 

 

     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:

 

post-2223-0-36047200-1467686933_thumb.png

 

     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:

 

post-2223-0-61741300-1467686718_thumb.png

 

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

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));
  • Like 1
Link to comment
×
×
  • Create New...