I'm trying to do something akin to spatial node parenting except programmatically.

Let's say that we have two spatials, A and B, and that I want to use their position at scene initialization to compute a relative transform between them so that I can re-use it to re-apply it later? How would that all work?

I'm familiar with Vector3 addition/subtraction to get what I want, but the issue is that I need to consider rotation and scaling too, hence why I'm trying to understand how to do the same kind of arithmetic but with transforms?

Maybe you can add it as child, then remove it back.

I think the appropriate thing to google here is "godot transformation matrix".

You can use the Basis class to get the rotation, and then multiply them (in Godot 3.x you'd use basis.xform) to apply one rotation to the other.

@Erich_L said: I think the appropriate thing to google here is "godot transformation matrix".

After sitting down and reading more carefully https://docs.godotengine.org/en/3.1/tutorials/math/matrices_and_transforms.html#matrix-tips I finally found it! Thank you!

Here's the code I ended up with:

extends Spatial

export(NodePath) var target_path
onready var target = get_node(target_path)
onready var relative_transform = target.global_transform.affine_inverse() * global_transform


func _process(_delta):
    global_transform = target.global_transform * relative_transform

The script is attached to the object that is supposed to follow target.

10 months later