Skip to content

Commit 8e872fc

Browse files
committed
Fixed closest and furthest when the RTree is disabled
1 parent d36ada0 commit 8e872fc

3 files changed

Lines changed: 37 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
## Version 3.17.0 - Ishikawa - in dev
44

5-
### Arcade Physics New Features, Updates and Fixes
5+
### Arcade Physics
6+
7+
#### New Features
68

79
* `World.overlapTiles` is a new method that allows you to check for overlaps between a physics enabled Game Object and an array of Tiles. The Tiles don't have to have been enable for collision, or even be on the same layer, for the overlap check to work. You can provide your own process callback and/or overlap callback. This is handy for testing for overlap for a specific Tile in your map, not just based on a tile index. This is also available via `this.physics.overlapTiles`.
810
* `World.collideTiles` is a new method that allows you to check for collision between a physics enabled Game Object and an array of Tiles. The Tiles don't have to have been enable for collision, or even be on the same layer, for the collision to work. You can provide your own process callback and/or overlap callback. There are some limitations in using this method, please consult the API Docs for details, but on the whole, it allows for dynamic collision on small sets of Tile instances. This is also available via `this.physics.collideTiles`.
9-
* The `Body.delta` values are now able to be read and acted upon during a Scene update, due to the new game step flow. This means you can now call `this.physics.collide` during a Scene `update` and it will work properly again. Fix #4370 (thanks @NokFrt)
11+
12+
#### Updates
13+
1014
* `Body.preUpdate` is a new method that is called only once per game step. It resets all collision status properties and syncs the Body with the parent Game Object.
1115
* `Body.update` has been rewritten to just perform one single physics step and no longer re-syncs with the Game Object. It can be called multiple times per game step, depending on the World FPS rate.
1216
* `Body.postUpdate` has been rewritten to make it more compact. It syncs the body data back to the parent Game Object and is only called once per game step now (previously it was called whenever the Body updated)
@@ -18,6 +22,12 @@
1822
* The internal method `TileCheckX` now has a new argument `isLayer` which controls if the set comes from a layer or an array.
1923
* The internal method `TileCheckY` now has a new argument `isLayer` which controls if the set comes from a layer or an array.
2024

25+
#### Bug Fixes
26+
27+
* The `Body.delta` values are now able to be read and acted upon during a Scene update, due to the new game step flow. This means you can now call `this.physics.collide` during a Scene `update` and it will work properly again. Fix #4370 (thanks @NokFrt)
28+
* `ArcadePhysics.furthest` now iterates the bodies Set, rather than the RTree, which keeps it working even if the RTree has been disabled.
29+
* `ArcadePhysics.closest` now iterates the bodies Set, rather than the RTree, which keeps it working even if the RTree has been disabled.
30+
2131
### New Features
2232

2333
* There is a new Game Config property `input.windowEvents` which is true by default. It controls if Phaser will listen for any input events on the Window. If you disable this, Phaser will stop being able to emit events like `POINTER_UP_OUTSIDE`, or be aware of anything that happens outside of the Canvas re: input.

src/physics/arcade/ArcadePhysics.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -344,69 +344,75 @@ var ArcadePhysics = new Class({
344344
},
345345

346346
/**
347-
* Finds the Body closest to a source point or object.
347+
* Finds the Dynamic Body closest to a source point or object.
348+
*
349+
* If two or more bodies are the exact same distance from the source point, only the first body
350+
* is returned.
348351
*
349352
* @method Phaser.Physics.Arcade.ArcadePhysics#closest
350353
* @since 3.0.0
351354
*
352-
* @param {object} source - Any object with public `x` and `y` properties, such as a Game Object or Geometry object.
355+
* @param {any} source - Any object with public `x` and `y` properties, such as a Game Object or Geometry object.
353356
*
354-
* @return {Phaser.Physics.Arcade.Body} The closest Body to the given source point.
357+
* @return {Phaser.Physics.Arcade.Body} The closest Dynamic Body to the given source point.
355358
*/
356359
closest: function (source)
357360
{
358-
var bodies = this.world.tree.all();
361+
var bodies = this.world.bodies;
359362

360363
var min = Number.MAX_VALUE;
361364
var closest = null;
362365
var x = source.x;
363366
var y = source.y;
364367

365-
for (var i = bodies.length - 1; i >= 0; i--)
368+
bodies.iterate(function (target)
366369
{
367-
var target = bodies[i];
368370
var distance = DistanceSquared(x, y, target.x, target.y);
369371

370372
if (distance < min)
371373
{
372374
closest = target;
373375
min = distance;
374376
}
375-
}
377+
378+
});
376379

377380
return closest;
378381
},
379382

380383
/**
381-
* Finds the Body farthest from a source point or object.
384+
* Finds the Dynamic Body farthest from a source point or object.
385+
*
386+
* If two or more bodies are the exact same distance from the source point, only the first body
387+
* is returned.
382388
*
383389
* @method Phaser.Physics.Arcade.ArcadePhysics#furthest
384390
* @since 3.0.0
385391
*
386-
* @param {object} source - Any object with public `x` and `y` properties, such as a Game Object or Geometry object.
392+
* @param {any} source - Any object with public `x` and `y` properties, such as a Game Object or Geometry object.
387393
*
388-
* @return {Phaser.Physics.Arcade.Body} The Body furthest from the given source point.
394+
* @return {Phaser.Physics.Arcade.Body} The Dynamic Body furthest away from the given source point.
389395
*/
390396
furthest: function (source)
391397
{
392-
var bodies = this.world.tree.all();
398+
var bodies = this.world.bodies;
393399

394400
var max = -1;
395401
var farthest = null;
396402
var x = source.x;
397403
var y = source.y;
398404

399-
for (var i = bodies.length - 1; i >= 0; i--)
405+
bodies.iterate(function (target)
400406
{
401-
var target = bodies[i];
402407
var distance = DistanceSquared(x, y, target.x, target.y);
403408

404409
if (distance > max)
405410
{
406411
farthest = target;
407412
max = distance;
408413
}
409-
}
414+
415+
});
410416

411417
return farthest;
412418
},

src/physics/arcade/World.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,18 +315,19 @@ var World = new Class({
315315
this.maxEntries = GetValue(config, 'maxEntries', 16);
316316

317317
/**
318-
* Should this Arcade Physics World use an RTree for Dynamic Physics bodies or not?
318+
* Should this Arcade Physics World use an RTree for Dynamic and Static Physics bodies?
319319
*
320-
* An RTree is a fast way of spatially sorting of all the moving bodies in the world.
320+
* An RTree is a fast way of spatially sorting of all the bodies in the world.
321321
* However, at certain limits, the cost of clearing and inserting the bodies into the
322322
* tree every frame becomes more expensive than the search speed gains it provides.
323323
*
324324
* If you have a large number of dynamic bodies in your world then it may be best to
325-
* disable the use of the RTree by setting this property to `true`.
325+
* disable the use of the RTree by setting this property to `false` in the physics config.
326+
*
326327
* The number it can cope with depends on browser and device, but a conservative estimate
327328
* of around 5,000 bodies should be considered the max before disabling it.
328329
*
329-
* Note this only applies to dynamic bodies. Static bodies are always kept in an RTree,
330+
* This only applies to dynamic bodies. Static bodies are always kept in an RTree,
330331
* because they don't have to be cleared every frame, so you benefit from the
331332
* massive search speeds all the time.
332333
*
@@ -2301,7 +2302,6 @@ var World = new Class({
23012302
}
23022303
},
23032304

2304-
23052305
/**
23062306
* Wrap each object's coordinates within {@link Phaser.Physics.Arcade.World#bounds}.
23072307
*

0 commit comments

Comments
 (0)