collider.ts 16 KB

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