Skip to content

Commit

Permalink
feat: add rotateAxisAngle for Quaternion (galacean#480)
Browse files Browse the repository at this point in the history
* feat: add rotateAxisAngle for Quaternion
  • Loading branch information
JujieX authored Sep 7, 2021
1 parent 9d8feae commit b317f3b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
14 changes: 14 additions & 0 deletions packages/math/src/Quaternion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { Vector3 } from "./Vector3";
export class Quaternion implements IClone {
/** @internal */
static readonly _tempVector3 = new Vector3();
/** @internal */
static readonly _tempQuat1 = new Quaternion();

/**
* Determines the sum of two quaternions.
Expand Down Expand Up @@ -697,4 +699,16 @@ export class Quaternion implements IClone {
Quaternion.lerp(this, quat, t, this);
return this;
}

/**
* Calculate this quaternion rotation around an arbitrary axis.
* @param axis - The axis
* @param rad - The rotation angle in radians
* @returns This quaternion
*/
rotateAxisAngle(axis: Vector3, rad: number): Quaternion {
Quaternion._tempQuat1.rotationAxisAngle(axis, rad);
this.multiply(Quaternion._tempQuat1);
return this;
}
}
10 changes: 9 additions & 1 deletion packages/math/tests/Quaternion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ describe("Quaternion test", () => {
expect(Quaternion.equals(out, b)).toEqual(true);
});

it("static rotatAxisAngle", () => {
const a = new Vector3(0, 5, 0);
const b = 0.5 * Math.PI;
const out = new Quaternion(0, 0, 0, 1);
out.rotateAxisAngle(a, b);
expect(Quaternion.equals(out, new Quaternion(0, 0.7071067811865475, 0, 0.7071067811865476))).toEqual(true);
});

it("static scale", () => {
const a = new Quaternion(3, 4, 5, 0);
const out = new Quaternion();
Expand All @@ -200,7 +208,7 @@ describe("Quaternion test", () => {
const b = new Quaternion();
b.setValueByArray([1, 1, 1, 1]);
expect(Quaternion.equals(a, b)).toEqual(true);

const c = [];
b.toArray(c);
const d = new Quaternion();
Expand Down

0 comments on commit b317f3b

Please sign in to comment.