Explain please what is to_local()

Godot Version

4.3

Question

Hello. Can someone explain what Node2D.to_local() and how it work, may be with a picture.

For example,
I have Node2D with position (global and local) Vector2(0,0)
I have some point
var point: Vector2 = Vector2(3.5, 30)
So,
node.to_local(point) returns (28.50028, -9.999864)
My naive representation says it must be point - node.global_position, but it not.
Yes, I’ve seen what inside to_local():

return get_global_transform().affine_inverse().xform(p_global);

But do not understand why it looks like node have offset.

Local coordinates are relative to the origin of some object, and global coordinates are relative to the world’s origin.

Node.to_local() will convert global coordinates to coordinates that are relative to the Node.

1 Like

Did more tests and now understand, it affected by rotation

    var n: Node2D = Node2D.new()
    var p: Vector2 = Vector2(3.0, 5.0)    
    var p_local: Vector2 = n.to_local(p)
    n.rotate(0.5)
    var p_local_rotated: Vector2 = n.to_local(p)
    var p_calc = (p - n.global_position).rotated(-n.rotation)    
    print("p_local = ", p_local) # p_local = (3, 5), same as p
    print("p_local_rotated = ", p_local_rotated)   # p_local_rotated = (5.029875, 2.949636)     
    print("p_calc = ", p_calc) # p_calc = (5.029875, 2.949636)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.