babylon.collider.js 13 KB

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