Quaternion and angle helpers, updated example

This commit is contained in:
Szymon Nowakowski
2023-08-16 21:42:38 +02:00
committed by Szymon Nowakowski
parent b42f8d62e4
commit 5d3a7de0c2
4 changed files with 69 additions and 11 deletions

View File

@@ -41,6 +41,27 @@ export class Quaternion {
return new Quaternion(0, 0, 0, 1);
}
static fromRotationXY(angleRad: number): Quaternion {
const halfAngleRad = 0.5 * angleRad;
const c = Math.cos(halfAngleRad);
const s = Math.sin(halfAngleRad);
return new Quaternion(0, 0, s, c);
}
static fromRotationYZ(angleRad: number): Quaternion {
const halfAngleRad = 0.5 * angleRad;
const c = Math.cos(halfAngleRad);
const s = Math.sin(halfAngleRad);
return new Quaternion(s, 0, 0, c);
}
static fromRotationZX(angleRad: number): Quaternion {
const halfAngleRad = 0.5 * angleRad;
const c = Math.cos(halfAngleRad);
const s = Math.sin(halfAngleRad);
return new Quaternion(0, s, 0, c);
}
setObject(object: QuaternionObject): Quaternion {
this.x = object.x;
this.y = object.y;
@@ -64,6 +85,33 @@ export class Quaternion {
this.w = 1;
return this;
}
setRotationXY(angleRad: number): Quaternion {
const halfAngleRad = 0.5 * angleRad;
this.x = 0;
this.y = 0;
this.z = Math.sin(halfAngleRad);
this.w = Math.cos(halfAngleRad);
return this;
}
setRotationYZ(angleRad: number): Quaternion {
const halfAngleRad = 0.5 * angleRad;
this.x = Math.sin(halfAngleRad);
this.y = 0;
this.z = 0;
this.w = Math.cos(halfAngleRad);
return this;
}
setRotationZX(angleRad: number): Quaternion {
const halfAngleRad = 0.5 * angleRad;
this.x = 0;
this.y = Math.sin(halfAngleRad);
this.z = 0;
this.w = Math.cos(halfAngleRad);
return this;
}
}
Object.defineProperty(Quaternion.prototype, "type", { value: "Quaternion" });

7
src/geometry.ts Normal file
View File

@@ -0,0 +1,7 @@
export function degToRad(angleDeg: number): number {
return angleDeg * Math.PI / 180;
}
export function radToDeg(angleRad: number): number {
return angleRad * 180 / Math.PI;
}

View File

@@ -5,6 +5,7 @@
*/
export * from "./_BinaryWriter";
export * from "./geometry";
export * from "./shader";
import { _BinaryWriter as BinaryWriter } from "./_BinaryWriter";