babylon.collider.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. /** Define if a collision was found */
  49. public collisionFound: boolean;
  50. /**
  51. * Define last intersection point in local space
  52. */
  53. public intersectionPoint: Vector3;
  54. /**
  55. * Define last collided mesh
  56. */
  57. public collidedMesh: Nullable<AbstractMesh>;
  58. private _collisionPoint = Vector3.Zero();
  59. private _planeIntersectionPoint = Vector3.Zero();
  60. private _tempVector = Vector3.Zero();
  61. private _tempVector2 = Vector3.Zero();
  62. private _tempVector3 = Vector3.Zero();
  63. private _tempVector4 = Vector3.Zero();
  64. private _edge = Vector3.Zero();
  65. private _baseToVertex = Vector3.Zero();
  66. private _destinationPoint = Vector3.Zero();
  67. private _slidePlaneNormal = Vector3.Zero();
  68. private _displacementVector = Vector3.Zero();
  69. public _radius = Vector3.One();
  70. public _retry = 0;
  71. private _velocity: Vector3;
  72. private _basePoint: Vector3;
  73. private _epsilon: number;
  74. public _velocityWorldLength: number;
  75. public _basePointWorld = Vector3.Zero();
  76. private _velocityWorld = Vector3.Zero();
  77. private _normalizedVelocity = Vector3.Zero();
  78. public _initialVelocity: Vector3;
  79. public _initialPosition: Vector3;
  80. private _nearestDistance: number;
  81. private _collisionMask = -1;
  82. public get collisionMask(): number {
  83. return this._collisionMask;
  84. }
  85. public set collisionMask(mask: number) {
  86. this._collisionMask = !isNaN(mask) ? mask : -1;
  87. }
  88. /**
  89. * Gets the plane normal used to compute the sliding response (in local space)
  90. */
  91. public get slidePlaneNormal(): Vector3 {
  92. return this._slidePlaneNormal;
  93. }
  94. // Methods
  95. public _initialize(source: Vector3, dir: Vector3, e: number): void {
  96. this._velocity = dir;
  97. Vector3.NormalizeToRef(dir, this._normalizedVelocity);
  98. this._basePoint = source;
  99. source.multiplyToRef(this._radius, this._basePointWorld);
  100. dir.multiplyToRef(this._radius, this._velocityWorld);
  101. this._velocityWorldLength = this._velocityWorld.length();
  102. this._epsilon = e;
  103. this.collisionFound = false;
  104. }
  105. public _checkPointInTriangle(point: Vector3, pa: Vector3, pb: Vector3, pc: Vector3, n: Vector3): boolean {
  106. pa.subtractToRef(point, this._tempVector);
  107. pb.subtractToRef(point, this._tempVector2);
  108. Vector3.CrossToRef(this._tempVector, this._tempVector2, this._tempVector4);
  109. var d = Vector3.Dot(this._tempVector4, n);
  110. if (d < 0)
  111. return false;
  112. pc.subtractToRef(point, this._tempVector3);
  113. Vector3.CrossToRef(this._tempVector2, this._tempVector3, this._tempVector4);
  114. d = Vector3.Dot(this._tempVector4, n);
  115. if (d < 0)
  116. return false;
  117. Vector3.CrossToRef(this._tempVector3, this._tempVector, this._tempVector4);
  118. d = Vector3.Dot(this._tempVector4, n);
  119. return d >= 0;
  120. }
  121. public _canDoCollision(sphereCenter: Vector3, sphereRadius: number, vecMin: Vector3, vecMax: Vector3): boolean {
  122. var distance = Vector3.Distance(this._basePointWorld, sphereCenter);
  123. var max = Math.max(this._radius.x, this._radius.y, this._radius.z);
  124. if (distance > this._velocityWorldLength + max + sphereRadius) {
  125. return false;
  126. }
  127. if (!intersectBoxAASphere(vecMin, vecMax, this._basePointWorld, this._velocityWorldLength + max))
  128. return false;
  129. return true;
  130. }
  131. public _testTriangle(faceIndex: number, trianglePlaneArray: Array<Plane>, p1: Vector3, p2: Vector3, p3: Vector3, hasMaterial: boolean): void {
  132. var t0;
  133. var embeddedInPlane = false;
  134. //defensive programming, actually not needed.
  135. if (!trianglePlaneArray) {
  136. trianglePlaneArray = [];
  137. }
  138. if (!trianglePlaneArray[faceIndex]) {
  139. trianglePlaneArray[faceIndex] = new Plane(0, 0, 0, 0);
  140. trianglePlaneArray[faceIndex].copyFromPoints(p1, p2, p3);
  141. }
  142. var trianglePlane = trianglePlaneArray[faceIndex];
  143. if ((!hasMaterial) && !trianglePlane.isFrontFacingTo(this._normalizedVelocity, 0))
  144. return;
  145. var signedDistToTrianglePlane = trianglePlane.signedDistanceTo(this._basePoint);
  146. var normalDotVelocity = Vector3.Dot(trianglePlane.normal, this._velocity);
  147. if (normalDotVelocity == 0) {
  148. if (Math.abs(signedDistToTrianglePlane) >= 1.0)
  149. return;
  150. embeddedInPlane = true;
  151. t0 = 0;
  152. }
  153. else {
  154. t0 = (-1.0 - signedDistToTrianglePlane) / normalDotVelocity;
  155. var t1 = (1.0 - signedDistToTrianglePlane) / normalDotVelocity;
  156. if (t0 > t1) {
  157. var temp = t1;
  158. t1 = t0;
  159. t0 = temp;
  160. }
  161. if (t0 > 1.0 || t1 < 0.0)
  162. return;
  163. if (t0 < 0)
  164. t0 = 0;
  165. if (t0 > 1.0)
  166. t0 = 1.0;
  167. }
  168. this._collisionPoint.copyFromFloats(0, 0, 0);
  169. var found = false;
  170. var t = 1.0;
  171. if (!embeddedInPlane) {
  172. this._basePoint.subtractToRef(trianglePlane.normal, this._planeIntersectionPoint);
  173. this._velocity.scaleToRef(t0, this._tempVector);
  174. this._planeIntersectionPoint.addInPlace(this._tempVector);
  175. if (this._checkPointInTriangle(this._planeIntersectionPoint, p1, p2, p3, trianglePlane.normal)) {
  176. found = true;
  177. t = t0;
  178. this._collisionPoint.copyFrom(this._planeIntersectionPoint);
  179. }
  180. }
  181. if (!found) {
  182. var velocitySquaredLength = this._velocity.lengthSquared();
  183. var a = velocitySquaredLength;
  184. this._basePoint.subtractToRef(p1, this._tempVector);
  185. var b = 2.0 * (Vector3.Dot(this._velocity, this._tempVector));
  186. var c = this._tempVector.lengthSquared() - 1.0;
  187. var lowestRoot = getLowestRoot(a, b, c, t);
  188. if (lowestRoot.found) {
  189. t = lowestRoot.root;
  190. found = true;
  191. this._collisionPoint.copyFrom(p1);
  192. }
  193. this._basePoint.subtractToRef(p2, this._tempVector);
  194. b = 2.0 * (Vector3.Dot(this._velocity, this._tempVector));
  195. c = this._tempVector.lengthSquared() - 1.0;
  196. lowestRoot = getLowestRoot(a, b, c, t);
  197. if (lowestRoot.found) {
  198. t = lowestRoot.root;
  199. found = true;
  200. this._collisionPoint.copyFrom(p2);
  201. }
  202. this._basePoint.subtractToRef(p3, this._tempVector);
  203. b = 2.0 * (Vector3.Dot(this._velocity, this._tempVector));
  204. c = this._tempVector.lengthSquared() - 1.0;
  205. lowestRoot = getLowestRoot(a, b, c, t);
  206. if (lowestRoot.found) {
  207. t = lowestRoot.root;
  208. found = true;
  209. this._collisionPoint.copyFrom(p3);
  210. }
  211. p2.subtractToRef(p1, this._edge);
  212. p1.subtractToRef(this._basePoint, this._baseToVertex);
  213. var edgeSquaredLength = this._edge.lengthSquared();
  214. var edgeDotVelocity = Vector3.Dot(this._edge, this._velocity);
  215. var edgeDotBaseToVertex = Vector3.Dot(this._edge, this._baseToVertex);
  216. a = edgeSquaredLength * (-velocitySquaredLength) + edgeDotVelocity * edgeDotVelocity;
  217. b = edgeSquaredLength * (2.0 * Vector3.Dot(this._velocity, this._baseToVertex)) - 2.0 * edgeDotVelocity * edgeDotBaseToVertex;
  218. c = edgeSquaredLength * (1.0 - this._baseToVertex.lengthSquared()) + edgeDotBaseToVertex * edgeDotBaseToVertex;
  219. lowestRoot = getLowestRoot(a, b, c, t);
  220. if (lowestRoot.found) {
  221. var f = (edgeDotVelocity * lowestRoot.root - edgeDotBaseToVertex) / edgeSquaredLength;
  222. if (f >= 0.0 && f <= 1.0) {
  223. t = lowestRoot.root;
  224. found = true;
  225. this._edge.scaleInPlace(f);
  226. p1.addToRef(this._edge, this._collisionPoint);
  227. }
  228. }
  229. p3.subtractToRef(p2, this._edge);
  230. p2.subtractToRef(this._basePoint, this._baseToVertex);
  231. edgeSquaredLength = this._edge.lengthSquared();
  232. edgeDotVelocity = Vector3.Dot(this._edge, this._velocity);
  233. edgeDotBaseToVertex = Vector3.Dot(this._edge, this._baseToVertex);
  234. a = edgeSquaredLength * (-velocitySquaredLength) + edgeDotVelocity * edgeDotVelocity;
  235. b = edgeSquaredLength * (2.0 * Vector3.Dot(this._velocity, this._baseToVertex)) - 2.0 * edgeDotVelocity * edgeDotBaseToVertex;
  236. c = edgeSquaredLength * (1.0 - this._baseToVertex.lengthSquared()) + edgeDotBaseToVertex * edgeDotBaseToVertex;
  237. lowestRoot = getLowestRoot(a, b, c, t);
  238. if (lowestRoot.found) {
  239. f = (edgeDotVelocity * lowestRoot.root - edgeDotBaseToVertex) / edgeSquaredLength;
  240. if (f >= 0.0 && f <= 1.0) {
  241. t = lowestRoot.root;
  242. found = true;
  243. this._edge.scaleInPlace(f);
  244. p2.addToRef(this._edge, this._collisionPoint);
  245. }
  246. }
  247. p1.subtractToRef(p3, this._edge);
  248. p3.subtractToRef(this._basePoint, this._baseToVertex);
  249. edgeSquaredLength = this._edge.lengthSquared();
  250. edgeDotVelocity = Vector3.Dot(this._edge, this._velocity);
  251. edgeDotBaseToVertex = Vector3.Dot(this._edge, this._baseToVertex);
  252. a = edgeSquaredLength * (-velocitySquaredLength) + edgeDotVelocity * edgeDotVelocity;
  253. b = edgeSquaredLength * (2.0 * Vector3.Dot(this._velocity, this._baseToVertex)) - 2.0 * edgeDotVelocity * edgeDotBaseToVertex;
  254. c = edgeSquaredLength * (1.0 - this._baseToVertex.lengthSquared()) + edgeDotBaseToVertex * edgeDotBaseToVertex;
  255. lowestRoot = getLowestRoot(a, b, c, t);
  256. if (lowestRoot.found) {
  257. f = (edgeDotVelocity * lowestRoot.root - edgeDotBaseToVertex) / edgeSquaredLength;
  258. if (f >= 0.0 && f <= 1.0) {
  259. t = lowestRoot.root;
  260. found = true;
  261. this._edge.scaleInPlace(f);
  262. p3.addToRef(this._edge, this._collisionPoint);
  263. }
  264. }
  265. }
  266. if (found) {
  267. var distToCollision = t * this._velocity.length();
  268. if (!this.collisionFound || distToCollision < this._nearestDistance) {
  269. if (!this.intersectionPoint) {
  270. this.intersectionPoint = this._collisionPoint.clone();
  271. } else {
  272. this.intersectionPoint.copyFrom(this._collisionPoint);
  273. }
  274. this._nearestDistance = distToCollision;
  275. this.collisionFound = true;
  276. }
  277. }
  278. }
  279. public _collide(trianglePlaneArray: Array<Plane>, pts: Vector3[], indices: IndicesArray, indexStart: number, indexEnd: number, decal: number, hasMaterial: boolean): void {
  280. for (var i = indexStart; i < indexEnd; i += 3) {
  281. var p1 = pts[indices[i] - decal];
  282. var p2 = pts[indices[i + 1] - decal];
  283. var p3 = pts[indices[i + 2] - decal];
  284. this._testTriangle(i, trianglePlaneArray, p3, p2, p1, hasMaterial);
  285. }
  286. }
  287. public _getResponse(pos: Vector3, vel: Vector3): void {
  288. pos.addToRef(vel, this._destinationPoint);
  289. vel.scaleInPlace((this._nearestDistance / vel.length()));
  290. this._basePoint.addToRef(vel, pos);
  291. pos.subtractToRef(this.intersectionPoint, this._slidePlaneNormal);
  292. this._slidePlaneNormal.normalize();
  293. this._slidePlaneNormal.scaleToRef(this._epsilon, this._displacementVector);
  294. pos.addInPlace(this._displacementVector);
  295. this.intersectionPoint.addInPlace(this._displacementVector);
  296. this._slidePlaneNormal.scaleInPlace(Plane.SignedDistanceToPlaneFromPositionAndNormal(this.intersectionPoint, this._slidePlaneNormal, this._destinationPoint));
  297. this._destinationPoint.subtractInPlace(this._slidePlaneNormal);
  298. this._destinationPoint.subtractToRef(this.intersectionPoint, vel);
  299. }
  300. }
  301. }