mirror of
https://github.com/photonstorm/phaser
synced 2024-11-24 13:43:26 +00:00
Moved OverlapRect to a component and added option to scan both dynamic and static bodies
This commit is contained in:
parent
9577f3e612
commit
bb058dc7ba
2 changed files with 91 additions and 54 deletions
|
@ -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);
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
79
src/physics/arcade/components/OverlapRect.js
Normal file
79
src/physics/arcade/components/OverlapRect.js
Normal file
|
@ -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;
|
Loading…
Reference in a new issue