RectangleCollisionChecker.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import RBush from "rbush";
  2. import Check from "./Check.js";
  3. /**
  4. * Wrapper around rbush for use with Rectangle types.
  5. * @private
  6. */
  7. function RectangleCollisionChecker() {
  8. this._tree = new RBush();
  9. }
  10. function RectangleWithId() {
  11. this.minX = 0.0;
  12. this.minY = 0.0;
  13. this.maxX = 0.0;
  14. this.maxY = 0.0;
  15. this.id = "";
  16. }
  17. RectangleWithId.fromRectangleAndId = function (id, rectangle, result) {
  18. result.minX = rectangle.west;
  19. result.minY = rectangle.south;
  20. result.maxX = rectangle.east;
  21. result.maxY = rectangle.north;
  22. result.id = id;
  23. return result;
  24. };
  25. /**
  26. * Insert a rectangle into the collision checker.
  27. *
  28. * @param {string} id Unique string ID for the rectangle being inserted.
  29. * @param {Rectangle} rectangle A Rectangle
  30. * @private
  31. */
  32. RectangleCollisionChecker.prototype.insert = function (id, rectangle) {
  33. //>>includeStart('debug', pragmas.debug);
  34. Check.typeOf.string("id", id);
  35. Check.typeOf.object("rectangle", rectangle);
  36. //>>includeEnd('debug');
  37. const withId = RectangleWithId.fromRectangleAndId(
  38. id,
  39. rectangle,
  40. new RectangleWithId()
  41. );
  42. this._tree.insert(withId);
  43. };
  44. function idCompare(a, b) {
  45. return a.id === b.id;
  46. }
  47. const removalScratch = new RectangleWithId();
  48. /**
  49. * Remove a rectangle from the collision checker.
  50. *
  51. * @param {string} id Unique string ID for the rectangle being removed.
  52. * @param {Rectangle} rectangle A Rectangle
  53. * @private
  54. */
  55. RectangleCollisionChecker.prototype.remove = function (id, rectangle) {
  56. //>>includeStart('debug', pragmas.debug);
  57. Check.typeOf.string("id", id);
  58. Check.typeOf.object("rectangle", rectangle);
  59. //>>includeEnd('debug');
  60. const withId = RectangleWithId.fromRectangleAndId(
  61. id,
  62. rectangle,
  63. removalScratch
  64. );
  65. this._tree.remove(withId, idCompare);
  66. };
  67. const collisionScratch = new RectangleWithId();
  68. /**
  69. * Checks if a given rectangle collides with any of the rectangles in the collection.
  70. *
  71. * @param {Rectangle} rectangle A Rectangle that should be checked against the rectangles in the collision checker.
  72. * @returns {boolean} Whether the rectangle collides with any of the rectangles in the collision checker.
  73. */
  74. RectangleCollisionChecker.prototype.collides = function (rectangle) {
  75. //>>includeStart('debug', pragmas.debug);
  76. Check.typeOf.object("rectangle", rectangle);
  77. //>>includeEnd('debug');
  78. const withId = RectangleWithId.fromRectangleAndId(
  79. "",
  80. rectangle,
  81. collisionScratch
  82. );
  83. return this._tree.collides(withId);
  84. };
  85. export default RectangleCollisionChecker;