Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rotateAxisAngle for Quaternion #480

Merged
merged 6 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Collaborator

@gz65555 gz65555 Sep 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a /** @internal */ comment.


/**
* 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;
}

/**
* Create this quaternion rotate around an arbitrary axis.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use create in the comment of Instance method.

* @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