Skip to content

Commit 0d5a688

Browse files
committed
Fix mult quarternion bug
1 parent 8ef4227 commit 0d5a688

1 file changed

Lines changed: 5 additions & 18 deletions

File tree

src/math/vec.zig

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const std = @import("std");
22

3+
34
const mach = @import("../main.zig");
45
const testing = mach.testing;
56
const math = mach.math;
@@ -142,24 +143,10 @@ pub fn Vec3(comptime Scalar: type) type {
142143
/// Vector * Quat multiplication
143144
/// https://github.com/greggman/wgpu-matrix/blob/main/src/vec3-impl.ts#L718
144145
pub inline fn mulQuat(v: *const VecN, q: *const quat.Quat(Scalar)) VecN {
145-
const qx = q.v.x();
146-
const qy = q.v.y();
147-
const qz = q.v.z();
148-
const w2 = q.v.w() * 2;
149-
150-
const vx = v.x();
151-
const vy = v.y();
152-
const vz = v.z();
153-
154-
const uv_x = qy * vz - qz * vy;
155-
const uv_y = qz * vx - qx * vz;
156-
const uv_z = qx * vy - qy * vx;
157-
158-
return math.vec3(
159-
vx + uv_x * w2 + (qy * uv_z - qz * uv_y) * 2,
160-
vy + uv_y * w2 + (qz * uv_x - qx * uv_z) * 2,
161-
vz + uv_z * w2 + (qz * uv_y - qy * uv_x) * 2,
162-
);
146+
const q_xyz = q.v.swizzle(.x, .y, .z);
147+
const uv = q_xyz.cross(v);
148+
const uuv = q.xyz(&uv);
149+
return v.add(&uv.mulScalar(q.v.w()).add(&uuv).mulScalar(2));
163150
}
164151

165152
pub const add = Shared.add;

0 commit comments

Comments
 (0)