babylon.collider.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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, trianglePlaneArray, p1, p2, p3, hasMaterial) {
  100. var t0;
  101. var embeddedInPlane = false;
  102. //defensive programming, actually not needed.
  103. if (!trianglePlaneArray) {
  104. trianglePlaneArray = [];
  105. }
  106. if (!trianglePlaneArray[faceIndex]) {
  107. trianglePlaneArray[faceIndex] = new BABYLON.Plane(0, 0, 0, 0);
  108. trianglePlaneArray[faceIndex].copyFromPoints(p1, p2, p3);
  109. }
  110. var trianglePlane = trianglePlaneArray[faceIndex];
  111. if ((!hasMaterial) && !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. 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. 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. }
  240. else {
  241. this.intersectionPoint.copyFrom(this._collisionPoint);
  242. }
  243. this.nearestDistance = distToCollision;
  244. this.collisionFound = true;
  245. }
  246. }
  247. };
  248. Collider.prototype._collide = function (trianglePlaneArray, pts, indices, indexStart, indexEnd, decal, hasMaterial) {
  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, trianglePlaneArray, p3, p2, p1, hasMaterial);
  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 = {}));