MatrixRotate function

Matrix4 MatrixRotate(
  1. Vector3 axis,
  2. double angle
)

Rotation matrix from axis and angle (radians) — Rodrigues' formula.

Implementation

Matrix4 MatrixRotate(Vector3 axis, double angle) {
  final a = axis.normalized();
  final c = math.cos(angle), s = math.sin(angle), t = 1 - c;
  // Matrix4 constructor is column-major: [col0 top-to-bottom, col1, col2, col3]
  return Matrix4(
    t * a.x * a.x + c,
    t * a.x * a.y + s * a.z,
    t * a.x * a.z - s * a.y,
    0,
    t * a.x * a.y - s * a.z,
    t * a.y * a.y + c,
    t * a.y * a.z + s * a.x,
    0,
    t * a.x * a.z + s * a.y,
    t * a.y * a.z - s * a.x,
    t * a.z * a.z + c,
    0,
    0,
    0,
    0,
    1,
  );
}