Godot transform.xform_inv vs to_local

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

Hi,
Last day I was making a joystick handle that would follow the mouse when clicked. To get the local position of the mouse position relative to the joystick background node(which I had to for my algorithm to clamp the max distance the handle could go) I used background.transform.xform_inv, but, unfortunately, it didn’t work. So i tried using background.to_local which,surprisingly, worked. Now my question is: Isn’t node.transform.xform_inv supposed to do the same as Node.to_local? Or am I missing something?
Btw the Node architecture i was using was the following: Player(Node2D) ->TouchControls(Canvas Layer) → Joystick(Area2D) → Background(TouchScreenButton) → Handle(Sprite)
Thanks in advance for any help in clarifying this question,

:bust_in_silhouette: Reply From: Lopy

When you use a self.transform.xform, you go from a Vector local to get_parent() into a Vector local to self. When you request the cursor position however, you get a global position, so you use to_local() instead. It will do xforms starting from the first Node2D grandparent down to you node.

But even if I use the global transform it doesnt work with xform…

MikeSundaysGameDev | 2021-01-23 20:14