Why is a local rotate changing the global transform?

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

I have setup a simple camera node and trying to rotate it by 90 degrees. As per the documentation:

rotate method “Rotates the local transformation around axis, a unit Vector3, by specified angle in radians.”

This is my code:

func _ready():
    print("Before rotate")
    rotate(Vector3.UP, deg2rad(90))
    print("After rotate")

This is the output:

Before rotate
((1, 0, 0), (0, 1, 0), (0, 0, 1))
((1, 0, 0), (0, 1, 0), (0, 0, 1))
After rotate
((-0, 0, 1), (0, 1, 0), (-1, 0, -0))
((-0, 0, 1), (0, 1, 0), (-1, 0, -0))

I am not sure if I understand the concept of global transforms and local transform clearly because as per the documentation they contain two different transforms.

This is the same behavior with rotate_object_local as well (but it shouldn’t matter because they both work with local transforms.

:bust_in_silhouette: Reply From: Zylann

Because the global transform is not a totally independent transform, it is the combination of the local transform and its parent’s global transforms.

They represent the same “state”, sort of, just in two different reference frames.
transform is in local space, global_transform is in world space.

Can we get a code example or something to understand this more in detail? That would be great!

keshav | 2020-04-29 14:54

In other words, transform is relative to the parent.
global_transform is relative to the world.

Using a simplified analogy, you can see that like in a solar system where the Sun is the center of the world. The Earth is 100 units away from the sun, and the moon is 2 units away from the Earth. That means 2 is the local position of the moon relative to its parent (Earth). Then, 102 would be the global position of the moon (relative to the world’s center, the Sun).

The same happens with transform and global_transform. Depending on what you want to achieve, you might use one or the other if it suits your needs better.

Zylann | 2020-04-29 15:00