Data type
CFrame
A data type that represents both a 3D position and orientation.
About CFrame
The CFrame data type, short for coordinate frame, describes a
3D position and orientation. It is made up of a positional component and a
rotational component and includes essential arithmetic operations for
working with 3D data on Roblox.
-- Create a CFrame at a certain position and Euler rotation
local cf = CFrame.new(0, 5, 0) * CFrame.fromEulerAngles(math.rad(45), 0, 0)
For an introduction to the CFrame data type, see
CFrames.
Positional Component
The positional component is available as a Vector3. In addition,
the components of a CFrame object's position are also available in
the X, Y and Z
properties like a Vector3.
Rotational Component
CFrame stores 3D rotation data in a 3×3 rotation matrix.
These values are returned by the CFrame.GetComponents function
after the x, y and z positional values. This matrix is used internally
when doing calculations involving rotations, using radians as their unit
(for conversion from one to the other, use math.rad or
math.deg). For more information on how the Roblox Engine performs
rotations, see RotationOrder.
The table below represents the components of a CFrame object's
rotation matrix and their relationship with the available vector properties
such as LookVector and
RightVector. Although the individual components
of the rotation matrix are rarely useful by themselves, the vector properties
which derive from them are much more useful.
Constructors 16
CFrame.new
()Returns a blank identityCFrame.CFrame.new
(x: number, y: number, z: number)Returns aCFramewith a position comprised of the providedx,y, andzcomponents.CFrame.new
(x: number, y: number, z: number, qX: number, qY: number, qZ: number, qW: number)Returns aCFramefrom position (x,y,z) and quaternion (qX,qY,qZ,qW).CFrame.new
(x: number, y: number, z: number, R00: number, R01: number, R02: number, R10: number, R11: number, R12: number, R20: number, R21: number, R22: number)Returns aCFramefrom position (x,y,z) with an orientation specified by the rotation matrix.CFrame.fromEulerAngles
(rx: number, ry: number, rz: number, order: RotationOrder = Enum.RotationOrder.XYZ)Returns a rotatedCFramefrom anglesrx,ry, andrzin radians. Rotations are applied in the optionalRotationOrderwith a default ofXYZ.CFrame.fromEulerAnglesXYZ
(rx: number, ry: number, rz: number)Returns a rotatedCFramefrom anglesrx,ry, andrzin radians usingRotationOrder.XYZ.CFrame.fromEulerAnglesYXZ
(rx: number, ry: number, rz: number)Returns a rotatedCFramefrom anglesrx,ry, andrzin radians usingRotationOrder.YXZ.CFrame.Angles
(rx: number, ry: number, rz: number)Equivalent tofromEulerAnglesXYZ().CFrame.fromOrientation
(rx: number, ry: number, rz: number)Equivalent tofromEulerAnglesYXZ().CFrame.fromMatrix
(pos: Vector3, vX: Vector3, vY: Vector3, vZ: Vector3)Returns aCFramefrom a translation and the columns of a rotation matrix.
Properties 12
X
: numberThe X coordinate of the position.The X component of the
CFrameobject's world-space position. Equivalent toCFrame.Position.X.Y
: numberThe Y coordinate of the position.The Y component of the
CFrameobject's world-space position. Equivalent toCFrame.Position.Y.Z
: numberThe Z coordinate of the position.The Z component of the
CFrameobject's world-space position. Equivalent toCFrame.Position.Z.LookVector
: Vector3The forward-direction component of theCFrameobject's orientation, equivalent to the negated form ofZVector.The forward-direction component of the
CFrameobject's orientation, equivalent to the negatedZVectoror the negated third column of the rotation matrix.local cf = CFrame.new(0, 0, 0) local x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = cf:GetComponents() print(cf.LookVector) --> (-0, -0, -1) print(-cf.ZVector) --> (-0, -0, -1) print(-R02, -R12, -R22) --> (-0 -0 -1)Adding a
CFrameobject'sLookVectorto itself produces aCFramemoved forward in whichever direction it's facing by 1 unit.RightVector
: Vector3The right-direction component of theCFrameobject's orientation.The right-direction component of the
CFrameobject's orientation. Equivalent toXVectoror the first column of the rotation matrix.local cf = CFrame.new(0, 0, 0) local x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = cf:GetComponents() print(cf.RightVector) --> (1, 0, 0) print(cf.XVector) --> (1, 0, 0) print(R00, R10, R20) --> (1 0 0)UpVector
: Vector3The up-direction component of theCFrameobject's orientation.The up-direction component of the
CFrameobject's orientation. Equivalent toYVectoror the second column of the rotation matrix.local cf = CFrame.new(0, 0, 0) local x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = cf:GetComponents() print(cf.UpVector) --> (0, 1, 0) print(cf.YVector) --> (0, 1, 0) print(R01, R11, R21) --> (0 1 0)XVector
: Vector3Equivalent toRightVector.The X component of the
CFrameobject's orientation. Equivalent toRightVectoror the first column of the rotation matrix.local cf = CFrame.new(0, 0, 0) local x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = cf:GetComponents() print(cf.XVector) --> (1, 0, 0) print(cf.RightVector) --> (1, 0, 0) print(R00, R10, R20) --> (1 0 0)YVector
: Vector3Equivalent toUpVector.The Y component of the
CFrameobject's orientation. Equivalent toUpVectoror the second column of the rotation matrix.local cf = CFrame.new(0, 0, 0) local x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = cf:GetComponents() print(cf.YVector) --> (0, 1, 0) print(cf.UpVector) --> (0, 1, 0) print(R01, R11, R21) --> (0 1 0)ZVector
: Vector3The Z component of theCFrameobject's orientation. Equivalent to the third column of the rotation matrix.The Z component of the
CFrameobject's orientation. Equivalent to the negatedLookVectoror the third column of the rotation matrix.local cf = CFrame.new(0, 0, 0) local x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = cf:GetComponents() print(cf.ZVector) --> (0, 0, 1) print(-cf.LookVector) --> (0, 0, 1) print(R02, R12, R22) --> (0 0 1)
Methods 18
Inverse
() → CFrameReturns the inverse of theCFrame.Lerp
(goal: CFrame, alpha: number) → CFrameReturns aCFrameinterpolated between itself andgoalby the fractionalpha.Returns a
CFrameinterpolated between itself andgoalby the fractionalpha. The positional component is interpolated linearly while the rotational component is interpolated using spherical linear interpolation (slerp), producing a constant-speed rotation along the shortest arc. Analphaof 0 returns the originalCFrameand analphaof 1 returnsgoal.Orthonormalize
() → CFrameReturns an orthonormalized copy of theCFrame.Returns an orthonormalized copy of the
CFrame. TheBasePart.CFrameproperty automatically applies orthonormalization, but other APIs which takeCFramesdo not, so this method is occasionally necessary when incrementally updating aCFrameand using it with them.ToWorldSpace
(...: Tuple<CFrame>) → Tuple<CFrame>Receives one or moreCFrameobjects and returns them transformed from object to world space.Receives one or more
CFrameobjects and returns them transformed from object to world space. Equivalent to:CFrame * cflocal cf = CFrame.new(5, 10, 0) local offset = CFrame.new(0, 2, 0) -- Output world space of offset CFrame relative to calling CFrame print(cf:ToWorldSpace(offset)) --> 5, 12, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1PointToWorldSpace
(...: Tuple<Vector3>) → Tuple<Vector3>Receives one or moreVector3objects and returns them transformed from object to world space.Receives one or more
Vector3objects and returns them transformed from object to world space. Equivalent to:CFrame * v3local cf = CFrame.new(5, 10, 0) -- Output core world space of the CFrame print(cf:PointToWorldSpace()) --> 5, 10, 0 -- Output world space of a Vector3 (0, 4, 0) relative to the CFrame (object space) print(cf:PointToWorldSpace(Vector3.new(0, 4, 0))) --> 5, 14, 0VectorToObjectSpace
(...: Tuple<Vector3>) → Tuple<Vector3>Receives one or moreVector3objects and returns them rotated from world to object space.Receives one or more
Vector3objects and returns them rotated from world to object space. Equivalent to:(CFrame:Inverse() - CFrame:Inverse().Position) * v3GetComponents
() → TupleReturns the valuesx,y,z,R00,R01,R02,R10,R11,R12,R20,R21, andR22, wherexyzrepresent the position of theCFrameandR00‑R22represent its 3×3 rotation matrix.Returns the 12 numeric components of the
CFrameas a tuple:x,y,z(position), followed byR00,R01,R02,R10,R11,R12,R20,R21,R22(the 3×3 rotation matrix in row-major order). This is the reverse of the 12-argumentCFrame.newconstructor.local cf = CFrame.new(1, 2, 3) * CFrame.fromEulerAnglesXYZ(0, math.rad(90), 0) local x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = cf:GetComponents()ToEulerAngles
(order: RotationOrder = Enum.RotationOrder.XYZ) → number, number, numberReturns approximate angles that could be used to generate theCFrameusing the optionalRotationOrder.Returns approximate angles that could be used to generate the
CFrameusing the optionalRotationOrder. If you don't provideorder, the method usesRotationOrder.XYZ.ToEulerAnglesXYZ
() → number, number, numberReturns the approximate angles (in radians) that could be used to generate theCFrameusingRotationOrder.XYZ.Returns the approximate angles, in radians, that could be used to generate the
CFrameusingRotationOrder.XYZ. The three returned values represent rotations around the X, Y, and Z axes respectively. Due to the nature of Euler angle decomposition, the angles are approximate and subject to gimbal lock near pitch angles of ±90 degrees.ToEulerAnglesYXZ
() → number, number, numberReturns the approximate angles (in radians) that could be used to generate theCFrameusingRotationOrder.YXZ.Returns the approximate angles, in radians, that could be used to generate the
CFrameusingRotationOrder.YXZ. The three returned values represent rotations around the X, Y, and Z axes respectively. Due to the nature of Euler angle decomposition, the angles are approximate and subject to gimbal lock near pitch angles of ±90 degrees.ToOrientation
() → number, number, numberReturns the approximate angles (in radians) that could be used to generate the CFrame using the YXZ rotation order.Returns the approximate angles, in radians, that could be used to generate the
CFrameusingRotationOrder.YXZ. Equivalent toCFrame.ToEulerAnglesYXZ.ToAxisAngle
() → Vector3, numberReturns a tuple containing a unitVector3axis and a rotation angle in radians.Returns a unit
Vector3representing the axis of rotation and a number representing the angle of rotation in radians. A positive angle represents counter-clockwise rotation when looking from the tip of the axis vector toward the origin (right-hand rule). This is the inverse ofCFrame.fromAxisAngle.components
() → TupleEquivalent toCFrame.GetComponents.FuzzyEq
(other: CFrame, epsilon: number = 0.00001 (1e-5)) → boolReturnstrueif the otherCFrameis sufficiently close to thisCFramein both position and rotation.Returns
trueif the otherCFrameis sufficiently close to thisCFramein both position and rotation. Theepsilonvalue is used to control the tolerance for this similarity; this value is optional and should be a small positive value if provided. The similarity for position is component-wise while rotation uses a fast approximation of the angle difference.AngleBetween
(other: CFrame) → numberReturns the angle, in radians, between the orientation of oneCFrameand another.
Math operations
- CFrame * CFrame → CFrame
- CFrame * Vector3 → Vector3
- CFrame + Vector3 → CFrame
- CFrame - Vector3 → CFrame
Reference text from the Roblox Creator Documentation, licensed CC BY 4.0, reorganized by rbxtoolshub.com · Studio 0.743.0.7431133.