Dealing with rotation axis of Camera3D that loops around for lerp function

Godot Version

4.3

Question

Hi guys, right now, I am playing around with Camera Nodes, and I have already got several cameras up and I could instantly switch them around by just calling make_current to the camera I wanted.

Now, I wanted the camera to smoothly switch over to the other camera, by panning and moving like this: https://www.youtube.com/watch?v=Vep3_3pQao0 (see 6:55 onward) See that the game’s camera moves from one camera to the next smoothly. I want something like that, so below is what I got right now as a test. I decided to use only one mainCamera which will always be the current Camera3D and this camera will move to the position of the cameraOfInterest and imitate all of its rotation axis upon request:

	if (Input.is_action_just_pressed("cam1")):
		cameraOfInterest = get_node("cam1")
		
	if (Input.is_action_just_pressed("cam2")):
		cameraOfInterest = get_node("cam2")
	
	mainCamera.position = mainCamera.position.lerp( cameraOfInterest.global_transform.origin, delta*6 )
	
	mainCamera.rotation.x = lerp( mainCamera.rotation.x, cameraOfInterest.rotation.x, delta*6 )
	mainCamera.rotation.y = lerp( mainCamera.rotation.y, cameraOfInterest.rotation.y, delta*6 )
	mainCamera.rotation.z = lerp( mainCamera.rotation.z, cameraOfInterest.rotation.z, delta*6 )

The mainCamera could move to the cameraOfInterest’s position without any issue.

But I got into this issue that all these rotation variables loop around between -180 and 180. So for example, when I am at 179 for an axis and my camera is changed by 2 degrees, going beyond 180, it will be subtracted by 360 and become -179. This poses a problem for lerp in that now, my mainCamera will do a quick pan 360 degrees from 179 to reach -179 instead of tilting just a bit. Because of this, how to deal with this?

Just use lerp_angle (@GlobalScope — Godot Engine (stable) documentation in English) instead of lerp:

		mainCamera.rotation.x = lerp_angle( mainCamera.rotation.x, cameraOfInterest.global_rotation.x, delta*6 )
		mainCamera.rotation.y = lerp_angle( mainCamera.rotation.y, cameraOfInterest.global_rotation.y, delta*6 )
		mainCamera.rotation.z = lerp_angle( mainCamera.rotation.z, cameraOfInterest.global_rotation.z, delta*6 )

Note: I am also including the global_rotation fix from the other topic.

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