babylon.collider.js 14 KB

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