When you're learning about game geometry on Roblox, you might see the phrases object space and world space in Vector3 and CFrame functions. These terms refer to different kinds of coordinate systems, or ways Studio defines an object's position within your experience. This article details how these two terms relate to each other within Roblox development.
Think of world space like yard lines on a football field. The yard lines never move and they act like coordinates: you can use them to find where a player might be standing on the field.
Just as in football, world space is the same in every Roblox game: the position (0, 0, 0) is at the center of the game. The "front" direction is always in the negative Z direction, and the "right" direction is always in the positive X direction. The Position property of parts always refers to their position in world space, also called their absolute position.
When working with positional data in code, it is very useful to first translate positions and directions into world space first. Use it as a basis for solving any geometry problem you come across!
To understand object space, imagine you're a football player. Your team is facing the opponents head on. If you hear your opponent yell "Go left!" That player is certainly referring to a direction relative to their team's object space. To understand the direction that player is referring to, you would have to translate that direction into your team's object space. In other words, their left is your right.
In this sense, when we talk about the "ownership" of a direction ("his left"), we're actually saying "According to him, that direction is called left." From a different perspective, or in a different object space, what he calls left you might call right. However, both are talking about the same direction expressed in different ways.
Position versus Directions
A Vector3 can be used to store positional (3D point) and directional data (3D vector), as each use X, Y and Z coordinates. It is important to recognize which of these a Vector3 represents. Usually, this is indicated by where it comes from:
- Subtracting two Vector3s, A - B, provides a Vector3 result that is in the direction of B towards A.
Functions used to translate coordinate data from object space to world space reside in the CFrame data type. For each of these, the CFrame on which you call the function should represent the CFrame of the object in question.
- Functions ending in "ToObjectSpace" translate data defined in world space and return the same data defined in object space.
- Conversely, functions ending in "ToWorldSpace" translate data defined in object space and return the same data defined in world space.
These functions take a CFrame and return a CFrame.
These functions take a positional Vector3 and return a positional Vector3.
These functions take a directional Vector3 and return a directional Vector3.
In these, we'll continue with the football example: the red team player and blue team player are trying to catch a thrown football. We have the CFrames of each:
1local redPlayerCF = workspace.RedPlayer.HumanoidRootPart.CFrame2local bluePlayerCF = workspace.BluePlayer.HumanoidRootPart.CFrame3local footballCF = workspace.Football.CFrame4
- To find the CFrame of the football relative to the blue player: footballCF:ToObjectSpace(bluePlayerCF)
- To determine if the football is in front of the red player: footballCF:ToObjectSpace(redPlayerCF).Z > 0
- The red player says that the football moved to some CFrame newCF, relative to him. To find out where the ball is relative to the blue player: redPlayerCF:ToWorldSpace(newCF):ToObjectSpace(bluePlayerCF)