Added circleToRect intersection check.

This commit is contained in:
Richard Davey
2013-05-31 05:28:36 +01:00
parent 39e7b84cd9
commit 23ec3816db
8 changed files with 115 additions and 69 deletions
+14 -17
View File
@@ -29,6 +29,7 @@ module Phaser.Physics {
this.scale = new Vec2(1, 1);
}
this.diameter = diameter;
this.radius = diameter / 2;
this.bounds = new Rectangle(x + Math.round(diameter / 2), y + Math.round(diameter / 2), diameter, diameter);
this.position = new Vec2(x + this.bounds.halfWidth, y + this.bounds.halfHeight);
@@ -49,6 +50,7 @@ module Phaser.Physics {
public bounds: Rectangle;
public radius: number;
public diameter: number;
public preUpdate() {
@@ -89,19 +91,11 @@ module Phaser.Physics {
public render(context:CanvasRenderingContext2D) {
context.beginPath();
//context.strokeStyle = 'rgb(255,255,0)';
//context.strokeRect(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight, this.bounds.width, this.bounds.height);
//context.fillStyle = 'rgba(0,0,255,0.8)';
context.strokeStyle = 'rgba(0,0,255,0.5)';
context.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2);
//context.fill();
context.stroke();
context.closePath();
// center point
context.fillStyle = 'rgb(255,255,0)';
context.fillRect(this.position.x, this.position.y, 2, 2);
context.fillStyle = 'rgba(255,0,0,0.5)';
context.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2);
context.rect(this.position.x, this.position.y, 2, 2);
context.fill();
/*
if (this.oH == 1)
@@ -149,11 +143,13 @@ module Phaser.Physics {
if (this.deltaX > 0)
{
return this.bounds.width + this.deltaX;
//return this.bounds.width + this.deltaX;
return this.diameter + this.deltaX;
}
else
{
return this.bounds.width - this.deltaX;
//return this.bounds.width - this.deltaX;
return this.diameter - this.deltaX;
}
}
@@ -162,11 +158,13 @@ module Phaser.Physics {
if (this.deltaY > 0)
{
return this.bounds.height + this.deltaY;
//return this.bounds.height + this.deltaY;
return this.diameter + this.deltaY;
}
else
{
return this.bounds.height - this.deltaY;
//return this.bounds.height - this.deltaY;
return this.diameter - this.deltaY;
}
}
@@ -213,7 +211,6 @@ module Phaser.Physics {
return this.position.y - this.oldPosition.y;
}
}
}
+12
View File
@@ -1,6 +1,7 @@
/// <reference path="../Game.ts" />
/// <reference path="IPhysicsShape.ts" />
/// <reference path="../utils/RectangleUtils.ts" />
/// <reference path="../utils/CircleUtils.ts" />
/**
* Phaser - PhysicsManager
@@ -312,6 +313,17 @@ module Phaser.Physics {
if (object1.physics.shape.deltaX != object2.physics.shape.deltaX)
{
//var intersects: bool = false;
//if (object1.physics.shape['radius'])
//{
// intersects = CircleUtils.intersectsRectangle(object1.physics.shape, object2.physics.shape.bounds)
//}
//else
//{
// intersects = RectangleUtils.intersects(object1.physics.shape.bounds, object2.physics.shape.bounds)
//}
if (RectangleUtils.intersects(object1.physics.shape.bounds, object2.physics.shape.bounds))
{
//var maxOverlap: number = object1.physics.shape.deltaXAbs + object2.physics.shape.deltaXAbs + Collision.OVERLAP_BIAS;
+56
View File
@@ -131,6 +131,62 @@ module Phaser {
}
/*
public static boolean intersect(Rectangle r, Circle c)
{
float cx = Math.abs(c.x - r.x - r.halfWidth);
float xDist = r.halfWidth + c.radius;
if (cx > xDist)
return false;
float cy = Math.abs(c.y - r.y - r.halfHeight);
float yDist = r.halfHeight + c.radius;
if (cy > yDist)
return false;
if (cx <= r.halfWidth || cy <= r.halfHeight)
return true;
float xCornerDist = cx - r.halfWidth;
float yCornerDist = cy - r.halfHeight;
float xCornerDistSq = xCornerDist * xCornerDist;
float yCornerDistSq = yCornerDist * yCornerDist;
float maxCornerDistSq = c.radius * c.radius;
return xCornerDistSq + yCornerDistSq <= maxCornerDistSq;
}
*/
static intersectsRectangle(c: Circle, r: Rectangle): bool {
var cx: number = Math.abs(c.x - r.x - r.halfWidth);
var xDist: number = r.halfWidth + c.radius;
if (cx > xDist)
{
return false;
}
var cy: number = Math.abs(c.y - r.y - r.halfHeight);
var yDist: number = r.halfHeight + c.radius;
if (cy > yDist)
{
return false;
}
if (cx <= r.halfWidth || cy <= r.halfHeight)
{
return true;
}
var xCornerDist: number = cx - r.halfWidth;
var yCornerDist: number = cy - r.halfHeight;
var xCornerDistSq = xCornerDist * xCornerDist;
var yCornerDistSq = yCornerDist * yCornerDist;
var maxCornerDistSq = c.radius * c.radius;
return xCornerDistSq + yCornerDistSq <= maxCornerDistSq;
}
}
}
+13 -15
View File
@@ -12559,6 +12559,7 @@ var Phaser;
this.physics = null;
this.scale = new Phaser.Vec2(1, 1);
}
this.diameter = diameter;
this.radius = diameter / 2;
this.bounds = new Phaser.Rectangle(x + Math.round(diameter / 2), y + Math.round(diameter / 2), diameter, diameter);
this.position = new Phaser.Vec2(x + this.bounds.halfWidth, y + this.bounds.halfHeight);
@@ -12589,18 +12590,11 @@ var Phaser;
this.bounds.height = height;
};
Circle.prototype.render = function (context) {
context.beginPath();
//context.strokeStyle = 'rgb(255,255,0)';
//context.strokeRect(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight, this.bounds.width, this.bounds.height);
//context.fillStyle = 'rgba(0,0,255,0.8)';
context.strokeStyle = 'rgba(0,0,255,0.5)';
context.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2);
//context.fill();
context.stroke();
context.closePath();
// center point
context.fillStyle = 'rgb(255,255,0)';
context.fillRect(this.position.x, this.position.y, 2, 2);
context.fillStyle = 'rgba(255,0,0,0.5)';
context.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2);
context.rect(this.position.x, this.position.y, 2, 2);
context.fill();
/*
if (this.oH == 1)
{
@@ -12644,9 +12638,11 @@ var Phaser;
Object.defineProperty(Circle.prototype, "hullWidth", {
get: function () {
if(this.deltaX > 0) {
return this.bounds.width + this.deltaX;
//return this.bounds.width + this.deltaX;
return this.diameter + this.deltaX;
} else {
return this.bounds.width - this.deltaX;
//return this.bounds.width - this.deltaX;
return this.diameter - this.deltaX;
}
},
enumerable: true,
@@ -12655,9 +12651,11 @@ var Phaser;
Object.defineProperty(Circle.prototype, "hullHeight", {
get: function () {
if(this.deltaY > 0) {
return this.bounds.height + this.deltaY;
//return this.bounds.height + this.deltaY;
return this.diameter + this.deltaY;
} else {
return this.bounds.height - this.deltaY;
//return this.bounds.height - this.deltaY;
return this.diameter - this.deltaY;
}
},
enumerable: true,
+3 -10
View File
@@ -14,12 +14,12 @@
card = game.add.sprite(400, 300, 'card');
//atari.texture.alpha = 0.5;
//atari.scale.setTo(1.5, 1.5);
atari.physics.setCircle(50);
atari.physics.setCircle(100);
//atari.physics.shape.setSize(150, 50);
atari.physics.shape.offset.setTo(7, 5);
//atari.physics.shape.offset.setTo(7, 5);
//atari.physics.gravity.setTo(0, 2);
atari.physics.bounce.setTo(0.7, 0.7);
atari.physics.drag.setTo(10, 10);
//atari.physics.drag.setTo(10, 10);
card.physics.bounce.setTo(0.7, 0.7);
}
function update() {
@@ -38,12 +38,5 @@
}
function render() {
atari.physics.renderDebugInfo(16, 16);
game.stage.context.save();
game.stage.context.beginPath();
game.stage.context.strokeStyle = 'rgba(255,0,255,0.5)';
game.stage.context.arc(200, 200, 50, 0, Math.PI * 2);
game.stage.context.stroke();
game.stage.context.closePath();
game.stage.context.restore();
}
})();
+3 -12
View File
@@ -23,14 +23,14 @@
//atari.texture.alpha = 0.5;
//atari.scale.setTo(1.5, 1.5);
atari.physics.setCircle(50);
atari.physics.setCircle(100);
//atari.physics.shape.setSize(150, 50);
atari.physics.shape.offset.setTo(7, 5);
//atari.physics.shape.offset.setTo(7, 5);
//atari.physics.gravity.setTo(0, 2);
atari.physics.bounce.setTo(0.7, 0.7);
atari.physics.drag.setTo(10, 10);
//atari.physics.drag.setTo(10, 10);
card.physics.bounce.setTo(0.7, 0.7);
@@ -65,15 +65,6 @@
atari.physics.renderDebugInfo(16, 16);
game.stage.context.save();
game.stage.context.beginPath();
game.stage.context.strokeStyle = 'rgba(255,0,255,0.5)';
game.stage.context.arc(200, 200, 50, 0, Math.PI * 2);
game.stage.context.stroke();
game.stage.context.closePath();
game.stage.context.restore();
}
})();
+1
View File
@@ -6705,6 +6705,7 @@ module Phaser.Physics {
public scale: Vec2;
public bounds: Rectangle;
public radius: number;
public diameter: number;
public preUpdate(): void;
public update(): void;
public setSize(width: number, height: number): void;
+13 -15
View File
@@ -12559,6 +12559,7 @@ var Phaser;
this.physics = null;
this.scale = new Phaser.Vec2(1, 1);
}
this.diameter = diameter;
this.radius = diameter / 2;
this.bounds = new Phaser.Rectangle(x + Math.round(diameter / 2), y + Math.round(diameter / 2), diameter, diameter);
this.position = new Phaser.Vec2(x + this.bounds.halfWidth, y + this.bounds.halfHeight);
@@ -12589,18 +12590,11 @@ var Phaser;
this.bounds.height = height;
};
Circle.prototype.render = function (context) {
context.beginPath();
//context.strokeStyle = 'rgb(255,255,0)';
//context.strokeRect(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight, this.bounds.width, this.bounds.height);
//context.fillStyle = 'rgba(0,0,255,0.8)';
context.strokeStyle = 'rgba(0,0,255,0.5)';
context.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2);
//context.fill();
context.stroke();
context.closePath();
// center point
context.fillStyle = 'rgb(255,255,0)';
context.fillRect(this.position.x, this.position.y, 2, 2);
context.fillStyle = 'rgba(255,0,0,0.5)';
context.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2);
context.rect(this.position.x, this.position.y, 2, 2);
context.fill();
/*
if (this.oH == 1)
{
@@ -12644,9 +12638,11 @@ var Phaser;
Object.defineProperty(Circle.prototype, "hullWidth", {
get: function () {
if(this.deltaX > 0) {
return this.bounds.width + this.deltaX;
//return this.bounds.width + this.deltaX;
return this.diameter + this.deltaX;
} else {
return this.bounds.width - this.deltaX;
//return this.bounds.width - this.deltaX;
return this.diameter - this.deltaX;
}
},
enumerable: true,
@@ -12655,9 +12651,11 @@ var Phaser;
Object.defineProperty(Circle.prototype, "hullHeight", {
get: function () {
if(this.deltaY > 0) {
return this.bounds.height + this.deltaY;
//return this.bounds.height + this.deltaY;
return this.diameter + this.deltaY;
} else {
return this.bounds.height - this.deltaY;
//return this.bounds.height - this.deltaY;
return this.diameter - this.deltaY;
}
},
enumerable: true,