babylon.collider.ts 14 KB

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