|
@@ -3723,23 +3723,24 @@ class Quaternion {
|
|
|
|
|
|
random() {
|
|
random() {
|
|
|
|
|
|
- // Derived from http://planning.cs.uiuc.edu/node198.html
|
|
|
|
- // Note, this source uses w, x, y, z ordering,
|
|
|
|
- // so we swap the order below.
|
|
|
|
|
|
+ // sets this quaternion to a uniform random unit quaternnion
|
|
|
|
|
|
- const u1 = Math.random();
|
|
|
|
- const sqrt1u1 = Math.sqrt( 1 - u1 );
|
|
|
|
- const sqrtu1 = Math.sqrt( u1 );
|
|
|
|
|
|
+ // Ken Shoemake
|
|
|
|
+ // Uniform random rotations
|
|
|
|
+ // D. Kirk, editor, Graphics Gems III, pages 124-132. Academic Press, New York, 1992.
|
|
|
|
|
|
- const u2 = 2 * Math.PI * Math.random();
|
|
|
|
|
|
+ const theta1 = 2 * Math.PI * Math.random();
|
|
|
|
+ const theta2 = 2 * Math.PI * Math.random();
|
|
|
|
|
|
- const u3 = 2 * Math.PI * Math.random();
|
|
|
|
|
|
+ const x0 = Math.random();
|
|
|
|
+ const r1 = Math.sqrt( 1 - x0 );
|
|
|
|
+ const r2 = Math.sqrt( x0 );
|
|
|
|
|
|
return this.set(
|
|
return this.set(
|
|
- sqrt1u1 * Math.cos( u2 ),
|
|
|
|
- sqrtu1 * Math.sin( u3 ),
|
|
|
|
- sqrtu1 * Math.cos( u3 ),
|
|
|
|
- sqrt1u1 * Math.sin( u2 ),
|
|
|
|
|
|
+ r1 * Math.sin( theta1 ),
|
|
|
|
+ r1 * Math.cos( theta1 ),
|
|
|
|
+ r2 * Math.sin( theta2 ),
|
|
|
|
+ r2 * Math.cos( theta2 ),
|
|
);
|
|
);
|
|
|
|
|
|
}
|
|
}
|
|
@@ -4507,15 +4508,15 @@ class Vector3 {
|
|
|
|
|
|
randomDirection() {
|
|
randomDirection() {
|
|
|
|
|
|
- // Derived from https://mathworld.wolfram.com/SpherePointPicking.html
|
|
|
|
|
|
+ // https://mathworld.wolfram.com/SpherePointPicking.html
|
|
|
|
|
|
- const u = ( Math.random() - 0.5 ) * 2;
|
|
|
|
- const t = Math.random() * Math.PI * 2;
|
|
|
|
- const f = Math.sqrt( 1 - u ** 2 );
|
|
|
|
|
|
+ const theta = Math.random() * Math.PI * 2;
|
|
|
|
+ const u = Math.random() * 2 - 1;
|
|
|
|
+ const c = Math.sqrt( 1 - u * u );
|
|
|
|
|
|
- this.x = f * Math.cos( t );
|
|
|
|
- this.y = f * Math.sin( t );
|
|
|
|
- this.z = u;
|
|
|
|
|
|
+ this.x = c * Math.cos( theta );
|
|
|
|
+ this.y = u;
|
|
|
|
+ this.z = c * Math.sin( theta );
|
|
|
|
|
|
return this;
|
|
return this;
|
|
|
|
|