babylon.collider.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. }
  120. else {
  121. t0 = (-1.0 - signedDistToTrianglePlane) / normalDotVelocity;
  122. var t1 = (1.0 - signedDistToTrianglePlane) / normalDotVelocity;
  123. if (t0 > t1) {
  124. var temp = t1;
  125. t1 = t0;
  126. t0 = temp;
  127. }
  128. if (t0 > 1.0 || t1 < 0.0)
  129. return;
  130. if (t0 < 0)
  131. t0 = 0;
  132. if (t0 > 1.0)
  133. t0 = 1.0;
  134. }
  135. this._collisionPoint.copyFromFloats(0, 0, 0);
  136. var found = false;
  137. var t = 1.0;
  138. if (!embeddedInPlane) {
  139. this.basePoint.subtractToRef(trianglePlane.normal, this._planeIntersectionPoint);
  140. this.velocity.scaleToRef(t0, this._tempVector);
  141. this._planeIntersectionPoint.addInPlace(this._tempVector);
  142. if (this._checkPointInTriangle(this._planeIntersectionPoint, p1, p2, p3, trianglePlane.normal)) {
  143. found = true;
  144. t = t0;
  145. this._collisionPoint.copyFrom(this._planeIntersectionPoint);
  146. }
  147. }
  148. if (!found) {
  149. var velocitySquaredLength = this.velocity.lengthSquared();
  150. var a = velocitySquaredLength;
  151. this.basePoint.subtractToRef(p1, this._tempVector);
  152. var b = 2.0 * (BABYLON.Vector3.Dot(this.velocity, this._tempVector));
  153. var c = this._tempVector.lengthSquared() - 1.0;
  154. var lowestRoot = getLowestRoot(a, b, c, t);
  155. if (lowestRoot.found) {
  156. t = lowestRoot.root;
  157. found = true;
  158. this._collisionPoint.copyFrom(p1);
  159. }
  160. this.basePoint.subtractToRef(p2, this._tempVector);
  161. b = 2.0 * (BABYLON.Vector3.Dot(this.velocity, this._tempVector));
  162. c = this._tempVector.lengthSquared() - 1.0;
  163. lowestRoot = getLowestRoot(a, b, c, t);
  164. if (lowestRoot.found) {
  165. t = lowestRoot.root;
  166. found = true;
  167. this._collisionPoint.copyFrom(p2);
  168. }
  169. this.basePoint.subtractToRef(p3, this._tempVector);
  170. b = 2.0 * (BABYLON.Vector3.Dot(this.velocity, this._tempVector));
  171. c = this._tempVector.lengthSquared() - 1.0;
  172. lowestRoot = getLowestRoot(a, b, c, t);
  173. if (lowestRoot.found) {
  174. t = lowestRoot.root;
  175. found = true;
  176. this._collisionPoint.copyFrom(p3);
  177. }
  178. p2.subtractToRef(p1, this._edge);
  179. p1.subtractToRef(this.basePoint, this._baseToVertex);
  180. var edgeSquaredLength = this._edge.lengthSquared();
  181. var edgeDotVelocity = BABYLON.Vector3.Dot(this._edge, this.velocity);
  182. var edgeDotBaseToVertex = BABYLON.Vector3.Dot(this._edge, this._baseToVertex);
  183. a = edgeSquaredLength * (-velocitySquaredLength) + edgeDotVelocity * edgeDotVelocity;
  184. b = edgeSquaredLength * (2.0 * BABYLON.Vector3.Dot(this.velocity, this._baseToVertex)) - 2.0 * edgeDotVelocity * edgeDotBaseToVertex;
  185. c = edgeSquaredLength * (1.0 - this._baseToVertex.lengthSquared()) + edgeDotBaseToVertex * edgeDotBaseToVertex;
  186. lowestRoot = getLowestRoot(a, b, c, t);
  187. if (lowestRoot.found) {
  188. var f = (edgeDotVelocity * lowestRoot.root - edgeDotBaseToVertex) / edgeSquaredLength;
  189. if (f >= 0.0 && f <= 1.0) {
  190. t = lowestRoot.root;
  191. found = true;
  192. this._edge.scaleInPlace(f);
  193. p1.addToRef(this._edge, this._collisionPoint);
  194. }
  195. }
  196. p3.subtractToRef(p2, this._edge);
  197. p2.subtractToRef(this.basePoint, this._baseToVertex);
  198. edgeSquaredLength = this._edge.lengthSquared();
  199. edgeDotVelocity = BABYLON.Vector3.Dot(this._edge, this.velocity);
  200. edgeDotBaseToVertex = BABYLON.Vector3.Dot(this._edge, this._baseToVertex);
  201. a = edgeSquaredLength * (-velocitySquaredLength) + edgeDotVelocity * edgeDotVelocity;
  202. b = edgeSquaredLength * (2.0 * BABYLON.Vector3.Dot(this.velocity, this._baseToVertex)) - 2.0 * edgeDotVelocity * edgeDotBaseToVertex;
  203. c = edgeSquaredLength * (1.0 - this._baseToVertex.lengthSquared()) + edgeDotBaseToVertex * edgeDotBaseToVertex;
  204. lowestRoot = getLowestRoot(a, b, c, t);
  205. if (lowestRoot.found) {
  206. f = (edgeDotVelocity * lowestRoot.root - edgeDotBaseToVertex) / edgeSquaredLength;
  207. if (f >= 0.0 && f <= 1.0) {
  208. t = lowestRoot.root;
  209. found = true;
  210. this._edge.scaleInPlace(f);
  211. p2.addToRef(this._edge, this._collisionPoint);
  212. }
  213. }
  214. p1.subtractToRef(p3, this._edge);
  215. p3.subtractToRef(this.basePoint, this._baseToVertex);
  216. edgeSquaredLength = this._edge.lengthSquared();
  217. edgeDotVelocity = BABYLON.Vector3.Dot(this._edge, this.velocity);
  218. edgeDotBaseToVertex = BABYLON.Vector3.Dot(this._edge, this._baseToVertex);
  219. a = edgeSquaredLength * (-velocitySquaredLength) + edgeDotVelocity * edgeDotVelocity;
  220. b = edgeSquaredLength * (2.0 * BABYLON.Vector3.Dot(this.velocity, this._baseToVertex)) - 2.0 * edgeDotVelocity * edgeDotBaseToVertex;
  221. c = edgeSquaredLength * (1.0 - this._baseToVertex.lengthSquared()) + edgeDotBaseToVertex * edgeDotBaseToVertex;
  222. lowestRoot = getLowestRoot(a, b, c, t);
  223. if (lowestRoot.found) {
  224. f = (edgeDotVelocity * lowestRoot.root - edgeDotBaseToVertex) / edgeSquaredLength;
  225. if (f >= 0.0 && f <= 1.0) {
  226. t = lowestRoot.root;
  227. found = true;
  228. this._edge.scaleInPlace(f);
  229. p3.addToRef(this._edge, this._collisionPoint);
  230. }
  231. }
  232. }
  233. if (found) {
  234. var distToCollision = t * this.velocity.length();
  235. if (!this.collisionFound || distToCollision < this.nearestDistance) {
  236. if (!this.intersectionPoint) {
  237. this.intersectionPoint = this._collisionPoint.clone();
  238. }
  239. else {
  240. this.intersectionPoint.copyFrom(this._collisionPoint);
  241. }
  242. this.nearestDistance = distToCollision;
  243. this.collisionFound = true;
  244. this.collidedMesh = subMesh.getMesh();
  245. }
  246. }
  247. };
  248. Collider.prototype._collide = function (subMesh, pts, indices, indexStart, indexEnd, decal) {
  249. for (var i = indexStart; i < indexEnd; i += 3) {
  250. var p1 = pts[indices[i] - decal];
  251. var p2 = pts[indices[i + 1] - decal];
  252. var p3 = pts[indices[i + 2] - decal];
  253. this._testTriangle(i, subMesh, p3, p2, p1);
  254. }
  255. };
  256. Collider.prototype._getResponse = function (pos, vel) {
  257. pos.addToRef(vel, this._destinationPoint);
  258. vel.scaleInPlace((this.nearestDistance / vel.length()));
  259. this.basePoint.addToRef(vel, pos);
  260. pos.subtractToRef(this.intersectionPoint, this._slidePlaneNormal);
  261. this._slidePlaneNormal.normalize();
  262. this._slidePlaneNormal.scaleToRef(this.epsilon, this._displacementVector);
  263. pos.addInPlace(this._displacementVector);
  264. this.intersectionPoint.addInPlace(this._displacementVector);
  265. this._slidePlaneNormal.scaleInPlace(BABYLON.Plane.SignedDistanceToPlaneFromPositionAndNormal(this.intersectionPoint, this._slidePlaneNormal, this._destinationPoint));
  266. this._destinationPoint.subtractInPlace(this._slidePlaneNormal);
  267. this._destinationPoint.subtractToRef(this.intersectionPoint, vel);
  268. };
  269. return Collider;
  270. })();
  271. BABYLON.Collider = Collider;
  272. })(BABYLON || (BABYLON = {}));
  273. //# sourceMappingURL=babylon.collider.js.map