OBB.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /**
  2. * @author Mugen87 / https://github.com/Mugen87
  3. */
  4. import {
  5. MathUtils,
  6. Matrix3,
  7. Vector3
  8. } from "../../../build/three.module.js";
  9. // module scope helper variables
  10. var a = {
  11. c: null, // center
  12. u: [ new Vector3(), new Vector3(), new Vector3() ], // basis vectors
  13. e: [] // half width
  14. };
  15. var b = {
  16. c: null, // center
  17. u: [ new Vector3(), new Vector3(), new Vector3() ], // basis vectors
  18. e: [] // half width
  19. };
  20. var R = [[], [], []];
  21. var AbsR = [[], [], []];
  22. var t = [];
  23. var xAxis = new Vector3();
  24. var yAxis = new Vector3();
  25. var zAxis = new Vector3();
  26. var v1 = new Vector3();
  27. var closestPoint = new Vector3();
  28. var rotationMatrix = new Matrix3();
  29. // OBB
  30. function OBB( center = new Vector3(), halfSize = new Vector3(), rotation = new Matrix3() ) {
  31. this.center = center;
  32. this.halfSize = halfSize;
  33. this.rotation = rotation;
  34. }
  35. Object.assign( OBB.prototype, {
  36. set: function ( center, halfSize, rotation ) {
  37. this.center = center;
  38. this.halfSize = halfSize;
  39. this.rotation = rotation;
  40. return this;
  41. },
  42. copy: function ( obb ) {
  43. this.center.copy( obb.center );
  44. this.halfSize.copy( obb.halfSize );
  45. this.rotation.copy( obb.rotation );
  46. return this;
  47. },
  48. clone: function () {
  49. return new this.constructor().copy( this );
  50. },
  51. getSize: function ( result ) {
  52. return result.copy( this.halfSize ).multiplyScalar( 2 );
  53. },
  54. /**
  55. * Reference: Closest Point on OBB to Point in Real-Time Collision Detection
  56. * by Christer Ericson (chapter 5.1.4)
  57. */
  58. clampPoint: function ( point, result ) {
  59. var halfSize = this.halfSize;
  60. v1.subVectors( point, this.center );
  61. this.rotation.extractBasis( xAxis, yAxis, zAxis );
  62. // start at the center position of the OBB
  63. result.copy( this.center );
  64. // project the target onto the OBB axes and walk towards that point
  65. var x = MathUtils.clamp( v1.dot( xAxis ), - halfSize.x, halfSize.x );
  66. result.add( xAxis.multiplyScalar( x ) );
  67. var y = MathUtils.clamp( v1.dot( yAxis ), - halfSize.y, halfSize.y );
  68. result.add( yAxis.multiplyScalar( y ) );
  69. var z = MathUtils.clamp( v1.dot( zAxis ), - halfSize.z, halfSize.z );
  70. result.add( zAxis.multiplyScalar( z ) );
  71. return result;
  72. },
  73. containsPoint: function ( point ) {
  74. v1.subVectors( point, this.center );
  75. this.rotation.extractBasis( xAxis, yAxis, zAxis );
  76. // project v1 onto each axis and check if these points lie inside the OBB
  77. return Math.abs( v1.dot( xAxis ) ) <= this.halfSize.x &&
  78. Math.abs( v1.dot( yAxis ) ) <= this.halfSize.y &&
  79. Math.abs( v1.dot( zAxis ) ) <= this.halfSize.z;
  80. },
  81. intersectsBox3: function ( box3 ) {
  82. return this.intersectsOBB( obb.fromBox3( box3 ) );
  83. },
  84. intersectsSphere: function ( sphere ) {
  85. // find the point on the OBB closest to the sphere center
  86. this.clampPoint( sphere.center, closestPoint );
  87. // if that point is inside the sphere, the OBB and sphere intersect
  88. return closestPoint.distanceToSquared( sphere.center ) <= ( sphere.radius * sphere.radius );
  89. },
  90. /**
  91. * Reference: OBB-OBB Intersection in Real-Time Collision Detection
  92. * by Christer Ericson (chapter 4.4.1)
  93. *
  94. */
  95. intersectsOBB: function ( obb, epsilon = Number.EPSILON ) {
  96. // prepare data structures (the code uses the same nomenclature like the reference)
  97. a.c = this.center;
  98. a.e[ 0 ] = this.halfSize.x;
  99. a.e[ 1 ] = this.halfSize.y;
  100. a.e[ 2 ] = this.halfSize.z;
  101. this.rotation.extractBasis( a.u[ 0 ], a.u[ 1 ], a.u[ 2 ] );
  102. b.c = obb.center;
  103. b.e[ 0 ] = obb.halfSize.x;
  104. b.e[ 1 ] = obb.halfSize.y;
  105. b.e[ 2 ] = obb.halfSize.z;
  106. obb.rotation.extractBasis( b.u[ 0 ], b.u[ 1 ], b.u[ 2 ] );
  107. // compute rotation matrix expressing b in a's coordinate frame
  108. for ( var i = 0; i < 3; i ++ ) {
  109. for ( var j = 0; j < 3; j ++ ) {
  110. R[ i ][ j ] = a.u[ i ].dot( b.u[ j ] );
  111. }
  112. }
  113. // compute translation vector
  114. v1.subVectors( b.c, a.c );
  115. // bring translation into a's coordinate frame
  116. t[ 0 ] = v1.dot( a.u[ 0 ] );
  117. t[ 1 ] = v1.dot( a.u[ 1 ] );
  118. t[ 2 ] = v1.dot( a.u[ 2 ] );
  119. // compute common subexpressions. Add in an epsilon term to
  120. // counteract arithmetic errors when two edges are parallel and
  121. // their cross product is (near) null
  122. for ( var i = 0; i < 3; i ++ ) {
  123. for ( var j = 0; j < 3; j ++ ) {
  124. AbsR[ i ][ j ] = Math.abs( R[ i ][ j ] ) + epsilon;
  125. }
  126. }
  127. var ra, rb;
  128. // test axes L = A0, L = A1, L = A2
  129. for ( var i = 0; i < 3; i ++ ) {
  130. ra = a.e[ i ];
  131. rb = b.e[ 0 ] * AbsR[ i ][ 0 ] + b.e[ 1 ] * AbsR[ i ][ 1 ] + b.e[ 2 ] * AbsR[ i ][ 2 ];
  132. if ( Math.abs( t[ i ] ) > ra + rb ) return false;
  133. }
  134. // test axes L = B0, L = B1, L = B2
  135. for ( var i = 0; i < 3; i ++ ) {
  136. ra = a.e[ 0 ] * AbsR[ 0 ][ i ] + a.e[ 1 ] * AbsR[ 1 ][ i ] + a.e[ 2 ] * AbsR[ 2 ][ i ];
  137. rb = b.e[ i ];
  138. if ( Math.abs( t[ 0 ] * R[ 0 ][ i ] + t[ 1 ] * R[ 1 ][ i ] + t[ 2 ] * R[ 2 ][ i ] ) > ra + rb ) return false;
  139. }
  140. // test axis L = A0 x B0
  141. ra = a.e[ 1 ] * AbsR[ 2 ][ 0 ] + a.e[ 2 ] * AbsR[ 1 ][ 0 ];
  142. rb = b.e[ 1 ] * AbsR[ 0 ][ 2 ] + b.e[ 2 ] * AbsR[ 0 ][ 1 ];
  143. if ( Math.abs( t[ 2 ] * R[ 1 ][ 0 ] - t[ 1 ] * R[ 2 ][ 0 ] ) > ra + rb ) return false;
  144. // test axis L = A0 x B1
  145. ra = a.e[ 1 ] * AbsR[ 2 ][ 1 ] + a.e[ 2 ] * AbsR[ 1 ][ 1 ];
  146. rb = b.e[ 0 ] * AbsR[ 0 ][ 2 ] + b.e[ 2 ] * AbsR[ 0 ][ 0 ];
  147. if ( Math.abs( t[ 2 ] * R[ 1 ][ 1 ] - t[ 1 ] * R[ 2 ][ 1 ] ) > ra + rb ) return false;
  148. // test axis L = A0 x B2
  149. ra = a.e[ 1 ] * AbsR[ 2 ][ 2 ] + a.e[ 2 ] * AbsR[ 1 ][ 2 ];
  150. rb = b.e[ 0 ] * AbsR[ 0 ][ 1 ] + b.e[ 1 ] * AbsR[ 0 ][ 0 ];
  151. if ( Math.abs( t[ 2 ] * R[ 1 ][ 2 ] - t[ 1 ] * R[ 2 ][ 2 ] ) > ra + rb ) return false;
  152. // test axis L = A1 x B0
  153. ra = a.e[ 0 ] * AbsR[ 2 ][ 0 ] + a.e[ 2 ] * AbsR[ 0 ][ 0 ];
  154. rb = b.e[ 1 ] * AbsR[ 1 ][ 2 ] + b.e[ 2 ] * AbsR[ 1 ][ 1 ];
  155. if ( Math.abs( t[ 0 ] * R[ 2 ][ 0 ] - t[ 2 ] * R[ 0 ][ 0 ] ) > ra + rb ) return false;
  156. // test axis L = A1 x B1
  157. ra = a.e[ 0 ] * AbsR[ 2 ][ 1 ] + a.e[ 2 ] * AbsR[ 0 ][ 1 ];
  158. rb = b.e[ 0 ] * AbsR[ 1 ][ 2 ] + b.e[ 2 ] * AbsR[ 1 ][ 0 ];
  159. if ( Math.abs( t[ 0 ] * R[ 2 ][ 1 ] - t[ 2 ] * R[ 0 ][ 1 ] ) > ra + rb ) return false;
  160. // test axis L = A1 x B2
  161. ra = a.e[ 0 ] * AbsR[ 2 ][ 2 ] + a.e[ 2 ] * AbsR[ 0 ][ 2 ];
  162. rb = b.e[ 0 ] * AbsR[ 1 ][ 1 ] + b.e[ 1 ] * AbsR[ 1 ][ 0 ];
  163. if ( Math.abs( t[ 0 ] * R[ 2 ][ 2 ] - t[ 2 ] * R[ 0 ][ 2 ] ) > ra + rb ) return false;
  164. // test axis L = A2 x B0
  165. ra = a.e[ 0 ] * AbsR[ 1 ][ 0 ] + a.e[ 1 ] * AbsR[ 0 ][ 0 ];
  166. rb = b.e[ 1 ] * AbsR[ 2 ][ 2 ] + b.e[ 2 ] * AbsR[ 2 ][ 1 ];
  167. if ( Math.abs( t[ 1 ] * R[ 0 ][ 0 ] - t[ 0 ] * R[ 1 ][ 0 ] ) > ra + rb ) return false;
  168. // test axis L = A2 x B1
  169. ra = a.e[ 0 ] * AbsR[ 1 ][ 1 ] + a.e[ 1 ] * AbsR[ 0 ][ 1 ];
  170. rb = b.e[ 0 ] * AbsR[ 2 ][ 2 ] + b.e[ 2 ] * AbsR[ 2 ][ 0 ];
  171. if ( Math.abs( t[ 1 ] * R[ 0 ][ 1 ] - t[ 0 ] * R[ 1 ][ 1 ] ) > ra + rb ) return false;
  172. // test axis L = A2 x B2
  173. ra = a.e[ 0 ] * AbsR[ 1 ][ 2 ] + a.e[ 1 ] * AbsR[ 0 ][ 2 ];
  174. rb = b.e[ 0 ] * AbsR[ 2 ][ 1 ] + b.e[ 1 ] * AbsR[ 2 ][ 0 ];
  175. if ( Math.abs( t[ 1 ] * R[ 0 ][ 2 ] - t[ 0 ] * R[ 1 ][ 2 ] ) > ra + rb ) return false;
  176. // since no separating axis is found, the OBBs must be intersecting
  177. return true;
  178. },
  179. /**
  180. * Reference: Testing Box Against Plane in Real-Time Collision Detection
  181. * by Christer Ericson (chapter 5.2.3)
  182. */
  183. intersectsPlane: function ( plane ) {
  184. this.rotation.extractBasis( xAxis, yAxis, zAxis );
  185. // compute the projection interval radius of this OBB onto L(t) = this->center + t * p.normal;
  186. const r = this.halfSize.x * Math.abs( plane.normal.dot( xAxis ) ) +
  187. this.halfSize.y * Math.abs( plane.normal.dot( yAxis ) ) +
  188. this.halfSize.z * Math.abs( plane.normal.dot( zAxis ) );
  189. // compute distance of the OBB's center from the plane
  190. const d = plane.normal.dot( this.center ) - plane.constant;
  191. // Intersection occurs when distance d falls within [-r,+r] interval
  192. return Math.abs( d ) <= r;
  193. },
  194. fromBox3: function ( box3 ) {
  195. box3.getCenter( this.center );
  196. box3.getSize( this.halfSize ).multiplyScalar( 0.5 );
  197. this.rotation.identity();
  198. return this;
  199. },
  200. equals: function ( obb ) {
  201. return obb.center.equals( this.center ) &&
  202. obb.halfSize.equals( this.halfSize ) &&
  203. obb.rotation.equals( this.rotation );
  204. },
  205. applyMatrix4: function ( matrix ) {
  206. var e = matrix.elements;
  207. var sx = v1.set( e[ 0 ], e[ 1 ], e[ 2 ] ).length();
  208. var sy = v1.set( e[ 4 ], e[ 5 ], e[ 6 ] ).length();
  209. var sz = v1.set( e[ 8 ], e[ 9 ], e[ 10 ] ).length();
  210. var det = matrix.determinant();
  211. if ( det < 0 ) sx = - sx;
  212. rotationMatrix.setFromMatrix4( matrix );
  213. var invSX = 1 / sx;
  214. var invSY = 1 / sy;
  215. var invSZ = 1 / sz;
  216. rotationMatrix.elements[ 0 ] *= invSX;
  217. rotationMatrix.elements[ 1 ] *= invSX;
  218. rotationMatrix.elements[ 2 ] *= invSX;
  219. rotationMatrix.elements[ 3 ] *= invSY;
  220. rotationMatrix.elements[ 4 ] *= invSY;
  221. rotationMatrix.elements[ 5 ] *= invSY;
  222. rotationMatrix.elements[ 6 ] *= invSZ;
  223. rotationMatrix.elements[ 7 ] *= invSZ;
  224. rotationMatrix.elements[ 8 ] *= invSZ;
  225. this.rotation.multiply( rotationMatrix );
  226. this.halfSize.x *= sx;
  227. this.halfSize.y *= sy;
  228. this.halfSize.z *= sz;
  229. v1.setFromMatrixPosition( matrix );
  230. this.center.add( v1 );
  231. return this;
  232. }
  233. } );
  234. var obb = new OBB();
  235. export { OBB };