Get global position of point (2D)

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

I would like to be able to write a function in a script for an object that would take a Vector2() point in local co-ordinates, and convert it to global. It should take into account the transform of the object (loc rot scale)

Example: If the object was at (3,3), rotated 45 degrees clockwise, and not scaled, an input of (0,-1) would return (3+cos(45), 3-sin(45)).

I’m not sure if understand, is like adding the vector to get_global_pos of a Node2D?

eons | 2017-01-03 01:25

Nearly, but I want to account for rotation of the object too. I probably should have done an example WITH rotation…

keke | 2017-01-03 01:29

Something like:

 get_global_pos()+my_vector.rotated(get_rot())

eons | 2017-01-03 02:07

Is there a way to account for scale?

keke | 2017-01-03 02:55

sorry for being unclear

keke | 2017-01-03 02:56

:bust_in_silhouette: Reply From: avencherus

Every canvas object will have a method get_global_transform() that returns a global transformation matrix32. Inside that matrix exists methods for transforming or inverting the transformation of a given point. xform() and xform_inv()

http://docs.godotengine.org/en/stable/classes/class_matrix32.html?class-matrix32-xform#class-matrix32

One thing to watch out for is if the item itself is transformed or acquires an offset. These can affect things, depending on what you’re trying to do.

For simple transformations I was trying to avoid Transform but since the OP want many characteristics to be applied, this could be the best way.

eons | 2017-01-03 14:46

I see! I tried to use these exact functions before, but couldn’t without your explanation because the documentation was so sparse… Thanks avencherus, and sorry for being unclear initially eons.

keke | 2017-01-03 21:06

You’re welcome. I do agree, they’re not very well documented.

avencherus | 2017-01-04 01:07