World vs Local Space. Why do we need them both?
What is a world space? A local space? Why do we need more than one coordinate space? What is a coordinates space to begin with? Keep reading to learn about all these things.
What is a (coordinate) space?
A coordinate space is a system to describe the position of something. In our case, it's also used to specify the orientation (rotation) and scale of objects in our scenes. The most known coordinate system is the Euclidean one, it's the same system that you've learned in school. But there are others like the barycentric and the trilinear, both have applications in computer graphics.
An Euclidean space might have N dimensions. The most common numbers of dimensions being 2 and 3. When coding these systems it's also common to use a fourth dimension, but it's not interpreted visually, it's just a way to make some calculations easier or even possible—if we only work in the real number of dimensions used in our application, translation can't be done using a simple matrix transformation. Matrix transformations are the core in which a lot of things are done in computer graphics and game programming in general.
But the last paragraph was only cited to instigate the curiosity of the more technically inclined audience. You'll only need that knowledge if you're working on a rendering engine or in a more specialized case during game programming, game engines like Unity abstract this out for us.
In Unity, when working with the transform component, we're basically using a coordinate space to move, rotate and scale things in the world. But not only within the world.
Why do we need 2 coordinate spaces? What's the purpose of them?
Isn't the world space sufficient? Why do we need to create another space? Convenience. In reality, yes, everything could be described in world coordinates, but it would make positioning and orientation problems are a lot harder. As humans we really need to think about things in isolation, one thing at a time, that's why we need a local space. An example of a character with a helmet equipped is going to be given in the section about local spaces to better illustrate this point.
Two other really common spaces are the object and screen space. Each one has its uses. The screen space, as its name says, is used when you need to know something that's directly tied to the screen, like a mouse position. Another really common space is the local space, it's used to describe relationships regarding the children of a game object.
Global or World Space
In this context, global and world space are synonyms. They describe in direct terms where each object is positioned and how they are oriented. The end result of local space calculations are numbers in the world space.
What is the local space? Why is it useful?
As the world space has two names, the local space can also be called the object space.
To understand better why we have a local space let's think of a character that has gear equipped, imagine also that this character sits not in the center of the world but at some arbitrary point. Imagine how crazy would it be if we only could describe absolute relationships instead of just saying that a helmet stays on top of the character head.
One of the most useful orientations (as in a direction) to have in mind on game development is the forward orientation. What is forward to the player is not necessarily what's forward for the character they are controlling. The difference lies in the coordinate system used. The world has only one forward vector, but each character has their own local forward vector too.
If you're trying to move a character forward using Vector3.forward and its moving in the wrong direction it's because the Vector3 version of forward is always in relation to the world, if you need to move a character forward in relation to itself what you need to use is Transform.forward.
Converting World to Local (and vice versa) in Unity
Fortunately, this can be achieved using helper functions provided by the engine.
Also see Space.World if you want to apply transformations to an object using the world space.
And that concludes the article. If you're still looking for explanations on this subject please let me know on @juliomarcos, I'll try to answer you the best I can and I'll also update the article.
- coordinate space