diff --git a/src/physics/arcade/ArcadePhysics.js b/src/physics/arcade/ArcadePhysics.js index 62e211fc7..74a603f4e 100644 --- a/src/physics/arcade/ArcadePhysics.js +++ b/src/physics/arcade/ArcadePhysics.js @@ -11,6 +11,7 @@ var DistanceSquared = require('../../math/distance/DistanceSquared'); var Factory = require('./Factory'); var GetFastValue = require('../../utils/object/GetFastValue'); var Merge = require('../../utils/object/Merge'); +var OverlapRect = require('./components/OverlapRect'); var PluginCache = require('../../plugins/PluginCache'); var SceneEvents = require('../../scene/events'); var Vector2 = require('../../math/Vector2'); @@ -543,8 +544,13 @@ var ArcadePhysics = new Class({ /** * This method will search the given rectangular area and return an array of all physics bodies that - * overlap with it. It can return either Dynamic or Static bodies. If Arcade Physics is set to use - * the RTree (which it is by default) then the search is extremely fast, otherwise the search is O(N). + * overlap with it. It can return either Dynamic, Static bodies or a mixture of both. + * + * A body only has to intersect with the search area to be considered, it doesn't have to be fully + * contained within it. + * + * If Arcade Physics is set to use the RTree (which it is by default) then the search for is extremely fast, + * otherwise the search is O(N) for Dynamic Bodies. * * @method Phaser.Physics.Arcade.ArcadePhysics#overlapRect * @since 3.17.0 @@ -553,62 +559,14 @@ var ArcadePhysics = new Class({ * @param {number} y - The top-left y coordinate of the area to search within. * @param {number} width - The width of the area to search within. * @param {number} height - The height of the area to search within. - * @param {boolean} [dynamic=true] - Search for Dynamic Bodies (true) or Static Bodies (false) + * @param {boolean} [dynamic=true] - Should the search include Dynamic Bodies? + * @param {boolean} [static=false] - Should the search include Static Bodies? * * @return {(Phaser.Physics.Arcade.Body[]|Phaser.Physics.Arcade.StaticBody[])} An array of bodies that overlap with the given area. */ - overlapRect: function (x, y, width, height, dynamic) + overlapRect: function (x, y, width, height, dynamic, static) { - if (dynamic === undefined) { dynamic = true; } - - var output = []; - var world = this.world; - - var minMax = world.treeMinMax; - - minMax.minX = x; - minMax.minY = y; - minMax.maxX = x + width; - minMax.maxY = y + height; - - if (!dynamic) - { - output = world.staticTree.search(minMax); - } - else if (world.useTree) - { - output = world.tree.search(minMax); - } - else - { - var bodies = world.bodies; - - var fakeBody = - { - position: { - x: x, - y: y - }, - left: x, - top: y, - right: x + width, - bottom: y + height, - isCircle: false - }; - - var intersects = world.intersects; - - bodies.iterate(function (target) - { - if (intersects(target, fakeBody)) - { - output.push(target); - } - - }); - } - - return output; + return OverlapRect(this.world, x, y, width, height, dynamic, static); }, /** diff --git a/src/physics/arcade/components/OverlapRect.js b/src/physics/arcade/components/OverlapRect.js new file mode 100644 index 000000000..836307c5b --- /dev/null +++ b/src/physics/arcade/components/OverlapRect.js @@ -0,0 +1,79 @@ +/** + * This method will search the given rectangular area and return an array of all physics bodies that + * overlap with it. It can return either Dynamic, Static bodies or a mixture of both. + * + * A body only has to intersect with the search area to be considered, it doesn't have to be fully + * contained within it. + * + * If Arcade Physics is set to use the RTree (which it is by default) then the search for is extremely fast, + * otherwise the search is O(N) for Dynamic Bodies. + * + * @function Phaser.Physics.Arcade.Components.OverlapRect + * @since 3.17.0 + * + * @param {number} x - The top-left x coordinate of the area to search within. + * @param {number} y - The top-left y coordinate of the area to search within. + * @param {number} width - The width of the area to search within. + * @param {number} height - The height of the area to search within. + * @param {boolean} [dynamic=true] - Should the search include Dynamic Bodies? + * @param {boolean} [static=false] - Should the search include Static Bodies? + * + * @return {(Phaser.Physics.Arcade.Body[]|Phaser.Physics.Arcade.StaticBody[])} An array of bodies that overlap with the given area. + */ +var OverlapRect = function (world, x, y, width, height, dynamic, static) +{ + if (dynamic === undefined) { dynamic = true; } + if (static === undefined) { static = false; } + + var dynamicBodies = []; + var staticBodies = []; + + var minMax = world.treeMinMax; + + minMax.minX = x; + minMax.minY = y; + minMax.maxX = x + width; + minMax.maxY = y + height; + + if (static) + { + staticBodies = world.staticTree.search(minMax); + } + + if (dynamic && world.useTree) + { + dynamicBodies = world.tree.search(minMax); + } + else if (dynamic) + { + var bodies = world.bodies; + + var fakeBody = + { + position: { + x: x, + y: y + }, + left: x, + top: y, + right: x + width, + bottom: y + height, + isCircle: false + }; + + var intersects = world.intersects; + + bodies.iterate(function (target) + { + if (intersects(target, fakeBody)) + { + dynamicBodies.push(target); + } + + }); + } + + return staticBodies.concat(dynamicBodies); +}; + +module.exports = OverlapRect;