What is the difference between rotation.x and rotate_x

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 1MochaChan1

I am trying a make 3D FPS controller, I’ve ran into an issue regarding mouse input and looking around in the scene using Camera which is nested under a Spatial Node called Head

And I’ve set the orientation of the Headimage
so the rotation aligns with where my player is facing

However whenever I use the following line of code for vertical rotation of the camera, it starts rotating like a pendulum from left to right :

  • head.rotate_x(deg2rad(-event.relative.x * mouse_sensitivity))

Although replacing it with this code works fine with all the settings mentioned above :

Why does this happen?

:bust_in_silhouette: Reply From: Ertain

From what I’ve read in the documentation, the Spatial property rotation changes (i.e. turns) the local transformation of the Head node, while the rotated_x() function (again, from the documentation) rotates the node relative to its parent node.

That could’ve been the reason for the weird pendulum effect I was getting. Thank you for the answer.

1MochaChan1 | 2021-10-22 14:45

That little difference drove me crazy. Thank you for the answer!

raflyyyanuar | 2023-01-26 07:48

:bust_in_silhouette: Reply From: DaddyMonster

Short answer:

rotation() takes up to three angles in Vector3 format. rotate_x() takes one. So rotation() is rotate_x(), rotate_y(), rotate_z() combined.

As you used them, they’re the same.

Long answer:

rotation() contains 3 matrices (one for x, y and z). These matrices contain take the sine / cosine of your angle for that axis and multiply it by the obj’s basis matrix, transforming it.

In English: it takes a Vector3 which isn’t a vector but a container for the 3 angles you want to rotate. If you send Vector3(3, 2, 1) then it’ll rotate 3 radians on the x, 2 on the y, etc. (I’d encourage you to abandon degrees btw, expense for no gain) It’ll multiply the result to the basis of you object. So if you have IDENTITY basis on head and you send PI on the y, it’ll face the other way.

rotation.y = PI and rotation(Vector3(0, PI, 0)) are equivalent.

You need this because the order you do Euler rotations in matters, rotating y then z will not leave you in the same position as z then y. Euler rotations can be like rubik’s cubes, undoing what you’ve done can get tricky if you’re not careful.

I’d strong recommend you read through this to understand the nuances of Euler transforms as it tackles your question:

Hope it helps.