Question about global and local values in Godot 3.5

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

My question simply revolves around the return values of global_position, global_rotation, global_transform, and its local counterparts.

Here is an image of the return values of a parent Node2D facing 270 degrees to the north, and its child Sprite node offset to where the head points towards the parent’s rotation direction.

Rotation Man

The return values:
This is local rotation:4.71239
This is global rotation:-1.570795
This is global transform:((0.000001, -1), (1, 0.000001), (465, 297))
This is local transform:((0.000001, -1), (1, 0.000001), (465, 297))
This is local position:(465, 297)
This is global position:(465, 297)

I started exploring these values when I was practicing tweening towards global mouse position: Thank you @GDQuest on youtube for the video lessons:

extends Node2D

func _unhandled_input(event):
if event is InputEventMouseButton and event.pressed:

func move_to_mouse():
var tween = create_tween().set_trans(Tween.TRANS_QUINT).set_ease(Tween.EASE_IN_OUT)
tween.tween_property(self, “global_position”, get_global_mouse_position(), 1)

var target_rotation = global_position.direction_to(get_global_mouse_position()).angle()
tween.parallel().tween_property(self, “global_rotation”, target_rotation, 1)

I noticed that in certain clicks, the node wouldn’t rotate towards the shortest path (namely when clicking top left and bottom right of the node) and it looked very awkward. So it made me wonder if it had anything to do with global vs local values.

But after testing between the two, not only were the results of the tweening the same, but the return values shown above were identical. Lots of confusion .

I feel like I’m missing something very simple. I understand that globally the rotation goes in the negative direction past PI radians, and locally it wraps continually depending on the rotation you set, but if the results are the same, are there instances where the choice is critical?

I’m very new to coding and game developing so any and all insight into the above return values as well as the awkward rotations is deeply appreciated .

I also just want to give a quick shoutout to @kidscancode @heartbeast @godottutorials @gdquest for their content that changed my life and the lives of others. Thank you.

I can’t really answer in depth to this, but tweening rotation is generally problematic as it is a floating value written into a circles. Vectors are better for this, so try to rotate objects using Transform and tween Transform instead. There is an underhood functionality in Godot ( quaternions ), that makes it easier for engine to orientate in vector space

Inces | 2022-09-09 13:56

Thank you! I will look into this and research more. Would you happen to know why the return values above are identical for the local and global values?

devshin | 2022-09-09 15:02

To be honest I didn’t entirely understand the problem of this post :stuck_out_tongue:
You wonder why local and global position of the Sprite are the same ?
local position is a position in regards to parent node. I assume Node2d has a global_position of 0,0. Only in this case such a situation is possible. If You change global position of Node 2D to (0,10), than Sprite will have local 465, 297 and global 465, 307

Inces | 2022-09-09 17:46

Sorry for the confusion, I actually just attached a child sprite to the parent Node2D both at (0,0), then moved both together to the center. I then added a script to the Node2D and printed out their global and local values when Node2D is rotated 270 degrees using the inspector. And then the values shown above were printed out and confused me.

In my head, I always understood that locally (1,0) was always 0 degrees, no matter where it’s facing in world space. So I guess those numbers stuck to me and then seeing 4.71239 radians I thought, why is it the same direction as it’s global_rotation -1.570795.

But perhaps this becomes more applicable when I need to rotate a child versus its parent like you mentioned above. I guess I just need to keep running through more tutorials to understand more deeply,

devshin | 2022-09-09 22:30

Yeah, all local means in comparison to parents.
Also, make sure You really moved both objects, not just a Sprite. Dragging objects in editor may leave parents behind, if You don’t flag locking function.

Inces | 2022-09-11 18:29