babylon.collider.ts 14 KB

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