From f566459afbbfc74117c51a99880bdc5e7340c7a6 Mon Sep 17 00:00:00 2001 From: mincomk Date: Thu, 12 Jun 2025 16:18:53 +0900 Subject: [PATCH] feat: Quaternion#fromEulerXYZ --- package.json | 2 +- src/quaternion.ts | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 84372ea..95b00d1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "computercraft-mutil", - "version": "1.0.10", + "version": "1.0.11", "description": "", "license": "ISC", "author": "", diff --git a/src/quaternion.ts b/src/quaternion.ts index bd71c1c..eb2638e 100644 --- a/src/quaternion.ts +++ b/src/quaternion.ts @@ -8,9 +8,9 @@ export class Quaternion { public y: number, public z: number, public w: number, - ) { } + ) {} - static fromXYZ({ + public static fromXYZ({ x, y, z, @@ -24,6 +24,27 @@ export class Quaternion { return new Quaternion(x, y, z, w); } + public static fromEulerXYZ( + roll: number, + pitch: number, + yaw: number, + ): Quaternion { + const cx = math.cos(roll * 0.5); + const sx = math.sin(roll * 0.5); + const cy = math.cos(pitch * 0.5); + const sy = math.sin(pitch * 0.5); + const cz = math.cos(yaw * 0.5); + const sz = math.sin(yaw * 0.5); + + // Quaternion multiplication order: q = qx * qy * qz (X → Y → Z) + const w = cx * cy * cz + sx * sy * sz; + const x = sx * cy * cz - cx * sy * sz; + const y = cx * sy * cz + sx * cy * sz; + const z = cx * cy * sz - sx * sy * cz; + + return new Quaternion(x, y, z, w); + } + public toEuler(): Vec3 { const { x, y, z, w } = this;