babylon.collider.ts 14 KB

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