What is the correct way to interpolate physics for rotations?

Godot Version

4.3

Question

I have a script that interpolates the position and rotation rigid bodies between physics frames so that it looks good on higher refresh rates. It works fine for the most part, but occasionally, the rotation will bug out. Like the object will randomly turn 180 degrees and other such issues.

This is the code i use:

func _process(_delta):
	var frac = Engine.get_physics_interpolation_fraction()
	global_position = lerp(prev_pos, current_pos, frac)
	global_rotation.y = lerp_angle(prev_rot.y, current_rot.y, frac)
	global_rotation.x = lerp_angle(prev_rot.x, current_rot.x, frac)
	global_rotation.z = lerp_angle(prev_rot.z, current_rot.z, frac)

note that the script is attached to the mesh corresponding to the rigid body, not the rigid body itself.

Interpolating with euler angles is generally iffy for various reasons (gimbal lock, angles wrapping around). The documentation suggests using quaternions for interpolating between rotations. See here.

Thanks! I have yet to notice anymore issues after implementing this, so I assume it works now.

1 Like

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