Skip to content

pymath.rotate_by_quat

Daniel Flassig edited this page Jun 24, 2026 · 2 revisions

Rotates a 3-vector with the rotation being represented as a quaternion.

w = pymath.rotate_by_quat(v, q [, center])
Parameter Type Description
v vector The 3-vector to rotate.
q quaternion The rotation quaternion {x, y, z, w}.
center vector (optional) Center of rotation. When given, the result is center + rot · (v - center). When omitted, the rotation is about the origin.

Return value

Type Description
w A new 3-vector, v rotated by q (about center if given).

Notes:

See pymath for the quaternion and vector representations.

If many points need to be rotated, it's more performant to build the rotation matrix pymath.matrix_from_quat and use matrix multiplication pymath.dot.

  • This is the active rotation of the vector, q * v * q⁻¹: the vector moves while the coordinate frame stays fixed. To rotate the frame instead (passive rotation), pass pymath.invert_quat(q).
  • For a unit quaternion this preserves the distance of v from the center.

Example:

-- 90° rotation about the z-axis maps the x-axis onto the y-axis
local q = pymath.quat_from_rotation({0, 0, 1}, 90)
local w = pymath.rotate_by_quat({1, 0, 0}, q)
-- w == {0, 1, 0}

-- the same rotation about the center {1, 1, 0}
local w2 = pymath.rotate_by_quat({1, 0, 0}, q, {1, 1, 0})
-- w2 == {2, 1, 0}

Version Support:

Minimum PYTHA Version: V27

See also:

pymath, pymath.matrix_from_quat, pymath.invert_quat, pymath.dot

Clone this wiki locally