Skip to content

Commit 800aac5

Browse files
authored
Merge pull request phaserjs#4749 from funnisimo/hitTest
Fix for Issue phaserjs#4748 - Physics.Arcade.Body.hitTest - bug with circles
2 parents 8c18699 + 4bffc5a commit 800aac5

1 file changed

Lines changed: 27 additions & 13 deletions

File tree

src/physics/arcade/Body.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* @license {@link https://opensource.org/licenses/MIT|MIT License}
55
*/
66

7-
var CircleContains = require('../../geom/circle/Contains');
87
var Class = require('../../utils/Class');
98
var CONST = require('./const');
109
var Events = require('./events');
@@ -421,14 +420,14 @@ var Body = new Class({
421420

422421
/**
423422
* The maximum speed this Body is allowed to reach.
424-
*
423+
*
425424
* If not negative it limits the scalar value of speed.
426-
*
425+
*
427426
* Any negative value means no maximum is being applied.
428-
*
427+
*
429428
* @name Phaser.Physics.Arcade.Body#maxSpeed
430429
* @type {number}
431-
* @since 3.16.0
430+
* @since 3.16.0
432431
*/
433432
this.maxSpeed = -1;
434433

@@ -844,7 +843,7 @@ var Body = new Class({
844843

845844
/**
846845
* Prepares the Body for a physics step by resetting the `wasTouching`, `touching` and `blocked` states.
847-
*
846+
*
848847
* This method is only called if the physics world is going to run a step this frame.
849848
*
850849
* @method Phaser.Physics.Arcade.Body#resetFlags
@@ -880,12 +879,12 @@ var Body = new Class({
880879

881880
/**
882881
* Syncs the position body position with the parent Game Object.
883-
*
882+
*
884883
* This method is called every game frame, regardless if the world steps or not.
885884
*
886885
* @method Phaser.Physics.Arcade.Body#preUpdate
887886
* @since 3.17.0
888-
*
887+
*
889888
* @param {boolean} willStep - Will this Body run an update as well?
890889
* @param {number} delta - The delta time, in seconds, elapsed since the last frame.
891890
*/
@@ -923,9 +922,9 @@ var Body = new Class({
923922

924923
/**
925924
* Performs a single physics step and updates the body velocity, angle, speed and other properties.
926-
*
925+
*
927926
* This method can be called multiple times per game frame, depending on the physics step rate.
928-
*
927+
*
929928
* The results are synced back to the Game Object in `postUpdate`.
930929
*
931930
* @method Phaser.Physics.Arcade.Body#update
@@ -967,7 +966,7 @@ var Body = new Class({
967966

968967
/**
969968
* Feeds the Body results back into the parent Game Object.
970-
*
969+
*
971970
* This method is called every game frame, regardless if the world steps or not.
972971
*
973972
* @method Phaser.Physics.Arcade.Body#postUpdate
@@ -1116,6 +1115,7 @@ var Body = new Class({
11161115
if (y === undefined) { y = x; }
11171116

11181117
this.offset.set(x, y);
1118+
this.updateCenter();
11191119

11201120
return this;
11211121
},
@@ -1306,7 +1306,21 @@ var Body = new Class({
13061306
*/
13071307
hitTest: function (x, y)
13081308
{
1309-
return (this.isCircle) ? CircleContains(this, x, y) : RectangleContains(this, x, y);
1309+
if (!this.isCircle)
1310+
{
1311+
return RectangleContains(this, x, y);
1312+
}
1313+
1314+
// Check if x/y are within the bounds first
1315+
if (this.radius > 0 && x >= this.left && x <= this.right && y >= this.top && y <= this.bottom)
1316+
{
1317+
var dx = (this.center.x - x) * (this.center.x - x);
1318+
var dy = (this.center.y - y) * (this.center.y - y);
1319+
1320+
return (dx + dy) <= (this.radius * this.radius);
1321+
}
1322+
1323+
return false;
13101324
},
13111325

13121326
/**
@@ -1504,7 +1518,7 @@ var Body = new Class({
15041518

15051519
/**
15061520
* Sets whether this Body collides with the world boundary.
1507-
*
1521+
*
15081522
* Optionally also sets the World Bounce values. If the `Body.worldBounce` is null, it's set to a new Vec2 first.
15091523
*
15101524
* @method Phaser.Physics.Arcade.Body#setCollideWorldBounds

0 commit comments

Comments
 (0)