-
Notifications
You must be signed in to change notification settings - Fork 8
Coordinate System
You probably already know what a coordinate system is, but I’ll go over it just in case. First off, let’s look at this picture:
A coordinate system is basically just a grid that represents positions.
Let’s look at the upper right portion of the grid:
In this picture we see a bunch of numbers above and to the right of the grid. These represent the positions of the lines.
In the above picture there is a dot. The dots position is represented as “(0.9 , 0.2)”. Let’s look at another picture that will help us see what the position of the dot is.
We see the red lines intersect at the blue dot. If you follow the lines to the numbers, you will see that they end at certain numbers. The numbers are “0.2” and “0.9”.
A coordinate, or position, is represented like this: ( 0.9 , 0.2 )
The first number, “0.9”, is on what’s called the “X” axis. It tells us the horizontal position of the dot. The second number, “0.2”, is on what’s called the “Y” axis. It represents the vertical position of the dot. So “(0.9 , 0.2)” means that the dot is 0.9 units to the right, and 0.2 units up.
The position numbers don’t mean anything in particular. That is, they don’t represent meters, feet, inches, or anything else unless specified. They are arbitrary.
So the dot in the above picture is located 0.9 units to the right and 0.2 units up.
But what happens when one of the numbers is negative?
The first number of the blue dot’s position is “-0.3”. This means that it’s 0.3 units to the left. In coordinate systems a negative “-” symbol just tells us to go in the opposite direction. More on this later.
So if the second number was negative what direction would the dot go?
That’s right. It would go down.
So what does a coordinate system look like in 3D? Like this:
All we do is just add one extra number, and it represents the new “Z” axis. In Godot the X axis goes east to west, the Z axis goes north and south, and the Y axis goes up and down. But in other programs, like Blender, the Z axis is the one that goes up and down, and the Y axis goes north and south. You’ll get used to it, just keep trukin’.
In most 3D programs the X axis is represented by red, the Y axis is represented by green, and the Z axis is represented by blue. So it would look like this in Godot:
So to reiterate, the Y axis is vertical in Godot. And as a side note the white grid lines represent 1 unit of length. I just think of these units as meters, because it’s easier to work with when I’m trying to make real world measurements of characters, buildings, etc.
Next let’s look at this picture:
In this picture we have a red box. The center of the box, where the dot is, represents the origin of the box. The origin of the box is the position that the box holds.
The position of the box is relative to the origin of the scene. It’s called an offset. It represents the position of the cube in relation to the center of the scene. It’s almost as if you were moving the red box from “(0, 0)” to “(0.4 , 0.7)”. It might look like this (on next page):
In this picture the box position is also called its offset because the box is “offset” from the center of the scene by the amount specified in the parentheses.
This is called global space. It means that the vector that is given is in relation to the center of the whole scene.
The center of the scene in Godot looks like this:
So anything in “global space” is in relation to this origin.
The other kind of “space” is local space. This is a vector that represents a position relative to another object.
Let’s take a look at another example picture:
In this picture, notice how the coordinates of the boxes are still in relation to the origin of the whole scene.
Now let’s look at that picture again, but instead of the origin of the scene being the center of the graph, let’s make the red box the center of the graph:
This is what is considered local space. Whenever you get a vector coordinate in local space, you get a vector that is relative to the object you are asking for it from. So if you ask for the position of the blue box in local space from a script that is attached to the red box, you will get “(0.3 , -0.4)” because the blue box is 0.3 units to the right of the red box and -0.4 units to the south of the red box.
If you want to get the local coordinates of the blue box from the red box’s script, you could do this in GDScript:
var blue_box_pos = get_node("BlueBox").to_local( blue_box_pos.get_transform().origin )
And that will give you the local coordinates of the blue box from the red box.
But let’s say you want to get the local coordinates of the red box from the blue box’s script. What would they look like? It would be the same but in the opposite direction:
red_box_pos = (-0.5 , 1.0)
It’s the exact opposite because you are calling it from the other object now.