Rotations¶
Luanti provides a proper helper class for working with 3D rotations. Using vectors of Euler angles instead is discouraged as it is error-prone. This class was added in Luanti 5.19.0.
The arithmetic precision of rotation computations may change (improve) in the future. In the current implementation, rotations are internally represented as quaternions of 32-bit floats (less precision than the Lua number type).
Rotations use left-handed conventions. For example a quarter turn (+pi/2) around the Y-axis will map +Z to +X.
Constructors¶
Rotation.identity(): Constructs a no-op rotation.Rotation.quaternion(x, y, z, w): Constructs a rotation from a quaternion (which need not be normalized).Rotation.axis_angle(axis, angle): Constructs a rotation around the given axis by the given angle.axisis a nonzero vector, which need not be normalized.angleis in radians.- Example:
Rotation.axis_angle(vector.new(1, 0, 1), math.pi/2)is a quarter-turn around the bisector between the X and Z axes. - Let
v1,v2be nonzero vectors. Then it holds thatRotation.axis_angle(v1:cross(v2), v1:angle(v2)):apply(v1):normalize()is (approximately)v2:normalize(). A shorthand for this is provided asRotation.maps_to(v1, v2). - There are shorthands for rotations around the cardinal axes:
Rotation.x(pitch)Rotation.y(yaw)Rotation.z(roll)
Rotation.euler_xyz(pitch, yaw, roll)- All angles in radians.
- Uses X-Y-Z rotation order, equivalent to
Rotation.compose(Rotation.z(roll), Rotation.y(yaw), Rotation.x(pitch)). - Consistent with the Euler angles that can be used for bones or attachments.
Rotation.euler_zxy(pitch, yaw, roll)- Same as
euler_xyz, but uses Z-X-Y rotation order. - This is consistent with the Euler angles that can be used for entities.
You can do
Rotation.euler_zxy((-rotation):unpack())to convert an entity rotation vector (note the handedness conversion). Rotation.maps_to(dir_from, dir_to): Construct a rotation that mapsdir_fromtodir_to.dir_fromanddir_toare nonzero direction vectors.- The given rotation only rotates in the plane spanned by the two vectors,
taking a shortest path. If
dir_fromanddir_toare parallel, any plane containing the spanned line may be chosen. Otherwise, the returned rotation is uniquely defined. Rotation.compose(...): Returns the composition of the given rotations.Rotation.compose()is equivalent toRotation.identity().Rotation.compose(rot)copies the rotation.Rotation.compose(...)for at least two rotations composes the given rotations in right-to-left order. This means thatRotation.compose(second, first):apply(v)is equivalent tosecond:apply(first:apply(v)): The composed rotation first appliesfirst, thensecondto a vector.
Conversions¶
Corresponding to the constructors, rotations can be converted to different representations. Note that this conversion is not guaranteed to produce the same values you put in. It is only guaranteed that the values produce an approximately equivalent rotation when passed to the corresponding constructor.
x, y, z, w = rot:to_quaternion()- Returns the normalized quaternion representation.
axis, angle = rot:to_axis_angle()axisis a normalized vector that can point in any direction.angleis the rotation about this axis as an angle in radians.pitch, yaw, roll = rot:to_euler_xyz()- Angles are all in radians.
pitch,yaw,roll: Rotation around the X-, Y-, and Z-axis respectively.- Inverse of
Rotation.euler_xyz. pitch, yaw, roll = rot:to_euler_zxy()- Same as
to_euler_xyz, except uses Z-X-Y rotation order. - To obtain a right-handed rotation in Z-X-Y order used for entities,
you can do
-vector.new(rot:to_euler_zxy()).
Rotations can also be converted to matrices using Matrix4.rotation(rot).
Methods¶
rot:compose(...): Shorthand forRotation.compose(rot, ...).rot:apply(vec): Returns the result of applying the rotation to the given vector.rot:invert(): Returns the inverse rotation.from:slerp(to, time): Returns an interpolated rotation.- Uses shortest path spherical linear interpolation.
time = 0is allfrom,time = 1is allto.rot:angle_to(other): Returns the absolute angle between two rotations.- Useful to measure similarity.
Rotations implement __tostring. The format is only intended for human-readability,
not serialization, and may thus change in the future.