Skip to content

Commit 3038f6f

Browse files
committed
Added optimised point in circle test to CircleUtils
1 parent d80467a commit 3038f6f

6 files changed

Lines changed: 45 additions & 6 deletions

File tree

6.87 MB
Loading

Phaser/utils/CircleUtils.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,17 @@ module Phaser {
3636
* @return {Boolean} True if the coordinates are within this circle, otherwise false.
3737
**/
3838
static contains(a: Circle, x: number, y: number): bool {
39-
//return (a.radius * a.radius >= Collision.distanceSquared(a.x, a.y, x, y));
40-
return true;
39+
40+
// Check if x/y are within the bounds first
41+
if (x >= a.left && x <= a.right && y >= a.top && y <= a.bottom)
42+
{
43+
var dx: number = (a.x - x) * (a.x - x);
44+
var dy: number = (a.y - y) * (a.y - y);
45+
return (dx + dy) <= (a.radius * a.radius);
46+
}
47+
48+
return false;
49+
4150
}
4251

4352
/**

Phaser/utils/DebugUtils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ module Phaser {
9696

9797
}
9898

99+
static renderCircle(circle: Phaser.Circle, fillStyle: string = 'rgba(0,255,0,0.3)') {
100+
101+
DebugUtils.context.fillStyle = fillStyle;
102+
DebugUtils.context.arc(circle.x, circle.y, circle.radius, 0, Math.PI * 2, false);
103+
DebugUtils.context.fill();
104+
105+
}
106+
99107
static renderPhysicsBody(body: Phaser.Physics.Body, lineWidth: number = 1, fillStyle: string = 'rgba(0,255,0,0.2)', sleepStyle: string = 'rgba(100,100,100,0.2)') {
100108

101109
for (var s = 0; s < body.shapesLength; s++)

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ TODO:
6060
* Stage CSS3 Transforms?
6161
* Ability to layer another DOM object and have it controlled by the game somehow. Can then do stacked canvas effects.
6262
* Stage lost to mute
63+
* When game is paused Pointer shouldn't process targetObjects / change cursor
6364

6465

6566

@@ -157,6 +158,7 @@ V1.0.0
157158
* Dropped the StageScaleMode.setScreenSize iterations count from 40 down to 10 and document min body height to 2000px.
158159
* Added Phaser.Net for browser and network specific functions, currently includes query string parsing and updating methods.
159160
* Added a new CSS3 Filters component. Apply blur, grayscale, sepia, brightness, contrast, hue rotation, invert, opacity and saturate filters to the games stage.
161+
* Fixed the CircleUtils.contains and containsPoint methods
160162

161163

162164

Tests/phaser.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20758,8 +20758,18 @@ var Phaser;
2075820758
* @return {Boolean} True if the coordinates are within this circle, otherwise false.
2075920759
**/
2076020760
function contains(a, x, y) {
20761-
//return (a.radius * a.radius >= Collision.distanceSquared(a.x, a.y, x, y));
20762-
return true;
20761+
// Check if x/y are within the bounds first
20762+
if(x >= a.left && x <= a.right && y >= a.top && y <= a.bottom) {
20763+
var dx = a.x - x;
20764+
var dy = a.y - y;
20765+
dx *= dx;
20766+
dy *= dy;
20767+
var radSqr = a.radius * a.radius;
20768+
console.log('within bounds', dx, dy, radSqr);
20769+
return (dx + dy) <= radSqr;
20770+
//return (a.left * a.left + a.top * a.top) <= (a.radius * a.radius);
20771+
}
20772+
return false;
2076320773
};
2076420774
CircleUtils.containsPoint = /**
2076520775
* Return true if the coordinates of the given Point object are within this Circle object.

build/phaser.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20758,8 +20758,18 @@ var Phaser;
2075820758
* @return {Boolean} True if the coordinates are within this circle, otherwise false.
2075920759
**/
2076020760
function contains(a, x, y) {
20761-
//return (a.radius * a.radius >= Collision.distanceSquared(a.x, a.y, x, y));
20762-
return true;
20761+
// Check if x/y are within the bounds first
20762+
if(x >= a.left && x <= a.right && y >= a.top && y <= a.bottom) {
20763+
var dx = a.x - x;
20764+
var dy = a.y - y;
20765+
dx *= dx;
20766+
dy *= dy;
20767+
var radSqr = a.radius * a.radius;
20768+
console.log('within bounds', dx, dy, radSqr);
20769+
return (dx + dy) <= radSqr;
20770+
//return (a.left * a.left + a.top * a.top) <= (a.radius * a.radius);
20771+
}
20772+
return false;
2076320773
};
2076420774
CircleUtils.containsPoint = /**
2076520775
* Return true if the coordinates of the given Point object are within this Circle object.

0 commit comments

Comments
 (0)