babylon.collider.js 14 KB

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