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.
  • axis is a nonzero vector, which need not be normalized.
  • angle is 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, v2 be nonzero vectors. Then it holds that Rotation.axis_angle(v1:cross(v2), v1:angle(v2)):apply(v1):normalize() is (approximately) v2:normalize(). A shorthand for this is provided as Rotation.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 maps dir_from to dir_to.
  • dir_from and dir_to are nonzero direction vectors.
  • The given rotation only rotates in the plane spanned by the two vectors, taking a shortest path. If dir_from and dir_to are 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 to Rotation.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 that Rotation.compose(second, first):apply(v) is equivalent to second:apply(first:apply(v)): The composed rotation first applies first, then second to 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()
  • axis is a normalized vector that can point in any direction.
  • angle is 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 for Rotation.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 = 0 is all from, time = 1 is all to.
  • 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.