Quaternion multiplication

This commit is contained in:
Alex Selimov 2026-07-26 22:12:48 -04:00
parent 861f638ea6
commit 01a057939e

View file

@ -111,6 +111,15 @@ pub fn Quat(comptime T: type) type {
};
}
pub fn quatMul(comptime T: type, a: Quat(T), b: Quat(T)) Quat(T) {
return .{
.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z,
.x = a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
.y = a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
.z = a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w,
};
}
test "vec3Add adds properly" {
const a: Vec3(i8) = .{ .x = 1, .y = 2, .z = 3 };
const b: Vec3(i8) = .{ .x = 3, .y = 1, .z = 0 };
@ -257,3 +266,15 @@ test "quat normalized" {
try std.testing.expect(std.math.approxEqAbs(f32, normalized.z, expected.z, 1e-7));
try std.testing.expect(std.math.approxEqAbs(f32, normalized.w, expected.w, 1e-7));
}
test "quat mul" {
const a: Quat(i8) = .{ .w = 1, .x = 2, .y = 3, .z = 4 };
const b: Quat(i8) = .{ .w = 1, .x = 0, .y = 1, .z = 0 };
const c = quatMul(i8, a, b);
try std.testing.expect(c.w == -2);
try std.testing.expect(c.x == -2);
try std.testing.expect(c.y == 4);
try std.testing.expect(c.z == 6);
}