Collisions.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /**
  2. * @author bartek drozdz / http://everyday3d.com/
  3. */
  4. THREE.PlaneCollider = function( point, normal ) {
  5. this.point = point;
  6. this.normal = normal;
  7. };
  8. THREE.SphereCollider = function( center, radius ) {
  9. this.center = center;
  10. this.radius = radius;
  11. this.radiusSq = radius * radius;
  12. };
  13. THREE.BoxCollider = function( min, max ) {
  14. this.min = min;
  15. this.max = max;
  16. this.dynamic = true;
  17. this.normal = new THREE.Vector3();
  18. };
  19. THREE.MeshCollider = function( mesh, box ) {
  20. this.mesh = mesh;
  21. this.box = box;
  22. this.numFaces = this.mesh.geometry.faces.length;
  23. this.normal = new THREE.Vector3();
  24. };
  25. THREE.CollisionSystem = function() {
  26. this.collisionNormal = new THREE.Vector3();
  27. this.colliders = [];
  28. this.hits = [];
  29. // console.log("Collision system init / 004");
  30. };
  31. THREE.Collisions = new THREE.CollisionSystem();
  32. THREE.CollisionSystem.prototype.merge = function( collisionSystem ) {
  33. this.colliders = this.colliders.concat( collisionSystem.colliders );
  34. this.hits = this.hits.concat( collisionSystem.hits );
  35. };
  36. THREE.CollisionSystem.prototype.rayCastAll = function( ray ) {
  37. ray.direction.normalize();
  38. this.hits.length = 0;
  39. var i, l, d, collider,
  40. ld = 0;
  41. for ( i = 0, l = this.colliders.length; i < l; i++ ) {
  42. collider = this.colliders[ i ];
  43. d = this.rayCast( ray, collider );
  44. if ( d < Number.MAX_VALUE ) {
  45. collider.distance = d;
  46. if ( d > ld )
  47. this.hits.push( collider );
  48. else
  49. this.hits.unshift( collider );
  50. ld = d;
  51. }
  52. }
  53. return this.hits;
  54. };
  55. THREE.CollisionSystem.prototype.rayCastNearest = function( ray ) {
  56. var cs = this.rayCastAll( ray );
  57. if( cs.length == 0 ) return null;
  58. var i = 0;
  59. while( cs[ i ] instanceof THREE.MeshCollider ) {
  60. var dist_index = this.rayMesh ( ray, cs[ i ] );
  61. if( dist_index.dist < Number.MAX_VALUE ) {
  62. cs[ i ].distance = dist_index.dist;
  63. cs[ i ].faceIndex = dist_index.faceIndex;
  64. break;
  65. }
  66. i++;
  67. }
  68. if ( i > cs.length ) return null;
  69. return cs[ i ];
  70. };
  71. THREE.CollisionSystem.prototype.rayCast = function( ray, collider ) {
  72. if ( collider instanceof THREE.PlaneCollider )
  73. return this.rayPlane( ray, collider );
  74. else if ( collider instanceof THREE.SphereCollider )
  75. return this.raySphere( ray, collider );
  76. else if ( collider instanceof THREE.BoxCollider )
  77. return this.rayBox( ray, collider );
  78. else if ( collider instanceof THREE.MeshCollider && collider.box )
  79. return this.rayBox( ray, collider.box );
  80. };
  81. THREE.CollisionSystem.prototype.rayMesh = function( r, me ) {
  82. var rt = this.makeRayLocal( r, me.mesh );
  83. var d = Number.MAX_VALUE;
  84. var nearestface;
  85. for( var i = 0; i < me.numFaces; i++ ) {
  86. var face = me.mesh.geometry.faces[i];
  87. var p0 = me.mesh.geometry.vertices[ face.a ].position;
  88. var p1 = me.mesh.geometry.vertices[ face.b ].position;
  89. var p2 = me.mesh.geometry.vertices[ face.c ].position;
  90. var p3 = face instanceof THREE.Face4 ? me.mesh.geometry.vertices[ face.d ].position : null;
  91. if (face instanceof THREE.Face3) {
  92. var nd = this.rayTriangle( rt, p0, p1, p2, d, this.collisionNormal, me.mesh );
  93. if( nd < d ) {
  94. d = nd;
  95. nearestface = i;
  96. me.normal.copy( this.collisionNormal );
  97. me.normal.normalize();
  98. }
  99. }
  100. else if (face instanceof THREE.Face4) {
  101. var nd = this.rayTriangle( rt, p0, p1, p3, d, this.collisionNormal, me.mesh );
  102. if( nd < d ) {
  103. d = nd;
  104. nearestface = i;
  105. me.normal.copy( this.collisionNormal );
  106. me.normal.normalize();
  107. }
  108. nd = this.rayTriangle( rt, p1, p2, p3, d, this.collisionNormal, me.mesh );
  109. if( nd < d ) {
  110. d = nd;
  111. nearestface = i;
  112. me.normal.copy( this.collisionNormal );
  113. me.normal.normalize();
  114. }
  115. }
  116. }
  117. return {dist: d, faceIndex: nearestface};
  118. };
  119. THREE.CollisionSystem.prototype.rayTriangle = function( ray, p0, p1, p2, mind, n, mesh ) {
  120. var e1 = THREE.CollisionSystem.__v1,
  121. e2 = THREE.CollisionSystem.__v2;
  122. n.set( 0, 0, 0 );
  123. // do not crash on quads, fail instead
  124. e1.sub( p1, p0 );
  125. e2.sub( p2, p1 );
  126. n.cross( e1, e2 )
  127. var dot = n.dot( ray.direction );
  128. if ( !( dot < 0 ) ) {
  129. if ( mesh.doubleSided || mesh.flipSided ) {
  130. n.multiplyScalar (-1.0);
  131. dot *= -1.0;
  132. } else {
  133. return Number.MAX_VALUE;
  134. }
  135. }
  136. var d = n.dot( p0 );
  137. var t = d - n.dot( ray.origin );
  138. if ( !( t <= 0 ) ) return Number.MAX_VALUE;
  139. if ( !( t >= dot * mind ) ) return Number.MAX_VALUE;
  140. t = t / dot;
  141. var p = THREE.CollisionSystem.__v3;
  142. p.copy( ray.direction );
  143. p.multiplyScalar( t );
  144. p.addSelf( ray.origin );
  145. var u0, u1, u2, v0, v1, v2;
  146. if ( Math.abs( n.x ) > Math.abs( n.y ) ) {
  147. if ( Math.abs( n.x ) > Math.abs( n.z ) ) {
  148. u0 = p.y - p0.y;
  149. u1 = p1.y - p0.y;
  150. u2 = p2.y - p0.y;
  151. v0 = p.z - p0.z;
  152. v1 = p1.z - p0.z;
  153. v2 = p2.z - p0.z;
  154. } else {
  155. u0 = p.x - p0.x;
  156. u1 = p1.x - p0.x;
  157. u2 = p2.x - p0.x;
  158. v0 = p.y - p0.y;
  159. v1 = p1.y - p0.y;
  160. v2 = p2.y - p0.y;
  161. }
  162. } else {
  163. if( Math.abs( n.y ) > Math.abs( n.z ) ) {
  164. u0 = p.x - p0.x;
  165. u1 = p1.x - p0.x;
  166. u2 = p2.x - p0.x;
  167. v0 = p.z - p0.z;
  168. v1 = p1.z - p0.z;
  169. v2 = p2.z - p0.z;
  170. } else {
  171. u0 = p.x - p0.x;
  172. u1 = p1.x - p0.x;
  173. u2 = p2.x - p0.x;
  174. v0 = p.y - p0.y;
  175. v1 = p1.y - p0.y;
  176. v2 = p2.y - p0.y;
  177. }
  178. }
  179. var temp = u1 * v2 - v1 * u2;
  180. if( !(temp != 0) ) return Number.MAX_VALUE;
  181. //console.log("temp: " + temp);
  182. temp = 1 / temp;
  183. var alpha = ( u0 * v2 - v0 * u2 ) * temp;
  184. if( !(alpha >= 0) ) return Number.MAX_VALUE;
  185. //console.log("alpha: " + alpha);
  186. var beta = ( u1 * v0 - v1 * u0 ) * temp;
  187. if( !(beta >= 0) ) return Number.MAX_VALUE;
  188. //console.log("beta: " + beta);
  189. var gamma = 1 - alpha - beta;
  190. if( !(gamma >= 0) ) return Number.MAX_VALUE;
  191. //console.log("gamma: " + gamma);
  192. return t;
  193. };
  194. THREE.CollisionSystem.prototype.makeRayLocal = function( ray, m ) {
  195. var mt = THREE.CollisionSystem.__m;
  196. THREE.Matrix4.makeInvert( m.matrixWorld, mt );
  197. var rt = THREE.CollisionSystem.__r;
  198. rt.origin.copy( ray.origin );
  199. rt.direction.copy( ray.direction );
  200. mt.multiplyVector3( rt.origin );
  201. mt.rotateAxis( rt.direction );
  202. rt.direction.normalize();
  203. //m.localRay = rt;
  204. return rt;
  205. };
  206. THREE.CollisionSystem.prototype.rayBox = function( ray, ab ) {
  207. var rt;
  208. if ( ab.dynamic && ab.mesh && ab.mesh.matrixWorld ) {
  209. rt = this.makeRayLocal( ray, ab.mesh );
  210. } else {
  211. rt = THREE.CollisionSystem.__r;
  212. rt.origin.copy( ray.origin );
  213. rt.direction.copy( ray.direction );
  214. }
  215. var xt = 0, yt = 0, zt = 0;
  216. var xn = 0, yn = 0, zn = 0;
  217. var ins = true;
  218. if( rt.origin.x < ab.min.x ) {
  219. xt = ab.min.x - rt.origin.x;
  220. //if(xt > ray.direction.x) return return Number.MAX_VALUE;
  221. xt /= rt.direction.x;
  222. ins = false;
  223. xn = -1;
  224. } else if( rt.origin.x > ab.max.x ) {
  225. xt = ab.max.x - rt.origin.x;
  226. //if(xt < ray.direction.x) return return Number.MAX_VALUE;
  227. xt /= rt.direction.x;
  228. ins = false;
  229. xn = 1;
  230. }
  231. if( rt.origin.y < ab.min.y ) {
  232. yt = ab.min.y - rt.origin.y;
  233. //if(yt > ray.direction.y) return return Number.MAX_VALUE;
  234. yt /= rt.direction.y;
  235. ins = false;
  236. yn = -1;
  237. } else if( rt.origin.y > ab.max.y ) {
  238. yt = ab.max.y - rt.origin.y;
  239. //if(yt < ray.direction.y) return return Number.MAX_VALUE;
  240. yt /= rt.direction.y;
  241. ins = false;
  242. yn = 1;
  243. }
  244. if( rt.origin.z < ab.min.z ) {
  245. zt = ab.min.z - rt.origin.z;
  246. //if(zt > ray.direction.z) return return Number.MAX_VALUE;
  247. zt /= rt.direction.z;
  248. ins = false;
  249. zn = -1;
  250. } else if( rt.origin.z > ab.max.z ) {
  251. zt = ab.max.z - rt.origin.z;
  252. //if(zt < ray.direction.z) return return Number.MAX_VALUE;
  253. zt /= rt.direction.z;
  254. ins = false;
  255. zn = 1;
  256. }
  257. if( ins ) return -1;
  258. var which = 0;
  259. var t = xt;
  260. if( yt > t ) {
  261. which = 1;
  262. t = yt;
  263. }
  264. if ( zt > t ) {
  265. which = 2;
  266. t = zt;
  267. }
  268. switch( which ) {
  269. case 0:
  270. var y = rt.origin.y + rt.direction.y * t;
  271. if ( y < ab.min.y || y > ab.max.y ) return Number.MAX_VALUE;
  272. var z = rt.origin.z + rt.direction.z * t;
  273. if ( z < ab.min.z || z > ab.max.z ) return Number.MAX_VALUE;
  274. ab.normal.set( xn, 0, 0 );
  275. break;
  276. case 1:
  277. var x = rt.origin.x + rt.direction.x * t;
  278. if ( x < ab.min.x || x > ab.max.x ) return Number.MAX_VALUE;
  279. var z = rt.origin.z + rt.direction.z * t;
  280. if ( z < ab.min.z || z > ab.max.z ) return Number.MAX_VALUE;
  281. ab.normal.set( 0, yn, 0) ;
  282. break;
  283. case 2:
  284. var x = rt.origin.x + rt.direction.x * t;
  285. if ( x < ab.min.x || x > ab.max.x ) return Number.MAX_VALUE;
  286. var y = rt.origin.y + rt.direction.y * t;
  287. if ( y < ab.min.y || y > ab.max.y ) return Number.MAX_VALUE;
  288. ab.normal.set( 0, 0, zn );
  289. break;
  290. }
  291. return t;
  292. };
  293. THREE.CollisionSystem.prototype.rayPlane = function( r, p ) {
  294. var t = r.direction.dot( p.normal );
  295. var d = p.point.dot( p.normal );
  296. var ds;
  297. if( t < 0 ) ds = ( d - r.origin.dot( p.normal ) ) / t;
  298. else return Number.MAX_VALUE;
  299. if( ds > 0 ) return ds;
  300. else return Number.MAX_VALUE;
  301. };
  302. THREE.CollisionSystem.prototype.raySphere = function( r, s ) {
  303. var e = s.center.clone().subSelf( r.origin );
  304. if ( e.lengthSq < s.radiusSq ) return -1;
  305. var a = e.dot( r.direction.clone() );
  306. if ( a <= 0 ) return Number.MAX_VALUE;
  307. var t = s.radiusSq - ( e.lengthSq() - a * a );
  308. if ( t >= 0 ) return Math.abs( a ) - Math.sqrt( t );
  309. return Number.MAX_VALUE;
  310. };
  311. THREE.CollisionSystem.__v1 = new THREE.Vector3();
  312. THREE.CollisionSystem.__v2 = new THREE.Vector3();
  313. THREE.CollisionSystem.__v3 = new THREE.Vector3();
  314. THREE.CollisionSystem.__nr = new THREE.Vector3();
  315. THREE.CollisionSystem.__m = new THREE.Matrix4();
  316. THREE.CollisionSystem.__r = new THREE.Ray();