The Godot Community Forums are back up and running! Please read the the announcement post for important information on what happened. Thanks!

How do I move a bone to a desired location in 3D?

Hi,

I've been trying to find a way to move a bone to a desired location for ages now. The closest I've come moves the bone in the right direction but clearly not by the right amount.

I've got a bone moving a bow's rope. If I use Godot's editor, I can change the transform's origin of the bone pose and it does what I want. I'm trying to achieve the same effect through code. Basically I want the bone to follow the player's hand (in VR). Ideally I want to affect only the Y part of the transform's origin.

I understand that I can apply a Transform to the bone by using set_bone_pose() but the result is not what I expect. To move the arrow, I simply calculate the distance between the hand and the bow and I translate the arrow accordingly. Can I do something similar with my bone?

Thanks,
Victor

Best Answer

  • UnknownUserUnknownUser Posts: 0
    Accepted Answer

    Thanks for your response @TwistedTwigleg . I actually figured out my mistake.. It was because of the scale I applied to my model.

    For the record:

    #Calculate the distance between my bow and the right hand
    var distance = $bow.global_transform.origin.distance_to($hand.global_transform.origin)
    
    #Create a new transform to apply to the bone's rest pose
    var bone_transform = Transform()
    
    #Update only the y axis of the origin to move the rope along the hand
    #Note the distance is multiplied by 4, because my model is scaled to 0.25
    bone_transform.origin.y = distance*4
    skeleton.set_bone_pose(1, bone_transform)
    

    The only problem was that the distance was not big enough. Indeed my model is scaled to 0.25, so the distance must be multiplied by 4 to be the real distance between the bow and the hand.

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 3,141Admin
    edited September 16

    Welcome to the forums @mreliptik!

    Working with bone transforms can be difficult, because of the different transform spaces available. This should become a little easier in Godot 4, as I've implemented a number of helper functions for taking a Transform and converting it to and from the various transforms in the Skeleton.

    I would recommend looking at the world_transform_to_global_pose function (link) in the WIP pull request and writing a GDScript equivalent. Then, you can take a node in the scene, get its global transform, and then use set_bone_override_pose and pass the converted transform, which should keep it at the node's position in the scene.
    (When the 3D PR is pulled in to Godot 4, you could also use a BoneAttachment node for this purpose, as it can now optionally override bone poses :smile: )

  • UnknownUserUnknownUser Posts: 0
    Accepted Answer

    Thanks for your response @TwistedTwigleg . I actually figured out my mistake.. It was because of the scale I applied to my model.

    For the record:

    #Calculate the distance between my bow and the right hand
    var distance = $bow.global_transform.origin.distance_to($hand.global_transform.origin)
    
    #Create a new transform to apply to the bone's rest pose
    var bone_transform = Transform()
    
    #Update only the y axis of the origin to move the rope along the hand
    #Note the distance is multiplied by 4, because my model is scaled to 0.25
    bone_transform.origin.y = distance*4
    skeleton.set_bone_pose(1, bone_transform)
    

    The only problem was that the distance was not big enough. Indeed my model is scaled to 0.25, so the distance must be multiplied by 4 to be the real distance between the bow and the hand.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file