|
| 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