OBB.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. import {
  2. Box3,
  3. MathUtils,
  4. Matrix4,
  5. Matrix3,
  6. Ray,
  7. Vector3
  8. } from 'three';
  9. // module scope helper variables
  10. const a = {
  11. c: null, // center
  12. u: [ new Vector3(), new Vector3(), new Vector3() ], // basis vectors
  13. e: [] // half width
  14. };
  15. const b = {
  16. c: null, // center
  17. u: [ new Vector3(), new Vector3(), new Vector3() ], // basis vectors
  18. e: [] // half width
  19. };
  20. const R = [[], [], []];
  21. const AbsR = [[], [], []];
  22. const t = [];
  23. const xAxis = new Vector3();
  24. const yAxis = new Vector3();
  25. const zAxis = new Vector3();
  26. const v1 = new Vector3();
  27. const size = new Vector3();
  28. const closestPoint = new Vector3();
  29. const rotationMatrix = new Matrix3();
  30. const aabb = new Box3();
  31. const matrix = new Matrix4();
  32. const inverse = new Matrix4();
  33. const localRay = new Ray();
  34. // OBB
  35. class OBB {
  36. constructor( center = new Vector3(), halfSize = new Vector3(), rotation = new Matrix3() ) {
  37. this.center = center;
  38. this.halfSize = halfSize;
  39. this.rotation = rotation;
  40. }
  41. set( center, halfSize, rotation ) {
  42. this.center = center;
  43. this.halfSize = halfSize;
  44. this.rotation = rotation;
  45. return this;
  46. }
  47. copy( obb ) {
  48. this.center.copy( obb.center );
  49. this.halfSize.copy( obb.halfSize );
  50. this.rotation.copy( obb.rotation );
  51. return this;
  52. }
  53. clone() {
  54. return new this.constructor().copy( this );
  55. }
  56. getSize( result ) {
  57. return result.copy( this.halfSize ).multiplyScalar( 2 );
  58. }
  59. /**
  60. * Reference: Closest Point on OBB to Point in Real-Time Collision Detection
  61. * by Christer Ericson (chapter 5.1.4)
  62. *
  63. * @param {Vector3} point
  64. * @param {Vector3} result
  65. * @returns {Vector3}
  66. */
  67. clampPoint( point, result ) {
  68. const halfSize = this.halfSize;
  69. v1.subVectors( point, this.center );
  70. this.rotation.extractBasis( xAxis, yAxis, zAxis );
  71. // start at the center position of the OBB
  72. result.copy( this.center );
  73. // project the target onto the OBB axes and walk towards that point
  74. const x = MathUtils.clamp( v1.dot( xAxis ), - halfSize.x, halfSize.x );
  75. result.add( xAxis.multiplyScalar( x ) );
  76. const y = MathUtils.clamp( v1.dot( yAxis ), - halfSize.y, halfSize.y );
  77. result.add( yAxis.multiplyScalar( y ) );
  78. const z = MathUtils.clamp( v1.dot( zAxis ), - halfSize.z, halfSize.z );
  79. result.add( zAxis.multiplyScalar( z ) );
  80. return result;
  81. }
  82. containsPoint( point ) {
  83. v1.subVectors( point, this.center );
  84. this.rotation.extractBasis( xAxis, yAxis, zAxis );
  85. // project v1 onto each axis and check if these points lie inside the OBB
  86. return Math.abs( v1.dot( xAxis ) ) <= this.halfSize.x &&
  87. Math.abs( v1.dot( yAxis ) ) <= this.halfSize.y &&
  88. Math.abs( v1.dot( zAxis ) ) <= this.halfSize.z;
  89. }
  90. intersectsBox3( box3 ) {
  91. return this.intersectsOBB( obb.fromBox3( box3 ) );
  92. }
  93. intersectsSphere( sphere ) {
  94. // find the point on the OBB closest to the sphere center
  95. this.clampPoint( sphere.center, closestPoint );
  96. // if that point is inside the sphere, the OBB and sphere intersect
  97. return closestPoint.distanceToSquared( sphere.center ) <= ( sphere.radius * sphere.radius );
  98. }
  99. /**
  100. * Reference: OBB-OBB Intersection in Real-Time Collision Detection
  101. * by Christer Ericson (chapter 4.4.1)
  102. *
  103. * @param {OBB} obb
  104. * @param {Number} [epsilon=Number.EPSILON] - A small value to prevent arithmetic errors
  105. * @returns {Boolean}
  106. */
  107. intersectsOBB( obb, epsilon = Number.EPSILON ) {
  108. // prepare data structures (the code uses the same nomenclature like the reference)
  109. a.c = this.center;
  110. a.e[ 0 ] = this.halfSize.x;
  111. a.e[ 1 ] = this.halfSize.y;
  112. a.e[ 2 ] = this.halfSize.z;
  113. this.rotation.extractBasis( a.u[ 0 ], a.u[ 1 ], a.u[ 2 ] );
  114. b.c = obb.center;
  115. b.e[ 0 ] = obb.halfSize.x;
  116. b.e[ 1 ] = obb.halfSize.y;
  117. b.e[ 2 ] = obb.halfSize.z;
  118. obb.rotation.extractBasis( b.u[ 0 ], b.u[ 1 ], b.u[ 2 ] );
  119. // compute rotation matrix expressing b in a's coordinate frame
  120. for ( let i = 0; i < 3; i ++ ) {
  121. for ( let j = 0; j < 3; j ++ ) {
  122. R[ i ][ j ] = a.u[ i ].dot( b.u[ j ] );
  123. }
  124. }
  125. // compute translation vector
  126. v1.subVectors( b.c, a.c );
  127. // bring translation into a's coordinate frame
  128. t[ 0 ] = v1.dot( a.u[ 0 ] );
  129. t[ 1 ] = v1.dot( a.u[ 1 ] );
  130. t[ 2 ] = v1.dot( a.u[ 2 ] );
  131. // compute common subexpressions. Add in an epsilon term to
  132. // counteract arithmetic errors when two edges are parallel and
  133. // their cross product is (near) null
  134. for ( let i = 0; i < 3; i ++ ) {
  135. for ( let j = 0; j < 3; j ++ ) {
  136. AbsR[ i ][ j ] = Math.abs( R[ i ][ j ] ) + epsilon;
  137. }
  138. }
  139. let ra, rb;
  140. // test axes L = A0, L = A1, L = A2
  141. for ( let i = 0; i < 3; i ++ ) {
  142. ra = a.e[ i ];
  143. rb = b.e[ 0 ] * AbsR[ i ][ 0 ] + b.e[ 1 ] * AbsR[ i ][ 1 ] + b.e[ 2 ] * AbsR[ i ][ 2 ];
  144. if ( Math.abs( t[ i ] ) > ra + rb ) return false;
  145. }
  146. // test axes L = B0, L = B1, L = B2
  147. for ( let i = 0; i < 3; i ++ ) {
  148. ra = a.e[ 0 ] * AbsR[ 0 ][ i ] + a.e[ 1 ] * AbsR[ 1 ][ i ] + a.e[ 2 ] * AbsR[ 2 ][ i ];
  149. rb = b.e[ i ];
  150. if ( Math.abs( t[ 0 ] * R[ 0 ][ i ] + t[ 1 ] * R[ 1 ][ i ] + t[ 2 ] * R[ 2 ][ i ] ) > ra + rb ) return false;
  151. }
  152. // test axis L = A0 x B0
  153. ra = a.e[ 1 ] * AbsR[ 2 ][ 0 ] + a.e[ 2 ] * AbsR[ 1 ][ 0 ];
  154. rb = b.e[ 1 ] * AbsR[ 0 ][ 2 ] + b.e[ 2 ] * AbsR[ 0 ][ 1 ];
  155. if ( Math.abs( t[ 2 ] * R[ 1 ][ 0 ] - t[ 1 ] * R[ 2 ][ 0 ] ) > ra + rb ) return false;
  156. // test axis L = A0 x B1
  157. ra = a.e[ 1 ] * AbsR[ 2 ][ 1 ] + a.e[ 2 ] * AbsR[ 1 ][ 1 ];
  158. rb = b.e[ 0 ] * AbsR[ 0 ][ 2 ] + b.e[ 2 ] * AbsR[ 0 ][ 0 ];
  159. if ( Math.abs( t[ 2 ] * R[ 1 ][ 1 ] - t[ 1 ] * R[ 2 ][ 1 ] ) > ra + rb ) return false;
  160. // test axis L = A0 x B2
  161. ra = a.e[ 1 ] * AbsR[ 2 ][ 2 ] + a.e[ 2 ] * AbsR[ 1 ][ 2 ];
  162. rb = b.e[ 0 ] * AbsR[ 0 ][ 1 ] + b.e[ 1 ] * AbsR[ 0 ][ 0 ];
  163. if ( Math.abs( t[ 2 ] * R[ 1 ][ 2 ] - t[ 1 ] * R[ 2 ][ 2 ] ) > ra + rb ) return false;
  164. // test axis L = A1 x B0
  165. ra = a.e[ 0 ] * AbsR[ 2 ][ 0 ] + a.e[ 2 ] * AbsR[ 0 ][ 0 ];
  166. rb = b.e[ 1 ] * AbsR[ 1 ][ 2 ] + b.e[ 2 ] * AbsR[ 1 ][ 1 ];
  167. if ( Math.abs( t[ 0 ] * R[ 2 ][ 0 ] - t[ 2 ] * R[ 0 ][ 0 ] ) > ra + rb ) return false;
  168. // test axis L = A1 x B1
  169. ra = a.e[ 0 ] * AbsR[ 2 ][ 1 ] + a.e[ 2 ] * AbsR[ 0 ][ 1 ];
  170. rb = b.e[ 0 ] * AbsR[ 1 ][ 2 ] + b.e[ 2 ] * AbsR[ 1 ][ 0 ];
  171. if ( Math.abs( t[ 0 ] * R[ 2 ][ 1 ] - t[ 2 ] * R[ 0 ][ 1 ] ) > ra + rb ) return false;
  172. // test axis L = A1 x B2
  173. ra = a.e[ 0 ] * AbsR[ 2 ][ 2 ] + a.e[ 2 ] * AbsR[ 0 ][ 2 ];
  174. rb = b.e[ 0 ] * AbsR[ 1 ][ 1 ] + b.e[ 1 ] * AbsR[ 1 ][ 0 ];
  175. if ( Math.abs( t[ 0 ] * R[ 2 ][ 2 ] - t[ 2 ] * R[ 0 ][ 2 ] ) > ra + rb ) return false;
  176. // test axis L = A2 x B0
  177. ra = a.e[ 0 ] * AbsR[ 1 ][ 0 ] + a.e[ 1 ] * AbsR[ 0 ][ 0 ];
  178. rb = b.e[ 1 ] * AbsR[ 2 ][ 2 ] + b.e[ 2 ] * AbsR[ 2 ][ 1 ];
  179. if ( Math.abs( t[ 1 ] * R[ 0 ][ 0 ] - t[ 0 ] * R[ 1 ][ 0 ] ) > ra + rb ) return false;
  180. // test axis L = A2 x B1
  181. ra = a.e[ 0 ] * AbsR[ 1 ][ 1 ] + a.e[ 1 ] * AbsR[ 0 ][ 1 ];
  182. rb = b.e[ 0 ] * AbsR[ 2 ][ 2 ] + b.e[ 2 ] * AbsR[ 2 ][ 0 ];
  183. if ( Math.abs( t[ 1 ] * R[ 0 ][ 1 ] - t[ 0 ] * R[ 1 ][ 1 ] ) > ra + rb ) return false;
  184. // test axis L = A2 x B2
  185. ra = a.e[ 0 ] * AbsR[ 1 ][ 2 ] + a.e[ 1 ] * AbsR[ 0 ][ 2 ];
  186. rb = b.e[ 0 ] * AbsR[ 2 ][ 1 ] + b.e[ 1 ] * AbsR[ 2 ][ 0 ];
  187. if ( Math.abs( t[ 1 ] * R[ 0 ][ 2 ] - t[ 0 ] * R[ 1 ][ 2 ] ) > ra + rb ) return false;
  188. // since no separating axis is found, the OBBs must be intersecting
  189. return true;
  190. }
  191. /**
  192. * Reference: Testing Box Against Plane in Real-Time Collision Detection
  193. * by Christer Ericson (chapter 5.2.3)
  194. *
  195. * @param {Plane} plane
  196. * @returns {Boolean}
  197. */
  198. intersectsPlane( plane ) {
  199. this.rotation.extractBasis( xAxis, yAxis, zAxis );
  200. // compute the projection interval radius of this OBB onto L(t) = this->center + t * p.normal;
  201. const r = this.halfSize.x * Math.abs( plane.normal.dot( xAxis ) ) +
  202. this.halfSize.y * Math.abs( plane.normal.dot( yAxis ) ) +
  203. this.halfSize.z * Math.abs( plane.normal.dot( zAxis ) );
  204. // compute distance of the OBB's center from the plane
  205. const d = plane.normal.dot( this.center ) - plane.constant;
  206. // Intersection occurs when distance d falls within [-r,+r] interval
  207. return Math.abs( d ) <= r;
  208. }
  209. /**
  210. * Performs a ray/OBB intersection test and stores the intersection point
  211. * to the given 3D vector. If no intersection is detected, *null* is returned.
  212. *
  213. * @param {Ray} ray
  214. * @param {Vector3} result
  215. * @return {Vector3?}
  216. */
  217. intersectRay( ray, result ) {
  218. // the idea is to perform the intersection test in the local space
  219. // of the OBB.
  220. this.getSize( size );
  221. aabb.setFromCenterAndSize( v1.set( 0, 0, 0 ), size );
  222. // create a 4x4 transformation matrix
  223. matrix.setFromMatrix3( this.rotation );
  224. matrix.setPosition( this.center );
  225. // transform ray to the local space of the OBB
  226. inverse.copy( matrix ).invert();
  227. localRay.copy( ray ).applyMatrix4( inverse );
  228. // perform ray <-> AABB intersection test
  229. if ( localRay.intersectBox( aabb, result ) ) {
  230. // transform the intersection point back to world space
  231. return result.applyMatrix4( matrix );
  232. } else {
  233. return null;
  234. }
  235. }
  236. /**
  237. * Performs a ray/OBB intersection test. Returns either true or false if
  238. * there is a intersection or not.
  239. *
  240. * @param {Ray} ray
  241. * @returns {Boolean}
  242. */
  243. intersectsRay( ray ) {
  244. return this.intersectRay( ray, v1 ) !== null;
  245. }
  246. fromBox3( box3 ) {
  247. box3.getCenter( this.center );
  248. box3.getSize( this.halfSize ).multiplyScalar( 0.5 );
  249. this.rotation.identity();
  250. return this;
  251. }
  252. equals( obb ) {
  253. return obb.center.equals( this.center ) &&
  254. obb.halfSize.equals( this.halfSize ) &&
  255. obb.rotation.equals( this.rotation );
  256. }
  257. applyMatrix4( matrix ) {
  258. const e = matrix.elements;
  259. let sx = v1.set( e[ 0 ], e[ 1 ], e[ 2 ] ).length();
  260. const sy = v1.set( e[ 4 ], e[ 5 ], e[ 6 ] ).length();
  261. const sz = v1.set( e[ 8 ], e[ 9 ], e[ 10 ] ).length();
  262. const det = matrix.determinant();
  263. if ( det < 0 ) sx = - sx;
  264. rotationMatrix.setFromMatrix4( matrix );
  265. const invSX = 1 / sx;
  266. const invSY = 1 / sy;
  267. const invSZ = 1 / sz;
  268. rotationMatrix.elements[ 0 ] *= invSX;
  269. rotationMatrix.elements[ 1 ] *= invSX;
  270. rotationMatrix.elements[ 2 ] *= invSX;
  271. rotationMatrix.elements[ 3 ] *= invSY;
  272. rotationMatrix.elements[ 4 ] *= invSY;
  273. rotationMatrix.elements[ 5 ] *= invSY;
  274. rotationMatrix.elements[ 6 ] *= invSZ;
  275. rotationMatrix.elements[ 7 ] *= invSZ;
  276. rotationMatrix.elements[ 8 ] *= invSZ;
  277. this.rotation.multiply( rotationMatrix );
  278. this.halfSize.x *= sx;
  279. this.halfSize.y *= sy;
  280. this.halfSize.z *= sz;
  281. v1.setFromMatrixPosition( matrix );
  282. this.center.add( v1 );
  283. return this;
  284. }
  285. }
  286. const obb = new OBB();
  287. export { OBB };
粤ICP备19079148号