Godot Version
4.3
Question
I’ve got a model viewer for my 3D models. Some of the models are meant to be viewed from all angles, but some are meant to be “on the ground” and therefore have no bottom. The viewer lets the player rotate the model, and I’d like to be able to constrain the rotations under some circumstances.
I have a working solution, but it’s kind of clumsy:
[...stuff...]
var rstick = Utility.get_3axis_rstick() # gamepad right stick & triggers as vec3
var model_rot = model.quaternion
var xrot = Quaternion(Vector3.RIGHT, rstick.y * delta)
var yrot = Quaternion(Vector3.UP, rstick.z * delta)
var zrot = Quaternion(Vector3.FORWARD, rstick.x * delta)
var new_rot = model_rot * xrot * yrot * zrot
if free_rotation:
model_rot = new_rot
else:
var euler = new_rot.get_euler()
if euler.x > (PI * -0.5) && euler.x < (PI * 0.5) && euler.z > (PI * -0.5) && euler.z < (PI * 0.5):
model_rot = new_rot
Does Godot have a cleaner way of clamping a quaternion like this?
1 - you can’t clamp a quaternion.
2 - you don’t need quaternions.
3 - use euler.
4 - stop manually calculating things, use the godot’s built in methods.
func _input(event) -> void:
if event is InputEventMouseMotion:
mouselook = event.relative
rotate_y(mouselook.x * sensitivity.x)
cam_v.rotate_x(mouselook.y * sensitivity.y)
func _physics_process(_delta) -> void:
cam_v.rotation.x = clampf(cam_v.rotation.x, cam_clamp.x, cam_clamp.y)
make two branches one for euler and one with quaternion.
It seems like that ought to be possible, at least through conversion to axis/angle.
I’m allowing free rotation and would prefer not to deal with gimbal locking. I have used quaternions before, and am aware of the tradeoffs.
I’m not using the mouse for this. I’m using a gamepad.
Clamping x does not stop gimbal locking. In both the constrained and unconstrained versions, I have rotation on 3 axes; in euler terms, yaw, pitch and roll. In both cases I want unlimited yaw. In the constrained case I want limited pitch and roll, in the unconstrained case I want unlimited pitch and roll.
As soon as any of yaw, pitch or roll are applied, you start to get gimbal locking, and it gets worse as any of the rotational angles approach 90 degrees. That is fundamental to how euler angles work. It’s less severe at smaller angles, but even a slight yaw (for example) causes pitch and roll to deflect. That’s what application order tries to hack around.
If I use euler angles for some objects and quaternions for others, they will rotate differently, which I’m trying to avoid.