Applying a translation in global space to an object in a hierachy ( global to local manipulations )

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By frankiezafe

The problem is quite simple: i wanted to translate an object parented to another along the world axis. the translation vector must be transformed in parent space.

see video: https://peertube.mastodon.host/videos/watch/514e26ea-e7f4-43e1-b642-b00d464fe5dd

Here is how to do it:

tool

extends Spatial

var init = null
func _ready():
    pass

func _process(delta):

    if init == null:
        init = $tester.transform.origin

    var diff = $tester.transform.origin - init

    # copying the full transform to p1
    $cub.transform = $p0.transform * $p0/p1.transform

    # extraction of the rotation matrix of p0
    var q = Quat()
    q.set_euler( $p0.transform.basis.get_euler() )
    var t = Transform(q)

    # creation of a temporary matrix containing 
    # the translation to apply
    var t2 = Transform()
    t2.origin = diff

    # important part: transformation of global matrix to 
    # local matrix of p0
    t2 = t2 * t.inverse()

    # we can now add the translation to the cube in p0 space
    $cub.translation += t2.origin

I don’t know much about this, but I’ll try taking a crack at it.

Have you looked into the to_local() and to_global() functions of the Spatial class?

Ertain | 2019-06-09 23:44

Nope… thanks for the tip :slight_smile:
I needed to understand the way to use Transforms to reorient direction (i’m currently working on skeletons and bone poses)

frankiezafe | 2019-06-10 09:33