#Global rotation true meaning ?

1 messages · Page 1 of 1 (latest)

tiny magnet
#

I was trying to write a SkeletonModifier3D that would rotate a bone on X,Y,Z axis the same way as some target object.

I tried to take the target object global rotation, and convert it using the transform of my bone to my bone "local rotation".
But when printing values, pre and post conversion, rotations values are the same.
This make it so that my bone is not actually rotating in the same direction as my target is.

I hade to rewrite the whole thing to make my bone rotate "as if " it had the same basis as my target.

But this is all very weird to me. I thought global rotation would be this, a way to interpret rotation in a common reference.
But it doesn't seem to and doesn't seem to help to convert a rotation in some basis to another.

So my question is, what is the purpose of global rotation then ?

#

For reference this is my old script :

@tool

class_name BoneMatchTargetRotation
extends SkeletonModifier3D

@export var y_rotation_target_object: Node3D
@export var x_rotation_target_object: Node3D
@export var z_rotation_target_object: Node3D

@export_enum(" ") var bone: String

func _validate_property(property: Dictionary) -> void:
    if property.name == "bone":
        var skeleton: Skeleton3D = get_skeleton()
        if skeleton:
            property.hint = PROPERTY_HINT_ENUM
            property.hint_string = skeleton.get_concatenated_bone_names()

func _process_modification() -> void:
    var bone_index = get_skeleton().find_bone(bone)
    if bone_index != -1:
        var skeleton = get_skeleton()
        var local_bone_transform = skeleton.get_bone_global_pose(bone_index)
        var global_bone_transform = skeleton.global_transform * local_bone_transform
        var new_global_rotation = Vector3.ZERO

        if y_rotation_target_object:
            new_global_rotation.y = y_rotation_target_object.global_rotation.y
        if x_rotation_target_object:
            new_global_rotation.x = x_rotation_target_object.global_rotation.x
        if z_rotation_target_object:
            new_global_rotation.z = z_rotation_target_object.global_rotation.z

        var new_global_basis = Basis.from_euler(new_global_rotation)
        var new_global_transform = Transform3D(new_global_basis, global_bone_transform.origin)
        var new_local_transform = skeleton.global_transform.inverse() * new_global_transform
        skeleton.set_bone_global_pose(bone_index, new_local_transform)