浏览代码

Introduce Quaternion.FromAxisAngle()

customisations
alemart 7 个月前
父节点
当前提交
dc365e2ed8
共有 1 个文件被更改,包括 26 次插入0 次删除
  1. 26
    0
      src/geometry/quaternion.ts

+ 26
- 0
src/geometry/quaternion.ts 查看文件

@@ -23,6 +23,7 @@
23 23
 import Speedy from 'speedy-vision';
24 24
 import { SpeedyMatrix } from 'speedy-vision/types/core/speedy-matrix';
25 25
 import { IllegalArgumentError } from '../utils/errors';
26
+import { Vector3 } from './vector3';
26 27
 
27 28
 /** Small number */
28 29
 const EPSILON = 1e-6;
@@ -75,6 +76,31 @@ export class Quaternion
75 76
     }
76 77
 
77 78
     /**
79
+     * Create a unit quaternion from an axis-angle representation of a rotation
80
+     * @param axis non-zero
81
+     * @param angle in radians
82
+     * @returns a new quaternion
83
+     */
84
+    static FromAxisAngle(axis: Vector3, angle: number): Quaternion
85
+    {
86
+        // sanity check
87
+        if(axis.dot(axis) < EPSILON * EPSILON)
88
+            return Quaternion.Identity();
89
+
90
+        // create the quaternion
91
+        const sin = Math.sin(angle / 2);
92
+        const cos = Math.cos(angle / 2);
93
+        const r = axis.normalized();
94
+
95
+        const x = r.x * sin;
96
+        const y = r.y * sin;
97
+        const z = r.z * sin;
98
+        const w = cos;
99
+
100
+        return new Quaternion(x, y, z, w);
101
+    }
102
+
103
+    /**
78 104
      * The x coordinate of the quaternion (imaginary)
79 105
      */
80 106
     get x(): number

正在加载...
取消
保存