Skip to content

Commit 16075d1

Browse files
demvladledvinap
andauthored
Quaternion normalize in case of its module more than 1
Co-authored-by: Petr Ledvina <[email protected]>
1 parent e3a499f commit 16075d1

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/flightlog.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -738,11 +738,16 @@ export function FlightLog(logData) {
738738
w: 1.0,
739739
};
740740

741-
const m = q.x ** 2 + q.y ** 2 + q.z ** 2;
741+
const m = Math.sqrt(q.x ** 2 + q.y ** 2 + q.z ** 2);
742742
if (m < 1.0) {
743-
q.w = Math.sqrt(1.0 - m);
744-
} else {
745-
q.w = 0.0;
743+
// reconstruct .w of unit quaternion
744+
q.w = Math.sqrt(1.0 - m ** 2);
745+
} else {
746+
// normalize [0,x,y,z]
747+
q.x /= m;
748+
q.y /= m;
749+
q.z /= m;
750+
q.w = 0;
746751
}
747752
const xx = q.x ** 2,
748753
xy = q.x * q.y,

0 commit comments

Comments
 (0)