A RigidBody3D and Torque mystery

Godot Version

Godot 4.3 dev (manually compiled)

Question

(The question has evolved a bit to: how to keep a rigid body somewhat upright while other forces are acting on it.)

I have a weird situation. I want a fish (rigidbody3d) to swim towards a target. This involves turning left/right (around fish Y) and turning up and down (around the X).

Here’s a snippet of the code:

		var T : Vector3
		#Fish left/right
		if true:
			T = basis.y
			T = tspeed * T
			apply_torque_impulse(T)

		#Fish up/down
		if true:
			T = basis.x
			T = -tspeed * T
			apply_torque_impulse(T)

When one or the other ifs happen, the fish behaves as expected, but when both are true the fish rolls on its side and goes a bit mad.

It reminds me of a gimbal-lock kind of problem.

I have an MRP on my Gtlab: Files · torque_issue · Donn Ingle —Dbat / fishy · GitLab

Can you explain what conditions you’re actually checking? It looks like you’re always going to execute both if statements because true is always true.

As for avoiding gimbal lock, try only rotating on the global y. That axis won’t be affected by local x axis rotation. Just clamp the x axis rotation so the fish doesn’t flip upside down.

Thanks Ben. The ifs were there to let me observe the behaviour one at a time or both, as a testing mechanism.

I think the problem is def me. When the fish is vertical (it swam up a bit) and then turns left, it’s suddenly on its side! This stuff is like doing rubick’s cubes in my head. Argh!

Oddly, I have tried doing the global Y thing and it still does the same.