diff --git a/Phaser/physics/Circle.ts b/Phaser/physics/Circle.ts index 52fd0c99..2e910e9b 100644 --- a/Phaser/physics/Circle.ts +++ b/Phaser/physics/Circle.ts @@ -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; } - } } \ No newline at end of file diff --git a/Phaser/physics/PhysicsManager.ts b/Phaser/physics/PhysicsManager.ts index 49d0dcbd..c34327ec 100644 --- a/Phaser/physics/PhysicsManager.ts +++ b/Phaser/physics/PhysicsManager.ts @@ -1,6 +1,7 @@ /// /// /// +/// /** * 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; diff --git a/Phaser/utils/CircleUtils.ts b/Phaser/utils/CircleUtils.ts index f3df6bd1..51fa2005 100644 --- a/Phaser/utils/CircleUtils.ts +++ b/Phaser/utils/CircleUtils.ts @@ -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; + + } + } } \ No newline at end of file diff --git a/Tests/phaser.js b/Tests/phaser.js index b01a4f9c..bb887891 100644 --- a/Tests/phaser.js +++ b/Tests/phaser.js @@ -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, diff --git a/Tests/physics/circle 1.js b/Tests/physics/circle 1.js index 2a31a112..d74bba72 100644 --- a/Tests/physics/circle 1.js +++ b/Tests/physics/circle 1.js @@ -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(); } })(); diff --git a/Tests/physics/circle 1.ts b/Tests/physics/circle 1.ts index 123c9a86..6f66e48e 100644 --- a/Tests/physics/circle 1.ts +++ b/Tests/physics/circle 1.ts @@ -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(); - - } })(); diff --git a/build/phaser.d.ts b/build/phaser.d.ts index 904a266f..07065819 100644 --- a/build/phaser.d.ts +++ b/build/phaser.d.ts @@ -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; diff --git a/build/phaser.js b/build/phaser.js index b01a4f9c..bb887891 100644 --- a/build/phaser.js +++ b/build/phaser.js @@ -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,