|
@@ -9,47 +9,47 @@ import {
|
|
|
|
|
|
// module scope helper variables
|
|
|
|
|
|
-var a = {
|
|
|
+const a = {
|
|
|
c: null, // center
|
|
|
u: [ new Vector3(), new Vector3(), new Vector3() ], // basis vectors
|
|
|
e: [] // half width
|
|
|
};
|
|
|
|
|
|
-var b = {
|
|
|
+const b = {
|
|
|
c: null, // center
|
|
|
u: [ new Vector3(), new Vector3(), new Vector3() ], // basis vectors
|
|
|
e: [] // half width
|
|
|
};
|
|
|
|
|
|
-var R = [[], [], []];
|
|
|
-var AbsR = [[], [], []];
|
|
|
-var t = [];
|
|
|
-
|
|
|
-var xAxis = new Vector3();
|
|
|
-var yAxis = new Vector3();
|
|
|
-var zAxis = new Vector3();
|
|
|
-var v1 = new Vector3();
|
|
|
-var size = new Vector3();
|
|
|
-var closestPoint = new Vector3();
|
|
|
-var rotationMatrix = new Matrix3();
|
|
|
-var aabb = new Box3();
|
|
|
-var matrix = new Matrix4();
|
|
|
-var inverse = new Matrix4();
|
|
|
-var localRay = new Ray();
|
|
|
+const R = [[], [], []];
|
|
|
+const AbsR = [[], [], []];
|
|
|
+const t = [];
|
|
|
+
|
|
|
+const xAxis = new Vector3();
|
|
|
+const yAxis = new Vector3();
|
|
|
+const zAxis = new Vector3();
|
|
|
+const v1 = new Vector3();
|
|
|
+const size = new Vector3();
|
|
|
+const closestPoint = new Vector3();
|
|
|
+const rotationMatrix = new Matrix3();
|
|
|
+const aabb = new Box3();
|
|
|
+const matrix = new Matrix4();
|
|
|
+const inverse = new Matrix4();
|
|
|
+const localRay = new Ray();
|
|
|
|
|
|
// OBB
|
|
|
|
|
|
-function OBB( center = new Vector3(), halfSize = new Vector3(), rotation = new Matrix3() ) {
|
|
|
+class OBB {
|
|
|
|
|
|
- this.center = center;
|
|
|
- this.halfSize = halfSize;
|
|
|
- this.rotation = rotation;
|
|
|
+ constructor( center = new Vector3(), halfSize = new Vector3(), rotation = new Matrix3() ) {
|
|
|
|
|
|
-}
|
|
|
+ this.center = center;
|
|
|
+ this.halfSize = halfSize;
|
|
|
+ this.rotation = rotation;
|
|
|
|
|
|
-Object.assign( OBB.prototype, {
|
|
|
+ }
|
|
|
|
|
|
- set: function ( center, halfSize, rotation ) {
|
|
|
+ set( center, halfSize, rotation ) {
|
|
|
|
|
|
this.center = center;
|
|
|
this.halfSize = halfSize;
|
|
@@ -57,9 +57,9 @@ Object.assign( OBB.prototype, {
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- copy: function ( obb ) {
|
|
|
+ copy( obb ) {
|
|
|
|
|
|
this.center.copy( obb.center );
|
|
|
this.halfSize.copy( obb.halfSize );
|
|
@@ -67,27 +67,27 @@ Object.assign( OBB.prototype, {
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- clone: function () {
|
|
|
+ clone() {
|
|
|
|
|
|
return new this.constructor().copy( this );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- getSize: function ( result ) {
|
|
|
+ getSize( result ) {
|
|
|
|
|
|
return result.copy( this.halfSize ).multiplyScalar( 2 );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* Reference: Closest Point on OBB to Point in Real-Time Collision Detection
|
|
|
* by Christer Ericson (chapter 5.1.4)
|
|
|
*/
|
|
|
- clampPoint: function ( point, result ) {
|
|
|
+ clampPoint( point, result ) {
|
|
|
|
|
|
- var halfSize = this.halfSize;
|
|
|
+ const halfSize = this.halfSize;
|
|
|
|
|
|
v1.subVectors( point, this.center );
|
|
|
this.rotation.extractBasis( xAxis, yAxis, zAxis );
|
|
@@ -98,20 +98,20 @@ Object.assign( OBB.prototype, {
|
|
|
|
|
|
// project the target onto the OBB axes and walk towards that point
|
|
|
|
|
|
- var x = MathUtils.clamp( v1.dot( xAxis ), - halfSize.x, halfSize.x );
|
|
|
+ const x = MathUtils.clamp( v1.dot( xAxis ), - halfSize.x, halfSize.x );
|
|
|
result.add( xAxis.multiplyScalar( x ) );
|
|
|
|
|
|
- var y = MathUtils.clamp( v1.dot( yAxis ), - halfSize.y, halfSize.y );
|
|
|
+ const y = MathUtils.clamp( v1.dot( yAxis ), - halfSize.y, halfSize.y );
|
|
|
result.add( yAxis.multiplyScalar( y ) );
|
|
|
|
|
|
- var z = MathUtils.clamp( v1.dot( zAxis ), - halfSize.z, halfSize.z );
|
|
|
+ const z = MathUtils.clamp( v1.dot( zAxis ), - halfSize.z, halfSize.z );
|
|
|
result.add( zAxis.multiplyScalar( z ) );
|
|
|
|
|
|
return result;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- containsPoint: function ( point ) {
|
|
|
+ containsPoint( point ) {
|
|
|
|
|
|
v1.subVectors( point, this.center );
|
|
|
this.rotation.extractBasis( xAxis, yAxis, zAxis );
|
|
@@ -122,15 +122,15 @@ Object.assign( OBB.prototype, {
|
|
|
Math.abs( v1.dot( yAxis ) ) <= this.halfSize.y &&
|
|
|
Math.abs( v1.dot( zAxis ) ) <= this.halfSize.z;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- intersectsBox3: function ( box3 ) {
|
|
|
+ intersectsBox3( box3 ) {
|
|
|
|
|
|
return this.intersectsOBB( obb.fromBox3( box3 ) );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- intersectsSphere: function ( sphere ) {
|
|
|
+ intersectsSphere( sphere ) {
|
|
|
|
|
|
// find the point on the OBB closest to the sphere center
|
|
|
|
|
@@ -140,14 +140,14 @@ Object.assign( OBB.prototype, {
|
|
|
|
|
|
return closestPoint.distanceToSquared( sphere.center ) <= ( sphere.radius * sphere.radius );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* Reference: OBB-OBB Intersection in Real-Time Collision Detection
|
|
|
* by Christer Ericson (chapter 4.4.1)
|
|
|
*
|
|
|
*/
|
|
|
- intersectsOBB: function ( obb, epsilon = Number.EPSILON ) {
|
|
|
+ intersectsOBB( obb, epsilon = Number.EPSILON ) {
|
|
|
|
|
|
// prepare data structures (the code uses the same nomenclature like the reference)
|
|
|
|
|
@@ -165,9 +165,9 @@ Object.assign( OBB.prototype, {
|
|
|
|
|
|
// compute rotation matrix expressing b in a's coordinate frame
|
|
|
|
|
|
- for ( var i = 0; i < 3; i ++ ) {
|
|
|
+ for ( let i = 0; i < 3; i ++ ) {
|
|
|
|
|
|
- for ( var j = 0; j < 3; j ++ ) {
|
|
|
+ for ( let j = 0; j < 3; j ++ ) {
|
|
|
|
|
|
R[ i ][ j ] = a.u[ i ].dot( b.u[ j ] );
|
|
|
|
|
@@ -189,9 +189,9 @@ Object.assign( OBB.prototype, {
|
|
|
// counteract arithmetic errors when two edges are parallel and
|
|
|
// their cross product is (near) null
|
|
|
|
|
|
- for ( var i = 0; i < 3; i ++ ) {
|
|
|
+ for ( let i = 0; i < 3; i ++ ) {
|
|
|
|
|
|
- for ( var j = 0; j < 3; j ++ ) {
|
|
|
+ for ( let j = 0; j < 3; j ++ ) {
|
|
|
|
|
|
AbsR[ i ][ j ] = Math.abs( R[ i ][ j ] ) + epsilon;
|
|
|
|
|
@@ -199,11 +199,11 @@ Object.assign( OBB.prototype, {
|
|
|
|
|
|
}
|
|
|
|
|
|
- var ra, rb;
|
|
|
+ let ra, rb;
|
|
|
|
|
|
// test axes L = A0, L = A1, L = A2
|
|
|
|
|
|
- for ( var i = 0; i < 3; i ++ ) {
|
|
|
+ for ( let i = 0; i < 3; i ++ ) {
|
|
|
|
|
|
ra = a.e[ i ];
|
|
|
rb = b.e[ 0 ] * AbsR[ i ][ 0 ] + b.e[ 1 ] * AbsR[ i ][ 1 ] + b.e[ 2 ] * AbsR[ i ][ 2 ];
|
|
@@ -214,7 +214,7 @@ Object.assign( OBB.prototype, {
|
|
|
|
|
|
// test axes L = B0, L = B1, L = B2
|
|
|
|
|
|
- for ( var i = 0; i < 3; i ++ ) {
|
|
|
+ for ( let i = 0; i < 3; i ++ ) {
|
|
|
|
|
|
ra = a.e[ 0 ] * AbsR[ 0 ][ i ] + a.e[ 1 ] * AbsR[ 1 ][ i ] + a.e[ 2 ] * AbsR[ 2 ][ i ];
|
|
|
rb = b.e[ i ];
|
|
@@ -280,13 +280,13 @@ Object.assign( OBB.prototype, {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* Reference: Testing Box Against Plane in Real-Time Collision Detection
|
|
|
* by Christer Ericson (chapter 5.2.3)
|
|
|
*/
|
|
|
- intersectsPlane: function ( plane ) {
|
|
|
+ intersectsPlane( plane ) {
|
|
|
|
|
|
this.rotation.extractBasis( xAxis, yAxis, zAxis );
|
|
|
|
|
@@ -304,13 +304,13 @@ Object.assign( OBB.prototype, {
|
|
|
|
|
|
return Math.abs( d ) <= r;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* Performs a ray/OBB intersection test and stores the intersection point
|
|
|
* to the given 3D vector. If no intersection is detected, *null* is returned.
|
|
|
*/
|
|
|
- intersectRay: function ( ray, result ) {
|
|
|
+ intersectRay( ray, result ) {
|
|
|
|
|
|
// the idea is to perform the intersection test in the local space
|
|
|
// of the OBB.
|
|
@@ -342,19 +342,19 @@ Object.assign( OBB.prototype, {
|
|
|
|
|
|
}
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* Performs a ray/OBB intersection test. Returns either true or false if
|
|
|
* there is a intersection or not.
|
|
|
*/
|
|
|
- intersectsRay: function ( ray ) {
|
|
|
+ intersectsRay( ray ) {
|
|
|
|
|
|
return this.intersectRay( ray, v1 ) !== null;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- fromBox3: function ( box3 ) {
|
|
|
+ fromBox3( box3 ) {
|
|
|
|
|
|
box3.getCenter( this.center );
|
|
|
|
|
@@ -364,32 +364,32 @@ Object.assign( OBB.prototype, {
|
|
|
|
|
|
return this;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- equals: function ( obb ) {
|
|
|
+ equals( obb ) {
|
|
|
|
|
|
return obb.center.equals( this.center ) &&
|
|
|
obb.halfSize.equals( this.halfSize ) &&
|
|
|
obb.rotation.equals( this.rotation );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- applyMatrix4: function ( matrix ) {
|
|
|
+ applyMatrix4( matrix ) {
|
|
|
|
|
|
- var e = matrix.elements;
|
|
|
+ const e = matrix.elements;
|
|
|
|
|
|
- var sx = v1.set( e[ 0 ], e[ 1 ], e[ 2 ] ).length();
|
|
|
- var sy = v1.set( e[ 4 ], e[ 5 ], e[ 6 ] ).length();
|
|
|
- var sz = v1.set( e[ 8 ], e[ 9 ], e[ 10 ] ).length();
|
|
|
+ const sx = v1.set( e[ 0 ], e[ 1 ], e[ 2 ] ).length();
|
|
|
+ const sy = v1.set( e[ 4 ], e[ 5 ], e[ 6 ] ).length();
|
|
|
+ const sz = v1.set( e[ 8 ], e[ 9 ], e[ 10 ] ).length();
|
|
|
|
|
|
- var det = matrix.determinant();
|
|
|
+ const det = matrix.determinant();
|
|
|
if ( det < 0 ) sx = - sx;
|
|
|
|
|
|
rotationMatrix.setFromMatrix4( matrix );
|
|
|
|
|
|
- var invSX = 1 / sx;
|
|
|
- var invSY = 1 / sy;
|
|
|
- var invSZ = 1 / sz;
|
|
|
+ const invSX = 1 / sx;
|
|
|
+ const invSY = 1 / sy;
|
|
|
+ const invSZ = 1 / sz;
|
|
|
|
|
|
rotationMatrix.elements[ 0 ] *= invSX;
|
|
|
rotationMatrix.elements[ 1 ] *= invSX;
|
|
@@ -416,8 +416,8 @@ Object.assign( OBB.prototype, {
|
|
|
|
|
|
}
|
|
|
|
|
|
-} );
|
|
|
+}
|
|
|
|
|
|
-var obb = new OBB();
|
|
|
+const obb = new OBB();
|
|
|
|
|
|
export { OBB };
|