Getting the rotation axis with respect to the preceding nodes' rotation

Godot Version

4.3

Question

I have followed this tutorial video https://www.youtube.com/watch?v=j_rf8zc5kTE and I have a CharacterBody3D Node which has this similar structure:

CharacterBody3D
  Node3D # used for pivot
    SpringArm3D
      Camera3D 

I would like to extend further from the Youtube Video above in that I will have a camera that smoothly jumps around between different cameras like this: https://www.youtube.com/watch?v=Vep3_3pQao0 1 (see 6:55 onward)

As I have multiple Camera3Ds in my scene already, I decided to create a main Camera3D called mainCamera which will be the active current Camera3D jumping around from one target Camera3D to next, using the variablecameraOfInterest. See this code here:

	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 )

(Yes, this is the same code with my other topic but the issue discussed here is different)

There are no issues with the positioning, but for the rotation, there is an issue with a Camera3D that the cameraOfInterest’s rotation axis is static. As I follow the tutorial Youtube video above, the code at 3:23, the CharacterBody3D’s y-axis is rotated and the pivoting Node3D (CamOrigin)'s x axis is being rotated. The camera’s rotation did not change, as its preceding parents are the entities being rotated.

Yes, I could retrieve the CharacterBody3D’s y-axis and CamOrigin’s x-axis and put into the code above, but in the future, if I have other constructs which have their own structures and hierarchy, this will not go well. So I would like to have a solution that just provide me the rotation Vector3D and disregard whatever structures the Camera3D is in.

Is it possible to get the Camera3D’s current rotation Vector3D with respect to all the preceding nodes’ rotation? How to do this in Godot 4?

I found the solution: global_rotation (Node3D — Godot Engine (stable) documentation in English):

		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 )

This will get the current global rotation and you do not have to care about the preceding Nodes’ rotations.

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

Damn. I was just about to post my answer when you solved your own issue. Ah well, I’ll post it anyway for anyone else coming across this problem.

I would say that unless you need your objects to be in relation to something else, you should get into the habit of using global_position/global_rotation over the position/rotation you’re currently using. As the name suggests, these variables provides you with the object’s position/rotation in the global space (just like global_transform does).

Using the global transformation data of a node ensures that the data represents its “true” state.

	# Reference data
	var current_transform = mainCamera.global_transform
	var target_transform = cameraOfInterest.global_transform

	# Computation of new transform data
	var new_position = current_transform.origin.lerp(target_transform.origin, delta * 6)
	var new_basis = current_transform.basis.slerp(target_transform.basis, delta * 6)

	# Assignment of computed data
	mainCamera.global_position = new_position
	mainCamera.global_basis = new_basis

# NOTE: You likely need to adjust the weight-parameter for the rotation.
1 Like

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