Difference between .xform() and .basis_xform

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By molamar
:warning: Old Version Published before Godot 3 was released.

Hi there,

digging into Godot (and game math in general) i read the docs Miscellaneous/Math/Matrices and transforms and what the manual called a " designer coordinate system " and the Orientated coordinate system (OCS), what Matrix32 means and so on.

Reffered to Godots way to instancing whole scenes as nodes in another scene i would like to do following for learning/understanding purpose what translation/transformation means, and youre able to do with it:

  1. A main scene in 2d with just a node2d where i want to instance another scene as (child)node.
  2. 2nd Scene contains just a node2d with a sprite node as child( guess the node2d is not necessary, but it stays at 0,0 so it should not affect it in anyway).

Now the point i try to figure out:

Regardless of the Sprite’s position from my 2nd scene, i want to have the position at 0,0 in my main scene.
So i thought matrix32.xform would do this, but it seems to be matrix32.basis_xform

code:

var tran = get_node("Node2D/Sprite").get_transform()
get_node("Node2D/Sprite").set_pos(tran.basis_xform(get_pos())) 

works fine.

Ok, but my question is why? From my understanding Basis from a Matrix32 is just for Rotation and Scaling and the Origin is for the offset.
Even the doc says: basis_xform() is only for basis(no translation).

So i guess i missunderstand the meaning of Basis and Origin or maybe what translation really means.

Thanks for any help

I indented your code with 4 spaces so the Q&A website will format it as code, don’t forget it next time :slight_smile:

On which node is the code you wrote? I see you use get_pos(), but the output of that depend on where you are calling this from. I’ll assume the code is on the root node of the main scene, and that “Node2D” is the sub-scene you added as child of it:

- Root (Node2D)
    - Node2D (Node2D)
        - Sprite (Sprite)

In that configuration, then it looks weird that you use basix_xform (or anything) on get_pos(), because get_pos will most likely be zero if it’s the root of the world. Of maybe I’m missing something?

So… you want the Sprite to be seen at the origin of the world, regardless of its parent. So why not use a simple Node as its parent, instead of a Node2D? This way you won’t have to compute anything.

If you really want the parent of the Sprite to be a Node2D (for any reason whatsoever), then you indeed would need a transformation from script. What you are looking for is a transformation that reverts the parent’s transform:

# Makes the sprite unaffected by its parent Node2D
var sprite = get_node("Sprite")
var parent = self
sprite.set_transform(parent.get_transform().affine_inverse())

Zylann | 2016-11-14 02:31