Skip to content

Commit 23ec381

Browse files
committed
Added circleToRect intersection check.
1 parent 39e7b84 commit 23ec381

8 files changed

Lines changed: 115 additions & 69 deletions

File tree

Phaser/physics/Circle.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module Phaser.Physics {
2929
this.scale = new Vec2(1, 1);
3030
}
3131

32+
this.diameter = diameter;
3233
this.radius = diameter / 2;
3334
this.bounds = new Rectangle(x + Math.round(diameter / 2), y + Math.round(diameter / 2), diameter, diameter);
3435
this.position = new Vec2(x + this.bounds.halfWidth, y + this.bounds.halfHeight);
@@ -49,6 +50,7 @@ module Phaser.Physics {
4950
public bounds: Rectangle;
5051

5152
public radius: number;
53+
public diameter: number;
5254

5355
public preUpdate() {
5456

@@ -89,19 +91,11 @@ module Phaser.Physics {
8991

9092
public render(context:CanvasRenderingContext2D) {
9193

92-
context.beginPath();
93-
//context.strokeStyle = 'rgb(255,255,0)';
94-
//context.strokeRect(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight, this.bounds.width, this.bounds.height);
95-
//context.fillStyle = 'rgba(0,0,255,0.8)';
96-
context.strokeStyle = 'rgba(0,0,255,0.5)';
97-
context.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2);
98-
//context.fill();
99-
context.stroke();
100-
context.closePath();
101-
10294
// center point
103-
context.fillStyle = 'rgb(255,255,0)';
104-
context.fillRect(this.position.x, this.position.y, 2, 2);
95+
context.fillStyle = 'rgba(255,0,0,0.5)';
96+
context.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2);
97+
context.rect(this.position.x, this.position.y, 2, 2);
98+
context.fill();
10599

106100
/*
107101
if (this.oH == 1)
@@ -149,11 +143,13 @@ module Phaser.Physics {
149143

150144
if (this.deltaX > 0)
151145
{
152-
return this.bounds.width + this.deltaX;
146+
//return this.bounds.width + this.deltaX;
147+
return this.diameter + this.deltaX;
153148
}
154149
else
155150
{
156-
return this.bounds.width - this.deltaX;
151+
//return this.bounds.width - this.deltaX;
152+
return this.diameter - this.deltaX;
157153
}
158154

159155
}
@@ -162,11 +158,13 @@ module Phaser.Physics {
162158

163159
if (this.deltaY > 0)
164160
{
165-
return this.bounds.height + this.deltaY;
161+
//return this.bounds.height + this.deltaY;
162+
return this.diameter + this.deltaY;
166163
}
167164
else
168165
{
169-
return this.bounds.height - this.deltaY;
166+
//return this.bounds.height - this.deltaY;
167+
return this.diameter - this.deltaY;
170168
}
171169

172170
}
@@ -213,7 +211,6 @@ module Phaser.Physics {
213211
return this.position.y - this.oldPosition.y;
214212
}
215213

216-
217214
}
218215

219216
}

Phaser/physics/PhysicsManager.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// <reference path="../Game.ts" />
22
/// <reference path="IPhysicsShape.ts" />
33
/// <reference path="../utils/RectangleUtils.ts" />
4+
/// <reference path="../utils/CircleUtils.ts" />
45

56
/**
67
* Phaser - PhysicsManager
@@ -312,6 +313,17 @@ module Phaser.Physics {
312313

313314
if (object1.physics.shape.deltaX != object2.physics.shape.deltaX)
314315
{
316+
//var intersects: bool = false;
317+
318+
//if (object1.physics.shape['radius'])
319+
//{
320+
// intersects = CircleUtils.intersectsRectangle(object1.physics.shape, object2.physics.shape.bounds)
321+
//}
322+
//else
323+
//{
324+
// intersects = RectangleUtils.intersects(object1.physics.shape.bounds, object2.physics.shape.bounds)
325+
//}
326+
315327
if (RectangleUtils.intersects(object1.physics.shape.bounds, object2.physics.shape.bounds))
316328
{
317329
//var maxOverlap: number = object1.physics.shape.deltaXAbs + object2.physics.shape.deltaXAbs + Collision.OVERLAP_BIAS;

Phaser/utils/CircleUtils.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,62 @@ module Phaser {
131131

132132
}
133133

134+
135+
/*
136+
public static boolean intersect(Rectangle r, Circle c)
137+
{
138+
float cx = Math.abs(c.x - r.x - r.halfWidth);
139+
float xDist = r.halfWidth + c.radius;
140+
if (cx > xDist)
141+
return false;
142+
float cy = Math.abs(c.y - r.y - r.halfHeight);
143+
float yDist = r.halfHeight + c.radius;
144+
if (cy > yDist)
145+
return false;
146+
if (cx <= r.halfWidth || cy <= r.halfHeight)
147+
return true;
148+
float xCornerDist = cx - r.halfWidth;
149+
float yCornerDist = cy - r.halfHeight;
150+
float xCornerDistSq = xCornerDist * xCornerDist;
151+
float yCornerDistSq = yCornerDist * yCornerDist;
152+
float maxCornerDistSq = c.radius * c.radius;
153+
return xCornerDistSq + yCornerDistSq <= maxCornerDistSq;
154+
}
155+
*/
156+
157+
static intersectsRectangle(c: Circle, r: Rectangle): bool {
158+
159+
var cx: number = Math.abs(c.x - r.x - r.halfWidth);
160+
var xDist: number = r.halfWidth + c.radius;
161+
162+
if (cx > xDist)
163+
{
164+
return false;
165+
}
166+
167+
var cy: number = Math.abs(c.y - r.y - r.halfHeight);
168+
var yDist: number = r.halfHeight + c.radius;
169+
170+
if (cy > yDist)
171+
{
172+
return false;
173+
}
174+
175+
if (cx <= r.halfWidth || cy <= r.halfHeight)
176+
{
177+
return true;
178+
}
179+
180+
var xCornerDist: number = cx - r.halfWidth;
181+
var yCornerDist: number = cy - r.halfHeight;
182+
var xCornerDistSq = xCornerDist * xCornerDist;
183+
var yCornerDistSq = yCornerDist * yCornerDist;
184+
var maxCornerDistSq = c.radius * c.radius;
185+
186+
return xCornerDistSq + yCornerDistSq <= maxCornerDistSq;
187+
188+
}
189+
134190
}
135191

136192
}

Tests/phaser.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12559,6 +12559,7 @@ var Phaser;
1255912559
this.physics = null;
1256012560
this.scale = new Phaser.Vec2(1, 1);
1256112561
}
12562+
this.diameter = diameter;
1256212563
this.radius = diameter / 2;
1256312564
this.bounds = new Phaser.Rectangle(x + Math.round(diameter / 2), y + Math.round(diameter / 2), diameter, diameter);
1256412565
this.position = new Phaser.Vec2(x + this.bounds.halfWidth, y + this.bounds.halfHeight);
@@ -12589,18 +12590,11 @@ var Phaser;
1258912590
this.bounds.height = height;
1259012591
};
1259112592
Circle.prototype.render = function (context) {
12592-
context.beginPath();
12593-
//context.strokeStyle = 'rgb(255,255,0)';
12594-
//context.strokeRect(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight, this.bounds.width, this.bounds.height);
12595-
//context.fillStyle = 'rgba(0,0,255,0.8)';
12596-
context.strokeStyle = 'rgba(0,0,255,0.5)';
12597-
context.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2);
12598-
//context.fill();
12599-
context.stroke();
12600-
context.closePath();
1260112593
// center point
12602-
context.fillStyle = 'rgb(255,255,0)';
12603-
context.fillRect(this.position.x, this.position.y, 2, 2);
12594+
context.fillStyle = 'rgba(255,0,0,0.5)';
12595+
context.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2);
12596+
context.rect(this.position.x, this.position.y, 2, 2);
12597+
context.fill();
1260412598
/*
1260512599
if (this.oH == 1)
1260612600
{
@@ -12644,9 +12638,11 @@ var Phaser;
1264412638
Object.defineProperty(Circle.prototype, "hullWidth", {
1264512639
get: function () {
1264612640
if(this.deltaX > 0) {
12647-
return this.bounds.width + this.deltaX;
12641+
//return this.bounds.width + this.deltaX;
12642+
return this.diameter + this.deltaX;
1264812643
} else {
12649-
return this.bounds.width - this.deltaX;
12644+
//return this.bounds.width - this.deltaX;
12645+
return this.diameter - this.deltaX;
1265012646
}
1265112647
},
1265212648
enumerable: true,
@@ -12655,9 +12651,11 @@ var Phaser;
1265512651
Object.defineProperty(Circle.prototype, "hullHeight", {
1265612652
get: function () {
1265712653
if(this.deltaY > 0) {
12658-
return this.bounds.height + this.deltaY;
12654+
//return this.bounds.height + this.deltaY;
12655+
return this.diameter + this.deltaY;
1265912656
} else {
12660-
return this.bounds.height - this.deltaY;
12657+
//return this.bounds.height - this.deltaY;
12658+
return this.diameter - this.deltaY;
1266112659
}
1266212660
},
1266312661
enumerable: true,

Tests/physics/circle 1.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
card = game.add.sprite(400, 300, 'card');
1515
//atari.texture.alpha = 0.5;
1616
//atari.scale.setTo(1.5, 1.5);
17-
atari.physics.setCircle(50);
17+
atari.physics.setCircle(100);
1818
//atari.physics.shape.setSize(150, 50);
19-
atari.physics.shape.offset.setTo(7, 5);
19+
//atari.physics.shape.offset.setTo(7, 5);
2020
//atari.physics.gravity.setTo(0, 2);
2121
atari.physics.bounce.setTo(0.7, 0.7);
22-
atari.physics.drag.setTo(10, 10);
22+
//atari.physics.drag.setTo(10, 10);
2323
card.physics.bounce.setTo(0.7, 0.7);
2424
}
2525
function update() {
@@ -38,12 +38,5 @@
3838
}
3939
function render() {
4040
atari.physics.renderDebugInfo(16, 16);
41-
game.stage.context.save();
42-
game.stage.context.beginPath();
43-
game.stage.context.strokeStyle = 'rgba(255,0,255,0.5)';
44-
game.stage.context.arc(200, 200, 50, 0, Math.PI * 2);
45-
game.stage.context.stroke();
46-
game.stage.context.closePath();
47-
game.stage.context.restore();
4841
}
4942
})();

Tests/physics/circle 1.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
//atari.texture.alpha = 0.5;
2424
//atari.scale.setTo(1.5, 1.5);
2525

26-
atari.physics.setCircle(50);
26+
atari.physics.setCircle(100);
2727

2828
//atari.physics.shape.setSize(150, 50);
29-
atari.physics.shape.offset.setTo(7, 5);
29+
//atari.physics.shape.offset.setTo(7, 5);
3030

3131
//atari.physics.gravity.setTo(0, 2);
3232
atari.physics.bounce.setTo(0.7, 0.7);
33-
atari.physics.drag.setTo(10, 10);
33+
//atari.physics.drag.setTo(10, 10);
3434

3535
card.physics.bounce.setTo(0.7, 0.7);
3636

@@ -65,15 +65,6 @@
6565

6666
atari.physics.renderDebugInfo(16, 16);
6767

68-
game.stage.context.save();
69-
game.stage.context.beginPath();
70-
game.stage.context.strokeStyle = 'rgba(255,0,255,0.5)';
71-
game.stage.context.arc(200, 200, 50, 0, Math.PI * 2);
72-
game.stage.context.stroke();
73-
game.stage.context.closePath();
74-
game.stage.context.restore();
75-
76-
7768
}
7869

7970
})();

build/phaser.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6705,6 +6705,7 @@ module Phaser.Physics {
67056705
public scale: Vec2;
67066706
public bounds: Rectangle;
67076707
public radius: number;
6708+
public diameter: number;
67086709
public preUpdate(): void;
67096710
public update(): void;
67106711
public setSize(width: number, height: number): void;

build/phaser.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12559,6 +12559,7 @@ var Phaser;
1255912559
this.physics = null;
1256012560
this.scale = new Phaser.Vec2(1, 1);
1256112561
}
12562+
this.diameter = diameter;
1256212563
this.radius = diameter / 2;
1256312564
this.bounds = new Phaser.Rectangle(x + Math.round(diameter / 2), y + Math.round(diameter / 2), diameter, diameter);
1256412565
this.position = new Phaser.Vec2(x + this.bounds.halfWidth, y + this.bounds.halfHeight);
@@ -12589,18 +12590,11 @@ var Phaser;
1258912590
this.bounds.height = height;
1259012591
};
1259112592
Circle.prototype.render = function (context) {
12592-
context.beginPath();
12593-
//context.strokeStyle = 'rgb(255,255,0)';
12594-
//context.strokeRect(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight, this.bounds.width, this.bounds.height);
12595-
//context.fillStyle = 'rgba(0,0,255,0.8)';
12596-
context.strokeStyle = 'rgba(0,0,255,0.5)';
12597-
context.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2);
12598-
//context.fill();
12599-
context.stroke();
12600-
context.closePath();
1260112593
// center point
12602-
context.fillStyle = 'rgb(255,255,0)';
12603-
context.fillRect(this.position.x, this.position.y, 2, 2);
12594+
context.fillStyle = 'rgba(255,0,0,0.5)';
12595+
context.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2);
12596+
context.rect(this.position.x, this.position.y, 2, 2);
12597+
context.fill();
1260412598
/*
1260512599
if (this.oH == 1)
1260612600
{
@@ -12644,9 +12638,11 @@ var Phaser;
1264412638
Object.defineProperty(Circle.prototype, "hullWidth", {
1264512639
get: function () {
1264612640
if(this.deltaX > 0) {
12647-
return this.bounds.width + this.deltaX;
12641+
//return this.bounds.width + this.deltaX;
12642+
return this.diameter + this.deltaX;
1264812643
} else {
12649-
return this.bounds.width - this.deltaX;
12644+
//return this.bounds.width - this.deltaX;
12645+
return this.diameter - this.deltaX;
1265012646
}
1265112647
},
1265212648
enumerable: true,
@@ -12655,9 +12651,11 @@ var Phaser;
1265512651
Object.defineProperty(Circle.prototype, "hullHeight", {
1265612652
get: function () {
1265712653
if(this.deltaY > 0) {
12658-
return this.bounds.height + this.deltaY;
12654+
//return this.bounds.height + this.deltaY;
12655+
return this.diameter + this.deltaY;
1265912656
} else {
12660-
return this.bounds.height - this.deltaY;
12657+
//return this.bounds.height - this.deltaY;
12658+
return this.diameter - this.deltaY;
1266112659
}
1266212660
},
1266312661
enumerable: true,

0 commit comments

Comments
 (0)