babylon.collider.js 13 KB

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