CFrame

Mostrar obsoleto

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 Enum.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.

XVector, RightVectorYVector, UpVectorZVector, -LookVector
R00R01R02
R10R11R12
R20R21R22
Unlike the others, LookVector represents the negated column components. The LookVector is useful because many Instances such as the Camera and Attachments treat that vector as the direction the instance is pointing.

Resumen

Constructores

Propiedades

Métodos

Constructores

new

new

Parámetros

pos: Vector3

new

Parámetros

pos: Vector3
lookAt: Vector3

new

Parámetros

new

Parámetros

new

Parámetros

R00: number
R01: number
R02: number
R10: number
R11: number
R12: number
R20: number
R21: number
R22: number

lookAt

Parámetros

lookAt: Vector3
Valor predeterminado: Vector3.yAxis

lookAlong

Parámetros

direction: Vector3
Valor predeterminado: Vector3.yAxis

fromRotationBetweenVectors

Parámetros

from: Vector3

fromEulerAngles

Parámetros

rx: number
ry: number
rz: number
Valor predeterminado: Enum.RotationOrder.XYZ

fromEulerAnglesXYZ

Parámetros

rx: number
ry: number
rz: number

fromEulerAnglesYXZ

Parámetros

rx: number
ry: number
rz: number

Angles

Parámetros

rx: number
ry: number
rz: number

fromOrientation

Parámetros

rx: number
ry: number
rz: number

fromAxisAngle

Parámetros

fromMatrix

Parámetros

Propiedades

identity

An identity CFrame with no translation or rotation. This property is a constant and must be accessed globally as opposed to through an individual CFrame object.

Position

The 3D position of the CFrame.

Rotation

A copy of the CFrame with no translation.

The X coordinate of the position.

The Y coordinate of the position.

The Z coordinate of the position.

LookVector

The forward-direction component of the CFrame object's orientation, equivalent to the negated ZVector or 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 CFrame object's LookVector to itself produces a CFrame moved forward in whichever direction it's facing by 1 unit.

RightVector

The right-direction component of the CFrame object's orientation. Equivalent to XVector or 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

The up-direction component of the CFrame object's orientation. Equivalent to YVector or 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

The X component of the CFrame object's orientation. Equivalent to RightVector or 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

The Y component of the CFrame object's orientation. Equivalent to UpVector or 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

The Z component of the CFrame object's orientation. Equivalent to the negated LookVector or 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)

Métodos

Inverse

Returns the inverse of the CFrame.

Devuelve

Lerp

Returns a CFrame interpolated between itself and goal by the fraction alpha.

Parámetros

goal: CFrame
alpha: number

Devuelve

Orthonormalize

Returns an orthonormalized copy of the CFrame. The BasePart.CFrame property automatically applies orthonormalization, but other APIs which take CFrames do not, so this method is occasionally necessary when incrementally updating a CFrame and using it with them.

Devuelve

ToWorldSpace

Receives one or more CFrame objects and returns them transformed from object to world space. Equivalent to:

CFrame * cf

Parámetros

Devuelve

ToObjectSpace

Receives one or more CFrame objects and returns them transformed from world to object space. Equivalent to:

CFrame:Inverse() * cf

Parámetros

Devuelve

PointToWorldSpace

Receives one or more Vector3 objects and returns them transformed from object to world space. Equivalent to:

CFrame * v3

Parámetros

Devuelve

PointToObjectSpace

Receives one or more Vector3 objects and returns them transformed from world to object space. Equivalent to:

CFrame:Inverse() * v3

Parámetros

Devuelve

VectorToWorldSpace

Receives one or more Vector3 objects and returns them rotated from object to world space. Equivalent to:

(CFrame - CFrame.Position) * v3

Parámetros

Devuelve

VectorToObjectSpace

Receives one or more Vector3 objects and returns them rotated from world to object space. Equivalent to:

(CFrame:Inverse() - CFrame:Inverse().Position) * v3

Parámetros

Devuelve

GetComponents

Returns the values x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, and R22, where x y z represent the position of the CFrame and R00R22 represent its 3×3 rotation matrix.

Devuelve

ToEulerAngles

Returns approximate angles that could be used to generate the CFrame using the optional Enum.RotationOrder. If you don't provide order, the method uses Enum.RotationOrder.XYZ.

Parámetros

Valor predeterminado: Enum.RotationOrder.XYZ

ToEulerAnglesXYZ

Returns approximate angles that could be used to generate the CFrame using Enum.RotationOrder.XYZ.

ToEulerAnglesYXZ

Returns approximate angles that could be used to generate the CFrame using Enum.RotationOrder.YXZ.

ToOrientation

ToAxisAngle

Returns a tuple of a Vector3 and a number which represent the rotation of the CFrame in the axis-angle representation.

components

Equivalent to CFrame:GetComponents().

Devuelve

FuzzyEq

Returns true if the other 'Datatype.CFrame' is sufficiently close to this 'Datatype.CFrame' in both position and rotation. The eps value 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, and for rotation uses a fast approximation of the angle difference.

Parámetros

other: CFrame
epsilon: number
Valor predeterminado: 0.00001 (1e-5)

Devuelve

Operaciones de matemáticas