RectangleCollisionChecker.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import rbush from '../ThirdParty/rbush.js';
  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 = 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. var withId = RectangleWithId.fromRectangleAndId(id, rectangle, new RectangleWithId());
  38. this._tree.insert(withId);
  39. };
  40. function idCompare(a, b) {
  41. return a.id === b.id;
  42. }
  43. var removalScratch = new RectangleWithId();
  44. /**
  45. * Remove a rectangle from the collision checker.
  46. *
  47. * @param {String} id Unique string ID for the rectangle being removed.
  48. * @param {Rectangle} rectangle A Rectangle
  49. * @private
  50. */
  51. RectangleCollisionChecker.prototype.remove = function(id, rectangle) {
  52. //>>includeStart('debug', pragmas.debug);
  53. Check.typeOf.string('id', id);
  54. Check.typeOf.object('rectangle', rectangle);
  55. //>>includeEnd('debug');
  56. var withId = RectangleWithId.fromRectangleAndId(id, rectangle, removalScratch);
  57. this._tree.remove(withId, idCompare);
  58. };
  59. var collisionScratch = new RectangleWithId();
  60. /**
  61. * Checks if a given rectangle collides with any of the rectangles in the collection.
  62. *
  63. * @param {Rectangle} rectangle A Rectangle that should be checked against the rectangles in the collision checker.
  64. * @returns {Boolean} Whether the rectangle collides with any of the rectangles in the collision checker.
  65. */
  66. RectangleCollisionChecker.prototype.collides = function(rectangle) {
  67. //>>includeStart('debug', pragmas.debug);
  68. Check.typeOf.object('rectangle', rectangle);
  69. //>>includeEnd('debug');
  70. var withId = RectangleWithId.fromRectangleAndId('', rectangle, collisionScratch);
  71. return this._tree.collides(withId);
  72. };
  73. export default RectangleCollisionChecker;