get angular velocity of camera 3d

Godot Version

4.7.2

Question

So this kinda works, however if you cross a specific angle (every 360 deg) the velocity is really high since the difference between the last rotation vector and new one is so big.

Any idea how to fix that, approach it in a better way?

@export var camera : Camera3D

var cam_vel = Vector3(0, 0, 0)
	
func _process(_delta: float) -> void:
	
	camera_velocity(camera.global_rotation_degrees)

func camera_velocity(last_rotation: Vector3):

	await get_tree().create_timer(0.1).timeout
	cam_vel = camera.global_rotation_degrees - last_rotation

Don’t use euler angles. Instead, get the angle between current and previous look vector (-global_basis.z). And get rid of that await.

thanks but could you give it to me in a code example, I’m new to gd-script so I have no idea how to do that

func get_angular_velocity(basis_now: Basis, basis_previous_frame: Basis, delta: float) -> Vector3:
	var delta_quaternion := basis_now.get_rotation_quaternion() * basis_previous_frame.get_rotation_quaternion().inverse()
	return delta_quaternion.get_axis() * delta_quaternion.get_angle() / delta