1

I'm trying to implement a leaning mechanic in a game that I'm building. To do that I want to set one variable to act as the default number of rotation degrees (ideally x0, y0, and z0), and one for the rotation degrees of a character that is leaning to the right (ideally x0.6, y0, and z0).

Here's my code (for context, this script is attached to a Spatial node called UpperBody):

extends Spatial

const LEAN_LERP = 5
export var default_degrees : Vector3
export var leaning_degrees : Vector3

func _process(delta):
    
    if Input.is_action_pressed("LeanRight"):
        transform.origin = transform.origin.linear_interpolate(leaning_degrees, LEAN_LERP * delta)
    else:
        transform.origin = transform.origin.linear_interpolate(default_degrees, LEAN_LERP * delta)
    
    if Input.is_action_pressed("LeanLeft"):
        transform.origin = transform.origin.linear_interpolate(-leaning_degrees, LEAN_LERP * delta)
    else:
        transform.origin = transform.origin.linear_interpolate(default_degrees, LEAN_LERP * delta)

As you can see, I have both default_degrees and leaning_degrees' types set to Vector3 instead of the (currently unknown) equivalent for rotational degrees.

My question is this: how do I set a variable to contain rotational degrees?

Thanks.

1 Answer 1

0

There is no dedicated type for Euler angles. Instead you would use … drum roll … Vector3.

In fact, if you see the rotation_degrees property, you will find it is defined as a Vector3.


That, of course, isn't the only way to represent rotations/orientations. Ultimately, the Transform has two parts:

  • A Vector3 called origin which represents the translation.
  • A Basis called basis which represent the rest of the transformation (rotation, scaling and reflection, and shear or skewing).

A Basis can be thought of a trio of Vector3 each representing one of the axis of the coordinate system. Another way to think of Basis is as a 3 by 3 matrix.

Thus whatever you use to represent rotations or orientations will ultimately be converted to a Basis (and then either replace or be composed with the Basis of the transform).


Now, you want to interpolate the rotations, right? Euler angles aren't good for interpolation. Instead you could interpolate:

  • Transformations (Transform using interpolate_with Transform.interpolate_with).
  • Basis (Basis using Basis.slerp).
  • Quaternions (Quat using Quat.slerp).

On the other hand, Euler angles are good for input. In this particular case that means it is relative easy to wrap your head around what the numbers mean compared to writing any of these in the inspector.

Thus, we have two avenues:

  • Convert Euler angles to either Transform, Basis or Quat.
  • Find an easy way to input a Transform, Basis or Quat.

Euler angle to Quat

The Quat has a constructor that takes a vector for Euler angles. The catch is that it is Euler angles in radians. So we need to convert degrees to radians (which we can do with deg2rad). Like this:

var target_quat := Quat(
    Vector3(
        deg2rad(degrees.x),
        deg2rad(degrees.y),
        deg2rad(degrees.z)
    )
)

Alternatively, you could do this:

var target_quat := Quat(degrees * PI / 180.0)

We also need to get the current quaternion from the transform:

var current_quat := transform.basis.get_rotation_quat()

Interpolate them:

var new_quat := current_quat.slerp(target_quat, LEAN_LERP * delta)

And replace the quat:

transform = Transform(
    Basis(new_quat).scaled(transform.basis.get_scale()),
    transform.origin
)

The above line assumes the transformation is only rotation, scaling, and translation. If we want to keep skewing, we can do this:

transform = Transform(
    Basis(new_quat) * Basis(current_quat).inverse() * transform.basis,
    transform.origin
)

The explanation for that is in the below section.

Notice we ended up converting the Quat to a Basis. So perhaps we are better off avoiding quaternions entirely.


Euler angle to Basis

The Basis class also has a constructor that works like the one we found in Quat. So we can do this:

var target_basis := Basis(degrees * PI / 180.0)

The catch this time is that Basis does not only represent rotation. So if we do that, we are losing scaling (and any other transformation the Basis has). We can preserve the scaling like this:

target_basis = target_basis.scaled(transform.basis.get_scale())

Ah, of course, the current Basis is this:

var current_basis := transform.basis

We interpolate like this:

var new_basis := current_basis.slerp(target_basis, LEAN_LERP * delta)

And we replace the Basis like this:

transform.basis = new_basis

To be honest, I'm not happy with the above approach. I'll show you a way to have the Basis you interpolate be only for rotation (so it can preserve any skewing the original Basis had, not only its scale), but it is a little more involved. Let us start here again:

var target_rotation := Basis(degrees * PI / 180.0)

And we will not scale that, instead we want to get a Basis that is only the rotation of the current one. We can do that by going from Basis to Quat and back:

var current_rotation := Basis(transform.basis.get_rotation_quat())

We interpolate the same way as before:

var new_rotation := current_rotation.slerp(target_rotation, LEAN_LERP * delta)

But to replace the Basis we want to keep everything about the old Basis that wasn't the rotation. In other words we are going to:

  • Take the Basis:

    transform.basis
    
  • Remove its rotation (i.e. compose it with the inverse of its rotation):

    Basis(transform.basis.get_rotation_quat()).inverse() * transform.basis
    

    Which is the same as:

    current_rotation.inverse() * transform.basis
    
  • And apply the new rotation:

    new_rotation * current_rotation.inverse() * transform.basis
    

And that is what we set:

transform.basis = new_rotation * current_rotation.inverse() * transform.basis

I have tested to make sure the composition order is correct. And, yes, code for preserving skewing with Quat I showed above is based on this.


Euler angle to Transform

The way to create a Transform from Euler angles is via a Basis:

var target_transform := Transform(Basis(degrees * PI / 180.0), Vector3.ZERO)

We could preserve scale and translation with this approach:

var target_transform := Transform(
    Basis(degrees * PI / 180.0).scaled(trasnform.basis.get_scale()),
    transform.origin
)

If you want to interpolate translation at the same time, you can set your target position instead of transform.origin.

The current transform is, of course:

var current_transform := transform

We interpolate them like this:

var new_transform = current_transform.interpolate_with(target_transform, LEAN_LERP * delta)

And we can set that:

transform = new_trasnform

If we inline these variables, we have this:

transform = transform.interpolated_with(target_transform, LEAN_LERP * delta)

If you want to preserve skewing, use the Basis approach.


Alternative input to Euler angles

We have found out that interpolating transforms is actually very easy. Is there a way to easily input a Transform? Rhetorical question. We can add some Position3D to the scene. Position and rotate them (and even scale them, even though Position3D has no size), and then use the Transform from them.

We can make the Position3D children of your Spatial (which is somewhat odd, but don't think too hard about it), or as sibling. Regardless, the idea is that we are going to take the transform from these Position3D and use it to interpolate the transform of your Spatial. It is the same code as before:

transform = transform.interpolated_with(position.transform, LEAN_LERP * delta)

In fact, while we are at it, why not have three Position3D:

  • The lean left target.
  • The lean right target.
  • The default target.

Then you pick which target to use depending on input, and interpolate to that:

extends Spatial

const LEAN_LERP = 5

onready var left_target:Position3D = get_node(…)
onready var right_target:Position3D = get_node(…)
onready var default_target:Position3D = get_node(…)


func _process(delta):
    var left := Input.is_action_pressed("LeanLeft")
    var right := Input.is_action_pressed("LeanRight")
    var target := default_target
    if left and not right:
        target = left_target
    
    if right and not left:
        target = right_target

    transform = transform.interpolate_with(target, LEAN_LERP * delta)

Put the node paths where I left ....


Ok, Ok, here is one of the Euler angles versions:

extends Spatial

const LEAN_LERP = 5

export var default_degrees : Vector3
export var leaning_degrees : Vector3


func _process(delta):
    var left := Input.is_action_pressed("LeanLeft")
    var right := Input.is_action_pressed("LeanRight")
    var degrees := default_degrees
    if left and not right:
        degrees = -leaning_degrees
    
    if right and not left:
        degrees = leaning_degrees

    var target_rotation := Basis(degrees * PI / 180.0)
    var current_rotation := Basis(transform.basis.get_rotation_quat())
    var new_rotation := current_rotation.slerp(target_rotation, LEAN_LERP * delta)
    transform.basis = new_rotation * current_rotation.inverse() * transform.basis

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.