PcoWSkbVqDnWTu_dm2ix

Object and World Space

Object and World Space

10 min

When dealing with game geometry, you may come across the phrases object space and world space. On Roblox, you’ll find these terms in Vector3 and CFrame functions. These terms refer to different kinds of coordinate systems, or ways a position is defined. This article will help you understand how these relate in Roblox development.

World Space

A football field with a Roblox character standing on it

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!

Object Space

Two football players facing each other. The blue one in the background has an arrow to the right with the label "His Left", and the red one in the foreground has an arrow to the right with the label "Your right"

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 datatype/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:

  • Vector3.FromNormalId and Vector3.FromAxis return vectors pointing in a given direction or axis.
    The Orientation of a part is a directional vector in world space.
  • CFrame:VectorToWorldSpace and `CFrame:VectorToObjectSpace1 both take and return directional vectors as Vector3s.
  • Subtracting two Vector3s, A - B, provides a Vector3 result that is in the direction of B towards A.

Translation Functions

Functions used to translate coordinate data from object space to world space reside in the datatype/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.
CFrame:ToObjectSpace()
CFrame:ToWorldSpace()
These functions take a CFrame and return a CFrame.
CFrame:PointToObjectSpace()
CFrame:PointToWorldSpace()
These functions take a positional Vector3 and return a positional Vector3.
CFrame:VectorToObjectSpace()
CFrame:VectorToWorldSpace()
These functions take a directional Vector3 and return a directional Vector3.

Examples

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:

local redPlayerCF = workspace.RedPlayer.HumanoidRootPart.CFrame
local bluePlayerCF = workspace.BluePlayer.HumanoidRootPart.CFrame
local footballCF = workspace.Football.CFrame
  • 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)
Tags:
  • position
  • space
  • world
  • object