Pārlūkot izejas kodu

Add Vector3._applyRotationQuaternion()

customisations
alemart 10 mēnešus atpakaļ
vecāks
revīzija
fcd0b5889d
3 mainītis faili ar 34 papildinājumiem un 17 dzēšanām
  1. 1
    1
      src/geometry/quaternion.ts
  2. 8
    16
      src/geometry/transform.ts
  3. 25
    0
      src/geometry/vector3.ts

+ 1
- 1
src/geometry/quaternion.ts Parādīt failu

@@ -338,7 +338,7 @@ export class Quaternion
338 338
         unit quaternion q associated with M.
339 339
 
340 340
         Before we begin, note that q and (-q) encode the same rotation, for
341
-        r_(-q)(p) = (-q)p(-q)* = (-1)q p (-1)q* = (-1)(-1)q p q* = = r_q(p).
341
+        r_(-q)(p) = (-q)p(-q)* = (-1)q p (-1)q* = (-1)(-1)q p q* = q p q* = r_q(p).
342 342
         Quaternion multiplication is commutative when a factor is a scalar, i.e.,
343 343
         d p = p d for a real d and a quaternion p (check: distributive operation).
344 344
 

+ 8
- 16
src/geometry/transform.ts Parādīt failu

@@ -153,9 +153,8 @@ export class Transform
153 153
     get right(): Vector3
154 154
     {
155 155
         if(this._right === Vector3.ZERO) {
156
-            const rotationMatrix = this.orientation._toRotationMatrix(); // R
157
-            const u = rotationMatrix.block(0, 2, 0, 0).read(); // R * [1 0 0]'
158
-            this._right = new Vector3(u[0], u[1], u[2]);
156
+            this._right = new Vector3(1, 0, 0)
157
+                          ._applyRotationQuaternion(this.orientation);
159 158
         }
160 159
 
161 160
         return this._right;
@@ -167,9 +166,8 @@ export class Transform
167 166
     get up(): Vector3
168 167
     {
169 168
         if(this._up === Vector3.ZERO) {
170
-            const rotationMatrix = this.orientation._toRotationMatrix(); // R
171
-            const v = rotationMatrix.block(0, 2, 1, 1).read(); // R * [0 1 0]'
172
-            this._up = new Vector3(v[0], v[1], v[2]);
169
+            this._up = new Vector3(0, 1, 0)
170
+                       ._applyRotationQuaternion(this.orientation);
173 171
         }
174 172
 
175 173
         return this._up;
@@ -181,16 +179,10 @@ export class Transform
181 179
     get forward(): Vector3
182 180
     {
183 181
         if(this._forward === Vector3.ZERO) {
184
-            const rotationMatrix = this.orientation._toRotationMatrix(); // R
185
-            const w = rotationMatrix.block(0, 2, 2, 2).read(); // R * [0 0 1]'
186
-            this._forward = new Vector3(-w[0], -w[1], -w[2]); // (*)
187
-
188
-            /*
189
-
190
-            (*) in a right-handed system, the unit forward vector is (0,0,-1)
191
-                in a left-handed system, it is (0,0,1)
192
-
193
-            */
182
+            // in a right-handed system, the unit forward vector is (0, 0, -1)
183
+            // in a left-handed system, it is (0, 0, 1)
184
+            this._forward = new Vector3(0, 0, -1)
185
+                            ._applyRotationQuaternion(this.orientation);
194 186
         }
195 187
 
196 188
         return this._forward;

+ 25
- 0
src/geometry/vector3.ts Parādīt failu

@@ -21,6 +21,7 @@
21 21
  */
22 22
 
23 23
 import { Nullable } from '../utils/utils';
24
+import { Quaternion } from './quaternion';
24 25
 
25 26
 /** Small number */
26 27
 const EPSILON = 1e-6;
@@ -288,4 +289,28 @@ export class Vector3
288 289
 
289 290
         return this;
290 291
     }
292
+
293
+    /**
294
+     * Compute the rotation q p q* in place, where q is a unit quaternion,
295
+     * q* is its conjugate and multiplicative inverse, and p is this vector
296
+     * @param q unit quaternion
297
+     * @returns this vector
298
+     * @internal
299
+     */
300
+    _applyRotationQuaternion(q: Quaternion): Vector3
301
+    {
302
+        // based on Quaternion._toRotationMatrix()
303
+        const x = q.x, y = q.y, z = q.z, w = q.w;
304
+        const vx = this._x, vy = this._y, vz = this._z;
305
+
306
+        const x2 = x*x, y2 = y*y, z2 = z*z;
307
+        const xy = 2*x*y, xz = 2*x*z, yz = 2*y*z;
308
+        const wx = 2*w*x, wy = 2*w*y, wz = 2*w*z;
309
+
310
+        this._x = (1-2*(y2+z2)) * vx + (xy-wz) * vy + (xz+wy) * vz;
311
+        this._y = (xy+wz) * vx + (1-2*(x2+z2)) * vy + (yz-wx) * vz;
312
+        this._z = (xz-wy) * vx + (yz+wx) * vy + (1-2*(x2+y2)) * vz;
313
+
314
+        return this;
315
+    }
291 316
 }

Notiek ielāde…
Atcelt
Saglabāt