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 `Head` 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 :

• `head.rotation.x += deg2rad(-event.relative.y * mouse_sensitivity)`

Why does this happen?

Godot version v3.3.4.stable.official
in Engine
edited

+1 vote

`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.

`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:

https://docs.godotengine.org/en/stable/tutorials/3d/using_transforms.html

Hope it helps.

by (2,156 points)
selected
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.