Skip to content

Commit adad830

Browse files
committed
Merge branch 'master' of https://github.com/photonstorm/phaser
2 parents ed2fa60 + dc6b080 commit adad830

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

src/physics/arcade/ArcadePhysics.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var DistanceSquared = require('../../math/distance/DistanceSquared');
1111
var Factory = require('./Factory');
1212
var GetFastValue = require('../../utils/object/GetFastValue');
1313
var Merge = require('../../utils/object/Merge');
14+
var OverlapCirc = require('./components/OverlapCirc');
1415
var OverlapRect = require('./components/OverlapRect');
1516
var PluginCache = require('../../plugins/PluginCache');
1617
var SceneEvents = require('../../scene/events');
@@ -604,6 +605,32 @@ var ArcadePhysics = new Class({
604605
return OverlapRect(this.world, x, y, width, height, includeDynamic, includeStatic);
605606
},
606607

608+
/**
609+
* This method will search the given circular area and return an array of all physics bodies that
610+
* overlap with it. It can return either Dynamic, Static bodies or a mixture of both.
611+
*
612+
* A body only has to intersect with the search area to be considered, it doesn't have to be fully
613+
* contained within it.
614+
*
615+
* If Arcade Physics is set to use the RTree (which it is by default) then the search is rather fast,
616+
* otherwise the search is O(N) for Dynamic Bodies.
617+
*
618+
* @method Phaser.Physics.Arcade.ArcadePhysics#overlapCirc
619+
* @since 3.21.0
620+
*
621+
* @param {number} x - The x coordinate of the center of the area to search within.
622+
* @param {number} y - The y coordinate of the center of the area to search within.
623+
* @param {number} radius - The radius of the area to search within.
624+
* @param {boolean} [includeDynamic=true] - Should the search include Dynamic Bodies?
625+
* @param {boolean} [includeStatic=false] - Should the search include Static Bodies?
626+
*
627+
* @return {(Phaser.Physics.Arcade.Body[]|Phaser.Physics.Arcade.StaticBody[])} An array of bodies that overlap with the given area.
628+
*/
629+
overlapCirc: function (x, y, radius, includeDynamic, includeStatic)
630+
{
631+
return OverlapCirc(this.world, x, y, radius, includeDynamic, includeStatic);
632+
},
633+
607634
/**
608635
* The Scene that owns this plugin is shutting down.
609636
* We need to kill and reset all internal properties as well as stop listening to Scene events.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
var OverlapRect = require('./OverlapRect');
2+
var Circle = require('../../../geom/circle/Circle');
3+
var CircleToCircle = require('../../../geom/intersects/CircleToCircle');
4+
var CircleToRectangle = require('../../../geom/intersects/CircleToRectangle');
5+
6+
/**
7+
* This method will search the given circular area and return an array of all physics bodies that
8+
* overlap with it. It can return either Dynamic, Static bodies or a mixture of both.
9+
*
10+
* A body only has to intersect with the search area to be considered, it doesn't have to be fully
11+
* contained within it.
12+
*
13+
* If Arcade Physics is set to use the RTree (which it is by default) then the search is rather fast,
14+
* otherwise the search is O(N) for Dynamic Bodies.
15+
*
16+
* @function Phaser.Physics.Arcade.Components.OverlapCirc
17+
* @since 3.21.0
18+
*
19+
* @param {number} x - The x coordinate of the center of the area to search within.
20+
* @param {number} y - The y coordinate of the center of the area to search within.
21+
* @param {number} radius - The radius of the area to search within.
22+
* @param {boolean} [includeDynamic=true] - Should the search include Dynamic Bodies?
23+
* @param {boolean} [includeStatic=false] - Should the search include Static Bodies?
24+
*
25+
* @return {(Phaser.Physics.Arcade.Body[]|Phaser.Physics.Arcade.StaticBody[])} An array of bodies that overlap with the given area.
26+
*/
27+
var OverlapCirc = function (world, x, y, radius, includeDynamic, includeStatic)
28+
{
29+
var bodiesInRect = OverlapRect(world, x - radius, y - radius, 2 * radius, 2 * radius, includeDynamic, includeStatic);
30+
31+
if (bodiesInRect.length === 0)
32+
{
33+
return bodiesInRect;
34+
}
35+
36+
var area = new Circle(x, y, radius);
37+
var circFromBody = new Circle();
38+
var bodiesInArea = [];
39+
40+
for (var i = 0; i < bodiesInRect.length; i++)
41+
{
42+
var body = bodiesInRect[i];
43+
44+
if (body.isCircle)
45+
{
46+
circFromBody.setTo(body.center.x, body.center.y, body.halfWidth);
47+
48+
if (CircleToCircle(area, circFromBody))
49+
{
50+
bodiesInArea.push(body);
51+
}
52+
}
53+
else if (CircleToRectangle(area, body))
54+
{
55+
bodiesInArea.push(body);
56+
}
57+
}
58+
59+
return bodiesInArea;
60+
};
61+
62+
module.exports = OverlapCirc;

0 commit comments

Comments
 (0)