rotate_object_local vs. rotate_y/x/z: Why is this code not equivalent?

Godot Version

Godot 4.4.1 Stable

Question

Hello!
I’ve come across this code in the manual for First Person Camera Movement using Transforms.

# accumulators
var rot_x = 0
var rot_y = 0

func _input(event):
	if event is InputEventMouseMotion and event.button_mask & 1:
		# modify accumulated mouse rotation
		rot_x += event.relative.x * LOOKAROUND_SPEED
		rot_y += event.relative.y * LOOKAROUND_SPEED
		transform.basis = Basis() # reset rotation
		rotate_object_local(Vector3(0, 1, 0), rot_x) # first rotate in Y
		rotate_object_local(Vector3(1, 0, 0), rot_y) # then rotate in X

From my current understanding, rotate_object_local(Vector3(0, 1, 0), rot_x) and rotate_y(rot_x) should be equivalent. However, replacing rotate_object_local(Vector3(0, 1, 0), rot_x) with rotate_y(rot_x) and rotate_object_local(Vector3(1, 0, 0), rot_y) with rotate_x(rot_y) results in different behavior of the camera: It starts tilting when moving diagonally.

For my test scene, I just have a simple, untransformed camera at world origin.

Why would this change not result in the same behavior?

rotate_object_local take a local axis into the calculations. So when you start rotating, the rotations will start to accumulate and result in a weird behavior.

Edited, because I mixed the functions up and explained the other way.

2 Likes

Ah, yeah that’s it. While I was reading the documentation the writing that it was transforming the local transform made me think it would use the local axes, but I mixed some concepts up. Thanks!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.