babylon.collider.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.Collider = function () {
  4. this.radius = new BABYLON.Vector3(1, 1, 1);
  5. this.retry = 0;
  6. this.basePointWorld = BABYLON.Vector3.Zero();
  7. this.velocityWorld = BABYLON.Vector3.Zero();
  8. this.normalizedVelocity = BABYLON.Vector3.Zero();
  9. // Internals
  10. this._collisionPoint = BABYLON.Vector3.Zero();
  11. this._planeIntersectionPoint = BABYLON.Vector3.Zero();
  12. this._tempVector = BABYLON.Vector3.Zero();
  13. this._tempVector2 = BABYLON.Vector3.Zero();
  14. this._tempVector3 = BABYLON.Vector3.Zero();
  15. this._tempVector4 = BABYLON.Vector3.Zero();
  16. this._edge = BABYLON.Vector3.Zero();
  17. this._baseToVertex = BABYLON.Vector3.Zero();
  18. this._destinationPoint = BABYLON.Vector3.Zero();
  19. this._slidePlaneNormal = BABYLON.Vector3.Zero();
  20. this._displacementVector = BABYLON.Vector3.Zero();
  21. };
  22. // Methods
  23. BABYLON.Collider.prototype._initialize = function (source, dir, e) {
  24. this.velocity = dir;
  25. BABYLON.Vector3.NormalizeToRef(dir, this.normalizedVelocity);
  26. this.basePoint = source;
  27. source.multiplyToRef(this.radius, this.basePointWorld);
  28. dir.multiplyToRef(this.radius, this.velocityWorld);
  29. this.velocityWorldLength = this.velocityWorld.length();
  30. this.epsilon = e;
  31. this.collisionFound = false;
  32. };
  33. BABYLON.Collider.prototype._checkPointInTriangle = function (point, pa, pb, pc, n) {
  34. pa.subtractToRef(point, this._tempVector);
  35. pb.subtractToRef(point, this._tempVector2);
  36. BABYLON.Vector3.CrossToRef(this._tempVector, this._tempVector2, this._tempVector4);
  37. var d = BABYLON.Vector3.Dot(this._tempVector4, n);
  38. if (d < 0)
  39. return false;
  40. pc.subtractToRef(point, this._tempVector3);
  41. BABYLON.Vector3.CrossToRef(this._tempVector2, this._tempVector3, this._tempVector4);
  42. d = BABYLON.Vector3.Dot(this._tempVector4, n);
  43. if (d < 0)
  44. return false;
  45. BABYLON.Vector3.CrossToRef(this._tempVector3, this._tempVector, this._tempVector4);
  46. d = BABYLON.Vector3.Dot(this._tempVector4, n);
  47. return d >= 0;
  48. };
  49. var intersectBoxAASphere = function (boxMin, boxMax, sphereCenter, sphereRadius) {
  50. if (boxMin.x > sphereCenter.x + sphereRadius)
  51. return false;
  52. if (sphereCenter.x - sphereRadius > boxMax.x)
  53. return false;
  54. if (boxMin.y > sphereCenter.y + sphereRadius)
  55. return false;
  56. if (sphereCenter.y - sphereRadius > boxMax.y)
  57. return false;
  58. if (boxMin.z > sphereCenter.z + sphereRadius)
  59. return false;
  60. if (sphereCenter.z - sphereRadius > boxMax.z)
  61. return false;
  62. return true;
  63. };
  64. var getLowestRoot = function (a, b, c, maxR) {
  65. var determinant = b * b - 4.0 * a * c;
  66. var result = { root: 0, found: false };
  67. if (determinant < 0)
  68. return result;
  69. var sqrtD = Math.sqrt(determinant);
  70. var r1 = (-b - sqrtD) / (2.0 * a);
  71. var r2 = (-b + sqrtD) / (2.0 * a);
  72. if (r1 > r2) {
  73. var temp = r2;
  74. r2 = r1;
  75. r1 = temp;
  76. }
  77. if (r1 > 0 && r1 < maxR) {
  78. result.root = r1;
  79. result.found = true;
  80. return result;
  81. }
  82. if (r2 > 0 && r2 < maxR) {
  83. result.root = r2;
  84. result.found = true;
  85. return result;
  86. }
  87. return result;
  88. };
  89. BABYLON.Collider.prototype._canDoCollision = function (sphereCenter, sphereRadius, vecMin, vecMax) {
  90. var distance = BABYLON.Vector3.Distance(this.basePointWorld, sphereCenter);
  91. var max = Math.max(this.radius.x, this.radius.y);
  92. max = Math.max(max, this.radius.z);
  93. if (distance > this.velocityWorldLength + max + sphereRadius) {
  94. return false;
  95. }
  96. if (!intersectBoxAASphere(vecMin, vecMax, this.basePointWorld, this.velocityWorldLength + max))
  97. return false;
  98. return true;
  99. };
  100. BABYLON.Collider.prototype._testTriangle = function (faceIndex, subMesh, p1, p2, p3) {
  101. var t0;
  102. var embeddedInPlane = false;
  103. if (!subMesh._trianglePlanes) {
  104. subMesh._trianglePlanes = [];
  105. }
  106. if (!subMesh._trianglePlanes[faceIndex]) {
  107. subMesh._trianglePlanes[faceIndex] = new BABYLON.Plane(0, 0, 0, 0);
  108. subMesh._trianglePlanes[faceIndex].copyFromPoints(p1, p2, p3);
  109. }
  110. var trianglePlane = subMesh._trianglePlanes[faceIndex];
  111. if ((!subMesh.getMaterial()) && !trianglePlane.isFrontFacingTo(this.normalizedVelocity, 0))
  112. return;
  113. var signedDistToTrianglePlane = trianglePlane.signedDistanceTo(this.basePoint);
  114. var normalDotVelocity = BABYLON.Vector3.Dot(trianglePlane.normal, this.velocity);
  115. if (normalDotVelocity == 0) {
  116. if (Math.abs(signedDistToTrianglePlane) >= 1.0)
  117. return;
  118. embeddedInPlane = true;
  119. t0 = 0;
  120. }
  121. else {
  122. t0 = (-1.0 - signedDistToTrianglePlane) / normalDotVelocity;
  123. var t1 = (1.0 - signedDistToTrianglePlane) / normalDotVelocity;
  124. if (t0 > t1) {
  125. var temp = t1;
  126. t1 = t0;
  127. t0 = temp;
  128. }
  129. if (t0 > 1.0 || t1 < 0.0)
  130. return;
  131. if (t0 < 0)
  132. t0 = 0;
  133. if (t0 > 1.0)
  134. t0 = 1.0;
  135. }
  136. this._collisionPoint.copyFromFloats(0, 0, 0);
  137. var found = false;
  138. var t = 1.0;
  139. if (!embeddedInPlane) {
  140. this.basePoint.subtractToRef(trianglePlane.normal, this._planeIntersectionPoint);
  141. this.velocity.scaleToRef(t0, this._tempVector);
  142. this._planeIntersectionPoint.addInPlace(this._tempVector);
  143. if (this._checkPointInTriangle(this._planeIntersectionPoint, p1, p2, p3, trianglePlane.normal)) {
  144. found = true;
  145. t = t0;
  146. this._collisionPoint.copyFrom(this._planeIntersectionPoint);
  147. }
  148. }
  149. if (!found) {
  150. var velocitySquaredLength = this.velocity.lengthSquared();
  151. var a = velocitySquaredLength;
  152. this.basePoint.subtractToRef(p1, this._tempVector);
  153. var b = 2.0 * (BABYLON.Vector3.Dot(this.velocity, this._tempVector));
  154. var c = this._tempVector.lengthSquared - 1.0;
  155. var lowestRoot = getLowestRoot(a, b, c, t);
  156. if (lowestRoot.found) {
  157. t = lowestRoot.root;
  158. found = true;
  159. this._collisionPoint.copyFrom(p1);
  160. }
  161. this.basePoint.subtractToRef(p2, this._tempVector);
  162. b = 2.0 * (BABYLON.Vector3.Dot(this.velocity, this._tempVector));
  163. c = this._tempVector.lengthSquared - 1.0;
  164. lowestRoot = getLowestRoot(a, b, c, t);
  165. if (lowestRoot.found) {
  166. t = lowestRoot.root;
  167. found = true;
  168. this._collisionPoint.copyFrom(p2);
  169. }
  170. this.basePoint.subtractToRef(p3, this._tempVector);
  171. b = 2.0 * (BABYLON.Vector3.Dot(this.velocity, this._tempVector));
  172. c = this._tempVector.lengthSquared - 1.0;
  173. lowestRoot = getLowestRoot(a, b, c, t);
  174. if (lowestRoot.found) {
  175. t = lowestRoot.root;
  176. found = true;
  177. this._collisionPoint.copyFrom(p3);
  178. }
  179. p2.subtractToRef(p1, this._edge);
  180. p1.subtractToRef(this.basePoint, this._baseToVertex);
  181. var edgeSquaredLength = this._edge.lengthSquared();
  182. var edgeDotVelocity = BABYLON.Vector3.Dot(this._edge, this.velocity);
  183. var edgeDotBaseToVertex = BABYLON.Vector3.Dot(this._edge, this._baseToVertex);
  184. a = edgeSquaredLength * (-velocitySquaredLength) + edgeDotVelocity * edgeDotVelocity;
  185. b = edgeSquaredLength * (2.0 * BABYLON.Vector3.Dot(this.velocity, this._baseToVertex)) - 2.0 * edgeDotVelocity * edgeDotBaseToVertex;
  186. c = edgeSquaredLength * (1.0 - this._baseToVertex.lengthSquared()) + edgeDotBaseToVertex * edgeDotBaseToVertex;
  187. lowestRoot = getLowestRoot(a, b, c, t);
  188. if (lowestRoot.found) {
  189. var f = (edgeDotVelocity * lowestRoot.root - edgeDotBaseToVertex) / edgeSquaredLength;
  190. if (f >= 0.0 && f <= 1.0) {
  191. t = lowestRoot.root;
  192. found = true;
  193. this._edge.scaleInPlace(f);
  194. p1.addToRef(this._edge, this._collisionPoint);
  195. }
  196. }
  197. p3.subtractToRef(p2, this._edge);
  198. p2.subtractToRef(this.basePoint, this._baseToVertex);
  199. edgeSquaredLength = this._edge.lengthSquared();
  200. edgeDotVelocity = BABYLON.Vector3.Dot(this._edge, this.velocity);
  201. edgeDotBaseToVertex = BABYLON.Vector3.Dot(this._edge, this._baseToVertex);
  202. a = edgeSquaredLength * (-velocitySquaredLength) + edgeDotVelocity * edgeDotVelocity;
  203. b = edgeSquaredLength * (2.0 * BABYLON.Vector3.Dot(this.velocity, this._baseToVertex)) - 2.0 * edgeDotVelocity * edgeDotBaseToVertex;
  204. c = edgeSquaredLength * (1.0 - this._baseToVertex.lengthSquared()) + edgeDotBaseToVertex * edgeDotBaseToVertex;
  205. lowestRoot = getLowestRoot(a, b, c, t);
  206. if (lowestRoot.found) {
  207. var f = (edgeDotVelocity * lowestRoot.root - edgeDotBaseToVertex) / edgeSquaredLength;
  208. if (f >= 0.0 && f <= 1.0) {
  209. t = lowestRoot.root;
  210. found = true;
  211. this._edge.scaleInPlace(f);
  212. p2.addToRef(this._edge, this._collisionPoint);
  213. }
  214. }
  215. p1.subtractToRef(p3, this._edge);
  216. p3.subtractToRef(this.basePoint, this._baseToVertex);
  217. edgeSquaredLength = this._edge.lengthSquared();
  218. edgeDotVelocity = BABYLON.Vector3.Dot(this._edge, this.velocity);
  219. edgeDotBaseToVertex = BABYLON.Vector3.Dot(this._edge, this._baseToVertex);
  220. a = edgeSquaredLength * (-velocitySquaredLength) + edgeDotVelocity * edgeDotVelocity;
  221. b = edgeSquaredLength * (2.0 * BABYLON.Vector3.Dot(this.velocity, this._baseToVertex)) - 2.0 * edgeDotVelocity * edgeDotBaseToVertex;
  222. c = edgeSquaredLength * (1.0 - this._baseToVertex.lengthSquared()) + edgeDotBaseToVertex * edgeDotBaseToVertex;
  223. lowestRoot = getLowestRoot(a, b, c, t);
  224. if (lowestRoot.found) {
  225. var f = (edgeDotVelocity * lowestRoot.root - edgeDotBaseToVertex) / edgeSquaredLength;
  226. if (f >= 0.0 && f <= 1.0) {
  227. t = lowestRoot.root;
  228. found = true;
  229. this._edge.scaleInPlace(f);
  230. p3.addToRef(this._edge, this._collisionPoint);
  231. }
  232. }
  233. }
  234. if (found) {
  235. var distToCollision = t * this.velocity.length();
  236. if (!this.collisionFound || distToCollision < this.nearestDistance) {
  237. if (!this.intersectionPoint) {
  238. this.intersectionPoint = this._collisionPoint.clone();
  239. } else {
  240. this.intersectionPoint.copyFrom(this._collisionPoint);
  241. }
  242. this.nearestDistance = distToCollision;
  243. this.collisionFound = true;
  244. }
  245. }
  246. };
  247. BABYLON.Collider.prototype._collide = function (subMesh, pts, indices, indexStart, indexEnd, decal) {
  248. for (var i = indexStart; i < indexEnd; i += 3) {
  249. var p1 = pts[indices[i] - decal];
  250. var p2 = pts[indices[i + 1] - decal];
  251. var p3 = pts[indices[i + 2] - decal];
  252. this._testTriangle(i, subMesh, p3, p2, p1);
  253. }
  254. };
  255. BABYLON.Collider.prototype._getResponse = function(pos, vel) {
  256. pos.addToRef(vel, this._destinationPoint);
  257. vel.scaleInPlace((this.nearestDistance / vel.length()));
  258. this.basePoint.addToRef(vel, pos);
  259. pos.subtractToRef(this.intersectionPoint, this._slidePlaneNormal);
  260. this._slidePlaneNormal.normalize();
  261. this._slidePlaneNormal.scaleToRef(this.epsilon, this._displacementVector);
  262. pos.addInPlace(this._displacementVector);
  263. this.intersectionPoint.addInPlace(this._displacementVector);
  264. this._slidePlaneNormal.scaleInPlace(BABYLON.Plane.SignedDistanceToPlaneFromPositionAndNormal(this.intersectionPoint, this._slidePlaneNormal, this._destinationPoint));
  265. this._destinationPoint.subtractInPlace(this._slidePlaneNormal);
  266. this._destinationPoint.subtractToRef(this.intersectionPoint, vel);
  267. };
  268. })();