Quaternion
Represents a Quaternion. Quaternions are 4D structs that represent rotations in 3D space. Quaternions are often used in 3D graphics to avoid the ambiguity that comes with Euler angles (e.g. gimbal lock).
Roblox represents the transformation of an object via CFrame values, which are
matrices that hold positional and rotational information. Quaternions can be converted
to and from CFrame values through the ToCFrame
method and cframe
constructor.
Types
Quaternion
interface
Quaternion {
X:
number
Y:
number
Z:
number
W:
number
}
Similar to Vector3s, Quaternions are immutable. You cannot manually set the individual properties of a Quaternion. Instead, a new Quaternion must first be constructed.
Properties
identity
This item is read only and cannot be modified. Read OnlyQuaternion.identity:
Quaternion
Identity Quaternion. Equal to Quaternion.new(0, 0, 0, 1)
.
Functions
new
Constructs a Quaternion.
caution
The new
constructor assumes the given arguments represent a proper Quaternion. This
constructor should only be used if you really know what you're doing.
euler
Constructs a Quaternion from Euler angles (radians).
-- Quaternion rotated 45 degrees on the Y axis:
local quat = Quaternion.euler(0, math.rad(45), 0)
axisAngle
Constructs a Quaternion representing a rotation of angle
radians around axis
.
-- Quaternion rotated 45 degrees on the Y axis:
local quat = Quaternion.axisAngle(Vector3.yAxis, math.rad(45))
lookRotation
Constructs a Quaternion representing a rotation facing forward
direction, where
upwards
represents the upwards direction (this defaults to Vector3.yAxis
).
-- Create a quaternion facing the same direction as the camera:
local camCf = workspace.CurrentCamera.CFrame
local quat = Quaternion.lookRotation(camCf.LookVector, camCf.UpVector)
cframe
Constructs a Quaternion from the rotation components of the given cframe
.
This method ortho-normalizes the CFrame value, so there is no need to do this yourself before calling the function.
-- Create a Quaternion representing the rotational CFrame of a part:
local quat = Quaternion.cframe(somePart.CFrame)
__mul
Multiplication metamethod. A Quaternion can be multiplied with another Quaternion or a Vector3.
local quat = quatA * quatB
local vec = quatA * vecA
Dot
Calculates the dot product between the two Quaternions.
local dot = quatA:Dot(quatB)
Slerp
Calculates a spherical interpolation between the two Quaternions. Parameter t
represents
the percentage between the two rotations, from a range of [0, 1]
.
Spherical interpolation is great for smoothing or animating between quaternions.
local midWay = quatA:Slerp(quatB, 0.5)
Angle
Calculates the angle (radians) between the two Quaternions.
local angle = quatA:Angle(quatB)
RotateTowards
Constructs a new Quaternion that rotates from this Quaternion to the other
quaternion, with a maximum
rotation of maxRadiansDelta
. Internally, this calls Slerp
, but limits the movement to maxRadiansDelta
.
-- Rotate from quatA to quatB, but only by 10 degrees:
local q = quatA:RotateTowards(quatB, math.rad(10))
ToCFrame
Constructs a CFrame value representing the Quaternion. An optional position
Vector can be given to
represent the position of the CFrame in 3D space. This defaults to Vector3.zero
.
-- Construct a CFrame from the quaternion, where the position will be at the origin point:
local cf = quat:ToCFrame()
-- Construct a CFrame with a given position:
local cf = quat:ToCFrame(someVector3)
-- e.g., set a part's CFrame:
local part = workspace.Part
local quat = Quaternion.axisAngle(Vector3.yAxis, math.rad(45))
local cframe = quat:ToCFrame(part.Position) -- Construct CFrame with a positional component
part.CFrame = cframe
ToEulerAngles
Calculates the Euler angles (radians) that represent the Quaternion.
local euler = quat:ToEulerAngles()
print(euler.X, euler.Y, euler.Z)
ToAxisAngle
Calculates the axis and angle representing the Quaternion.
local axis, angle = quat:ToAxisAngle()
Inverse
Returns the inverse of the Quaternion.
local quatInverse = quat:Inverse()
Conjugate
Returns the conjugate of the Quaternion. This is equal to Quaternion.new(-X, -Y, -Z, W)
.
local quatConjugate = quat:Conjugate()
Normalize
Returns the normalized representation of the Quaternion.
local quatNormalized = quat:Normalize()
Magnitude
Quaternion:
Magnitude
(
) →
number
Calculates the magnitude of the Quaternion.
local magnitude = quat:Magnitude()
SqrMagnitude
Quaternion:
SqrMagnitude
(
) →
number
Calculates the square magnitude of the Quaternion.
local squareMagnitude = quat:Magnitude()