0 votes

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 :

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

Why does this happen?

Godot version v3.3.4.stable.official
in Engine by (53 points)
edited by

2 Answers

+1 vote
Best answer

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.

by (2,156 points)
selected by
+2 votes

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.

by (3,144 points)

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

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.