collider.ts 15 KB

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