Skip to content

Commit

Permalink
fix: replace Mat4.rotateFromQuat() with Mat4.getRotationAndTranslatio…
Browse files Browse the repository at this point in the history
…nFromQuatAndVec()
  • Loading branch information
tchayen committed Feb 4, 2024
1 parent 2d17487 commit 2c11e0e
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/math/Mat4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,33 @@ export class Mat4 {
return this.xRotation(x).multiply(this.yRotation(y)).multiply(this.zRotation(z));
}

static rotateFromQuat(q: Vec4): Mat4 {
const x = q.x;
const y = q.y;
const z = q.z;
const w = q.w;

const x2 = x * x;
const y2 = y * y;
const z2 = z * z;
/**
* @param q rotation quaternion
* @param v translation vector
* @returns a matrix combining rotation and translation
*/
static getRotationAndTranslationFromQuatAndVec(q: Vec4, v: Vec4): Mat4 {
const x2 = q.x + q.x;
const y2 = q.y + q.y;
const z2 = q.z + q.z;

const xy = x * y;
const xz = x * z;
const yz = y * z;
const wx = w * x;
const wy = w * y;
const wz = w * z;
const xx = q.x * x2;
const xy = q.x * y2;
const xz = q.x * z2;
const yy = q.y * y2;
const yz = q.y * z2;
const zz = q.z * z2;
const wx = q.w * x2;
const wy = q.w * y2;
const wz = q.w * z2;

// prettier-ignore
return new Mat4([
1 - 2 * (y2 + z2), 2 * (xy - wz), 2 * (xz + wy), 0,
2 * (xy + wz), 1 - 2 * (x2 + z2), 2 * (yz - wx), 0,
2 * (xz - wy), 2 * (yz + wx), 1 - 2 * (x2 + y2), 0,
0, 0, 0, 1
])
1 - (yy + zz), xy + wz, xz - wy, 0,
xy - wz, 1 - (xx + zz), yz + wx, 0,
xz + wy, yz - wx, 1 - (xx + yy), 0,
v.x, v.y, v.z, 1,
]);
}

/**
Expand Down

0 comments on commit 2c11e0e

Please sign in to comment.