collider.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. import { Vector3 } from "../Maths/math.vector.js";
  2. import { Plane } from "../Maths/math.plane.js";
  3. const intersectBoxAASphere = (boxMin, boxMax, sphereCenter, sphereRadius) => {
  4. if (boxMin.x > sphereCenter.x + sphereRadius) {
  5. return false;
  6. }
  7. if (sphereCenter.x - sphereRadius > boxMax.x) {
  8. return false;
  9. }
  10. if (boxMin.y > sphereCenter.y + sphereRadius) {
  11. return false;
  12. }
  13. if (sphereCenter.y - sphereRadius > boxMax.y) {
  14. return false;
  15. }
  16. if (boxMin.z > sphereCenter.z + sphereRadius) {
  17. return false;
  18. }
  19. if (sphereCenter.z - sphereRadius > boxMax.z) {
  20. return false;
  21. }
  22. return true;
  23. };
  24. const getLowestRoot = (function () {
  25. const result = { root: 0, found: false };
  26. return function (a, b, c, maxR) {
  27. result.root = 0;
  28. result.found = false;
  29. const determinant = b * b - 4.0 * a * c;
  30. if (determinant < 0) {
  31. return result;
  32. }
  33. const sqrtD = Math.sqrt(determinant);
  34. let r1 = (-b - sqrtD) / (2.0 * a);
  35. let r2 = (-b + sqrtD) / (2.0 * a);
  36. if (r1 > r2) {
  37. const temp = r2;
  38. r2 = r1;
  39. r1 = temp;
  40. }
  41. if (r1 > 0 && r1 < maxR) {
  42. result.root = r1;
  43. result.found = true;
  44. return result;
  45. }
  46. if (r2 > 0 && r2 < maxR) {
  47. result.root = r2;
  48. result.found = true;
  49. return result;
  50. }
  51. return result;
  52. };
  53. })();
  54. /** @internal */
  55. export class Collider {
  56. constructor() {
  57. // Implementation of the "Improved Collision detection and Response" algorithm proposed by Kasper Fauerby
  58. // https://www.peroxide.dk/papers/collision/collision.pdf
  59. this._collisionPoint = Vector3.Zero();
  60. this._planeIntersectionPoint = Vector3.Zero();
  61. this._tempVector = Vector3.Zero();
  62. this._tempVector2 = Vector3.Zero();
  63. this._tempVector3 = Vector3.Zero();
  64. this._tempVector4 = Vector3.Zero();
  65. this._edge = Vector3.Zero();
  66. this._baseToVertex = Vector3.Zero();
  67. this._destinationPoint = Vector3.Zero();
  68. this._slidePlaneNormal = Vector3.Zero();
  69. this._displacementVector = Vector3.Zero();
  70. /** @internal */
  71. this._radius = Vector3.One();
  72. /** @internal */
  73. this._retry = 0;
  74. /** @internal */
  75. this._basePointWorld = Vector3.Zero();
  76. this._velocityWorld = Vector3.Zero();
  77. this._normalizedVelocity = Vector3.Zero();
  78. this._collisionMask = -1;
  79. }
  80. get collisionMask() {
  81. return this._collisionMask;
  82. }
  83. set collisionMask(mask) {
  84. this._collisionMask = !isNaN(mask) ? mask : -1;
  85. }
  86. /**
  87. * Gets the plane normal used to compute the sliding response (in local space)
  88. */
  89. get slidePlaneNormal() {
  90. return this._slidePlaneNormal;
  91. }
  92. // Methods
  93. /**
  94. * @internal
  95. */
  96. _initialize(source, dir, e) {
  97. this._velocity = dir;
  98. this._velocitySquaredLength = this._velocity.lengthSquared();
  99. const len = Math.sqrt(this._velocitySquaredLength);
  100. if (len === 0 || len === 1.0) {
  101. this._normalizedVelocity.copyFromFloats(dir._x, dir._y, dir._z);
  102. }
  103. else {
  104. dir.scaleToRef(1.0 / len, this._normalizedVelocity);
  105. }
  106. this._basePoint = source;
  107. source.multiplyToRef(this._radius, this._basePointWorld);
  108. dir.multiplyToRef(this._radius, this._velocityWorld);
  109. this._velocityWorldLength = this._velocityWorld.length();
  110. this._epsilon = e;
  111. this.collisionFound = false;
  112. }
  113. /**
  114. * @internal
  115. */
  116. _checkPointInTriangle(point, pa, pb, pc, n) {
  117. pa.subtractToRef(point, this._tempVector);
  118. pb.subtractToRef(point, this._tempVector2);
  119. Vector3.CrossToRef(this._tempVector, this._tempVector2, this._tempVector4);
  120. let d = Vector3.Dot(this._tempVector4, n);
  121. if (d < 0) {
  122. return false;
  123. }
  124. pc.subtractToRef(point, this._tempVector3);
  125. Vector3.CrossToRef(this._tempVector2, this._tempVector3, this._tempVector4);
  126. d = Vector3.Dot(this._tempVector4, n);
  127. if (d < 0) {
  128. return false;
  129. }
  130. Vector3.CrossToRef(this._tempVector3, this._tempVector, this._tempVector4);
  131. d = Vector3.Dot(this._tempVector4, n);
  132. return d >= 0;
  133. }
  134. /**
  135. * @internal
  136. */
  137. _canDoCollision(sphereCenter, sphereRadius, vecMin, vecMax) {
  138. const distance = Vector3.Distance(this._basePointWorld, sphereCenter);
  139. const max = Math.max(this._radius.x, this._radius.y, this._radius.z);
  140. if (distance > this._velocityWorldLength + max + sphereRadius) {
  141. return false;
  142. }
  143. if (!intersectBoxAASphere(vecMin, vecMax, this._basePointWorld, this._velocityWorldLength + max)) {
  144. return false;
  145. }
  146. return true;
  147. }
  148. /**
  149. * @internal
  150. */
  151. _testTriangle(faceIndex, trianglePlaneArray, p1, p2, p3, hasMaterial, hostMesh) {
  152. let t0;
  153. let embeddedInPlane = false;
  154. //defensive programming, actually not needed.
  155. if (!trianglePlaneArray) {
  156. trianglePlaneArray = [];
  157. }
  158. if (!trianglePlaneArray[faceIndex]) {
  159. trianglePlaneArray[faceIndex] = new Plane(0, 0, 0, 0);
  160. trianglePlaneArray[faceIndex].copyFromPoints(p1, p2, p3);
  161. }
  162. const trianglePlane = trianglePlaneArray[faceIndex];
  163. if (!hasMaterial && !trianglePlane.isFrontFacingTo(this._normalizedVelocity, 0)) {
  164. return;
  165. }
  166. const signedDistToTrianglePlane = trianglePlane.signedDistanceTo(this._basePoint);
  167. const normalDotVelocity = Vector3.Dot(trianglePlane.normal, this._velocity);
  168. // if DoubleSidedCheck is false(default), a double sided face will be consided 2 times.
  169. // if true, it discard the faces having normal not facing velocity
  170. if (Collider.DoubleSidedCheck && normalDotVelocity > 0.0001) {
  171. return;
  172. }
  173. if (normalDotVelocity == 0) {
  174. if (Math.abs(signedDistToTrianglePlane) >= 1.0) {
  175. return;
  176. }
  177. embeddedInPlane = true;
  178. t0 = 0;
  179. }
  180. else {
  181. t0 = (-1.0 - signedDistToTrianglePlane) / normalDotVelocity;
  182. let t1 = (1.0 - signedDistToTrianglePlane) / normalDotVelocity;
  183. if (t0 > t1) {
  184. const temp = t1;
  185. t1 = t0;
  186. t0 = temp;
  187. }
  188. if (t0 > 1.0 || t1 < 0.0) {
  189. return;
  190. }
  191. if (t0 < 0) {
  192. t0 = 0;
  193. }
  194. if (t0 > 1.0) {
  195. t0 = 1.0;
  196. }
  197. }
  198. this._collisionPoint.copyFromFloats(0, 0, 0);
  199. let found = false;
  200. let t = 1.0;
  201. if (!embeddedInPlane) {
  202. this._basePoint.subtractToRef(trianglePlane.normal, this._planeIntersectionPoint);
  203. this._velocity.scaleToRef(t0, this._tempVector);
  204. this._planeIntersectionPoint.addInPlace(this._tempVector);
  205. if (this._checkPointInTriangle(this._planeIntersectionPoint, p1, p2, p3, trianglePlane.normal)) {
  206. found = true;
  207. t = t0;
  208. this._collisionPoint.copyFrom(this._planeIntersectionPoint);
  209. }
  210. }
  211. if (!found) {
  212. let a = this._velocitySquaredLength;
  213. this._basePoint.subtractToRef(p1, this._tempVector);
  214. let b = 2.0 * Vector3.Dot(this._velocity, this._tempVector);
  215. let c = this._tempVector.lengthSquared() - 1.0;
  216. let lowestRoot = getLowestRoot(a, b, c, t);
  217. if (lowestRoot.found) {
  218. t = lowestRoot.root;
  219. found = true;
  220. this._collisionPoint.copyFrom(p1);
  221. }
  222. this._basePoint.subtractToRef(p2, this._tempVector);
  223. b = 2.0 * Vector3.Dot(this._velocity, this._tempVector);
  224. c = this._tempVector.lengthSquared() - 1.0;
  225. lowestRoot = getLowestRoot(a, b, c, t);
  226. if (lowestRoot.found) {
  227. t = lowestRoot.root;
  228. found = true;
  229. this._collisionPoint.copyFrom(p2);
  230. }
  231. this._basePoint.subtractToRef(p3, this._tempVector);
  232. b = 2.0 * Vector3.Dot(this._velocity, this._tempVector);
  233. c = this._tempVector.lengthSquared() - 1.0;
  234. lowestRoot = getLowestRoot(a, b, c, t);
  235. if (lowestRoot.found) {
  236. t = lowestRoot.root;
  237. found = true;
  238. this._collisionPoint.copyFrom(p3);
  239. }
  240. p2.subtractToRef(p1, this._edge);
  241. p1.subtractToRef(this._basePoint, this._baseToVertex);
  242. let edgeSquaredLength = this._edge.lengthSquared();
  243. let edgeDotVelocity = Vector3.Dot(this._edge, this._velocity);
  244. let edgeDotBaseToVertex = Vector3.Dot(this._edge, this._baseToVertex);
  245. a = edgeSquaredLength * -this._velocitySquaredLength + edgeDotVelocity * edgeDotVelocity;
  246. b = 2 * (edgeSquaredLength * Vector3.Dot(this._velocity, this._baseToVertex) - edgeDotVelocity * edgeDotBaseToVertex);
  247. c = edgeSquaredLength * (1.0 - this._baseToVertex.lengthSquared()) + edgeDotBaseToVertex * edgeDotBaseToVertex;
  248. lowestRoot = getLowestRoot(a, b, c, t);
  249. if (lowestRoot.found) {
  250. const f = (edgeDotVelocity * lowestRoot.root - edgeDotBaseToVertex) / edgeSquaredLength;
  251. if (f >= 0.0 && f <= 1.0) {
  252. t = lowestRoot.root;
  253. found = true;
  254. this._edge.scaleInPlace(f);
  255. p1.addToRef(this._edge, this._collisionPoint);
  256. }
  257. }
  258. p3.subtractToRef(p2, this._edge);
  259. p2.subtractToRef(this._basePoint, this._baseToVertex);
  260. edgeSquaredLength = this._edge.lengthSquared();
  261. edgeDotVelocity = Vector3.Dot(this._edge, this._velocity);
  262. edgeDotBaseToVertex = Vector3.Dot(this._edge, this._baseToVertex);
  263. a = edgeSquaredLength * -this._velocitySquaredLength + edgeDotVelocity * edgeDotVelocity;
  264. b = 2 * (edgeSquaredLength * Vector3.Dot(this._velocity, this._baseToVertex) - edgeDotVelocity * edgeDotBaseToVertex);
  265. c = edgeSquaredLength * (1.0 - this._baseToVertex.lengthSquared()) + edgeDotBaseToVertex * edgeDotBaseToVertex;
  266. lowestRoot = getLowestRoot(a, b, c, t);
  267. if (lowestRoot.found) {
  268. const f = (edgeDotVelocity * lowestRoot.root - edgeDotBaseToVertex) / edgeSquaredLength;
  269. if (f >= 0.0 && f <= 1.0) {
  270. t = lowestRoot.root;
  271. found = true;
  272. this._edge.scaleInPlace(f);
  273. p2.addToRef(this._edge, this._collisionPoint);
  274. }
  275. }
  276. p1.subtractToRef(p3, this._edge);
  277. p3.subtractToRef(this._basePoint, this._baseToVertex);
  278. edgeSquaredLength = this._edge.lengthSquared();
  279. edgeDotVelocity = Vector3.Dot(this._edge, this._velocity);
  280. edgeDotBaseToVertex = Vector3.Dot(this._edge, this._baseToVertex);
  281. a = edgeSquaredLength * -this._velocitySquaredLength + edgeDotVelocity * edgeDotVelocity;
  282. b = 2 * (edgeSquaredLength * Vector3.Dot(this._velocity, this._baseToVertex) - edgeDotVelocity * edgeDotBaseToVertex);
  283. c = edgeSquaredLength * (1.0 - this._baseToVertex.lengthSquared()) + edgeDotBaseToVertex * edgeDotBaseToVertex;
  284. lowestRoot = getLowestRoot(a, b, c, t);
  285. if (lowestRoot.found) {
  286. const f = (edgeDotVelocity * lowestRoot.root - edgeDotBaseToVertex) / edgeSquaredLength;
  287. if (f >= 0.0 && f <= 1.0) {
  288. t = lowestRoot.root;
  289. found = true;
  290. this._edge.scaleInPlace(f);
  291. p3.addToRef(this._edge, this._collisionPoint);
  292. }
  293. }
  294. }
  295. if (found) {
  296. const distToCollisionSquared = t * t * this._velocitySquaredLength;
  297. if (!this.collisionFound || distToCollisionSquared < this._nearestDistanceSquared) {
  298. // if collisionResponse is false, collision is not found but the collidedMesh is set anyway.
  299. // onCollide observable are triggered if collideMesh is set
  300. // this allow trigger volumes to be created.
  301. if (hostMesh.collisionResponse) {
  302. if (!this.intersectionPoint) {
  303. this.intersectionPoint = this._collisionPoint.clone();
  304. }
  305. else {
  306. this.intersectionPoint.copyFrom(this._collisionPoint);
  307. }
  308. this._nearestDistanceSquared = distToCollisionSquared;
  309. this._nearestDistance = Math.sqrt(distToCollisionSquared);
  310. this.collisionFound = true;
  311. }
  312. this.collidedMesh = hostMesh;
  313. }
  314. }
  315. }
  316. /**
  317. * @internal
  318. */
  319. _collide(trianglePlaneArray, pts, indices, indexStart, indexEnd, decal, hasMaterial, hostMesh, invertTriangles, triangleStrip = false) {
  320. if (triangleStrip) {
  321. if (!indices || indices.length === 0) {
  322. for (let i = 0; i < pts.length - 2; i += 1) {
  323. const p1 = pts[i];
  324. const p2 = pts[i + 1];
  325. const p3 = pts[i + 2];
  326. // stay defensive and don't check against undefined positions.
  327. if (!p1 || !p2 || !p3) {
  328. continue;
  329. }
  330. // Handles strip faces one on two is reversed
  331. if ((invertTriangles ? 1 : 0) ^ i % 2) {
  332. this._testTriangle(i, trianglePlaneArray, p1, p2, p3, hasMaterial, hostMesh);
  333. }
  334. else {
  335. this._testTriangle(i, trianglePlaneArray, p2, p1, p3, hasMaterial, hostMesh);
  336. }
  337. }
  338. }
  339. else {
  340. for (let i = indexStart; i < indexEnd - 2; i += 1) {
  341. const indexA = indices[i];
  342. const indexB = indices[i + 1];
  343. const indexC = indices[i + 2];
  344. if (indexC === 0xffffffff) {
  345. i += 2;
  346. continue;
  347. }
  348. const p1 = pts[indexA];
  349. const p2 = pts[indexB];
  350. const p3 = pts[indexC];
  351. // stay defensive and don't check against undefined positions.
  352. if (!p1 || !p2 || !p3) {
  353. continue;
  354. }
  355. // Handles strip faces one on two is reversed
  356. if ((invertTriangles ? 1 : 0) ^ i % 2) {
  357. this._testTriangle(i, trianglePlaneArray, p1, p2, p3, hasMaterial, hostMesh);
  358. }
  359. else {
  360. this._testTriangle(i, trianglePlaneArray, p2, p1, p3, hasMaterial, hostMesh);
  361. }
  362. }
  363. }
  364. }
  365. else if (!indices || indices.length === 0) {
  366. for (let i = 0; i < pts.length; i += 3) {
  367. const p1 = pts[i];
  368. const p2 = pts[i + 1];
  369. const p3 = pts[i + 2];
  370. if (invertTriangles) {
  371. this._testTriangle(i, trianglePlaneArray, p1, p2, p3, hasMaterial, hostMesh);
  372. }
  373. else {
  374. this._testTriangle(i, trianglePlaneArray, p3, p2, p1, hasMaterial, hostMesh);
  375. }
  376. }
  377. }
  378. else {
  379. for (let i = indexStart; i < indexEnd; i += 3) {
  380. const p1 = pts[indices[i] - decal];
  381. const p2 = pts[indices[i + 1] - decal];
  382. const p3 = pts[indices[i + 2] - decal];
  383. if (invertTriangles) {
  384. this._testTriangle(i, trianglePlaneArray, p1, p2, p3, hasMaterial, hostMesh);
  385. }
  386. else {
  387. this._testTriangle(i, trianglePlaneArray, p3, p2, p1, hasMaterial, hostMesh);
  388. }
  389. }
  390. }
  391. }
  392. /**
  393. * @internal
  394. */
  395. _getResponse(pos, vel) {
  396. pos.addToRef(vel, this._destinationPoint);
  397. vel.scaleInPlace(this._nearestDistance / vel.length());
  398. this._basePoint.addToRef(vel, pos);
  399. pos.subtractToRef(this.intersectionPoint, this._slidePlaneNormal);
  400. this._slidePlaneNormal.normalize();
  401. this._slidePlaneNormal.scaleToRef(this._epsilon, this._displacementVector);
  402. pos.addInPlace(this._displacementVector);
  403. this.intersectionPoint.addInPlace(this._displacementVector);
  404. this._slidePlaneNormal.scaleInPlace(Plane.SignedDistanceToPlaneFromPositionAndNormal(this.intersectionPoint, this._slidePlaneNormal, this._destinationPoint));
  405. this._destinationPoint.subtractInPlace(this._slidePlaneNormal);
  406. this._destinationPoint.subtractToRef(this.intersectionPoint, vel);
  407. }
  408. }
  409. /**
  410. * If true, it check for double sided faces and only returns 1 collision instead of 2
  411. */
  412. Collider.DoubleSidedCheck = false;
  413. //# sourceMappingURL=collider.js.map