Fixed the Body collide issues and optimised the process at the same time. Now the QuadTree appears to work perfectly as a result. Bonus!

This commit is contained in:
Richard Davey
2013-09-08 01:24:59 +01:00
parent bb5d8ca170
commit fe6664eac7
7 changed files with 116 additions and 44 deletions
+14 -2
View File
@@ -24,17 +24,29 @@
function create() {
player = game.add.sprite(400, 300, 'ship');
player.alpha = 0.2;
player.body.collideWorldBounds = true;
player.body.bounce.setTo(1, 1);
// The body offset is from the ANCHOR point of the Sprite, not the top-left (or center, etc)
player.body.setSize(60, 60, 0, 0);
player.anchor.setTo(0.5, 0.5);
player.body.velocity.x = 100;
}
function update() {
// console.log(Math.floor(player.center.x), Math.floor(player.center.y));
player.angle++;
player.scale.x += 0.005;
player.scale.y += 0.005;
}
function render() {
game.debug.renderSpriteCorners(player,true,true);
game.debug.renderRectangle(player.body.bounds);
// game.debug.renderSpriteCorners(player,true,true);
game.debug.renderRectangle(player.body);
game.debug.renderPixel(400, 300, 'rgb(255,0,0)');
game.debug.renderPoint(player.center, 'rgb(255,254,0)');
}
})();
+2 -2
View File
@@ -56,8 +56,8 @@
function render() {
game.debug.renderSpriteCorners(bunny, true, true);
game.debug.renderRectangle(bunny.body.bounds);
game.debug.renderRectangle(wall.body.bounds);
game.debug.renderRectangle(bunny.body);
game.debug.renderRectangle(wall.body);
}
+4 -3
View File
@@ -25,7 +25,7 @@
function create() {
player = game.add.sprite(400, 500, 'ship');
// player.anchor.setTo(0.5, 0.5);
player.anchor.setTo(0.5, 0.5);
aliens = game.add.group();
@@ -33,13 +33,14 @@
{
for (var x = 0; x < 10; x++)
{
aliens.create(x * 48, y * 64, 'alien');
aliens.create(x * 48, y * 50, 'alien');
}
}
aliens.x = 100;
aliens.y = 50;
var tween = game.add.tween(aliens).to({x: 200}, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true);
var tween = game.add.tween(aliens).to({x: 200}, 3000, Phaser.Easing.Linear.None, true, 0, 1000, true);
tween.onComplete.add(descend, this);
}
+3 -3
View File
@@ -39,7 +39,7 @@
ship = game.add.sprite(400, 400, 'ship');
ship.body.collideWorldBounds = true;
ship.body.bounce.setTo(0.5, 0.5);
ship.body.bounce.setTo(1, 1);
}
@@ -64,14 +64,14 @@
}
game.physics.collideGroup(aliens);
// total = game.physics.overlap(ship);
total = game.physics.overlap(ship);
}
function render() {
game.debug.renderQuadTree(game.physics.quadTree);
game.debug.renderRectangle(ship.body.bounds);
game.debug.renderRectangle(ship.body);
// game.debug.renderText('total: ' + total.length, 32, 50);
+3 -3
View File
@@ -104,7 +104,7 @@ Phaser.QuadTree.prototype = {
// if we have subnodes ...
if (this.nodes[0] != null)
{
index = this.getIndex(body.bounds);
index = this.getIndex(body);
if (index !== -1)
{
@@ -126,7 +126,7 @@ Phaser.QuadTree.prototype = {
// Add objects to subnodes
while (i < this.objects.length)
{
index = this.getIndex(this.objects[i].bounds);
index = this.getIndex(this.objects[i]);
if (index !== -1)
{
@@ -192,7 +192,7 @@ Phaser.QuadTree.prototype = {
var returnObjects = this.objects;
sprite.body.quadTreeIndex = this.getIndex(sprite.body.bounds);
sprite.body.quadTreeIndex = this.getIndex(sprite.body);
// Temp store for the node IDs this sprite is in, we can use this for fast elimination later
sprite.body.quadTreeIDs.push(this.ID);
+85 -31
View File
@@ -5,7 +5,6 @@ Phaser.Physics.Arcade.Body = function (sprite) {
this.offset = new Phaser.Point;
// the top-left of the Body
this.x = sprite.x;
this.y = sprite.y;
@@ -19,8 +18,6 @@ Phaser.Physics.Arcade.Body = function (sprite) {
this.halfWidth = Math.floor(sprite.currentFrame.sourceSizeW / 2);
this.halfHeight = Math.floor(sprite.currentFrame.sourceSizeH / 2);
this.bounds = new Phaser.Rectangle(sprite.x, sprite.y, this.width, this.height);
// Scale value cache
this._sx = sprite.scale.x;
this._sy = sprite.scale.y;
@@ -69,8 +66,6 @@ Phaser.Physics.Arcade.Body.prototype = {
this.height = this.sourceHeight * scaleY;
this.halfWidth = Math.floor(this.width / 2);
this.halfHeight = Math.floor(this.height / 2);
this.bounds.width = this.width;
this.bounds.height = this.height;
this._sx = scaleX;
this._sy = scaleY;
}
@@ -94,19 +89,16 @@ Phaser.Physics.Arcade.Body.prototype = {
this.lastX = this.x;
this.lastY = this.y;
this.x = this.sprite.x - this.offset.x + (this.sprite.anchor.x * this.width));
this.y = this.sprite.y - this.offset.y + (this.sprite.anchor.y * this.height));
this.rotation = this.sprite.angle;
this.x = (this.sprite.x - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.y = (this.sprite.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
if (this.moves)
{
this.game.physics.updateMotion(this);
}
this.bounds.x = this.x;
this.bounds.y = this.y;
if (this.collideWorldBounds)
{
this.checkWorldBounds();
@@ -120,44 +112,52 @@ Phaser.Physics.Arcade.Body.prototype = {
}
// Adjust the sprite based on all of the above, so the x/y coords will be correct going into the State update
// this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width);
// this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height);
// this.sprite.x = this.x;
// this.sprite.y = this.y;
this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width);
this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height);
if (this.allowRotation)
{
// this.sprite.angle = this.rotation;
this.sprite.angle = this.rotation;
}
},
/*
postUpdate: function () {
this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width);
this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height);
if (this.allowRotation)
{
this.sprite.angle = this.rotation;
}
},
*/
checkWorldBounds: function () {
if (this.bounds.x < this.game.world.bounds.x)
if (this.x < this.game.world.bounds.x)
{
this.x = this.game.world.bounds.x;
this.velocity.x *= -1;
this.velocity.x *= this.bounce.x;
this.velocity.x *= -this.bounce.x;
}
else if (this.bounds.right > this.game.world.bounds.right)
else if (this.right > this.game.world.bounds.right)
{
this.x = this.game.world.bounds.right - this.width;
this.velocity.x *= -1;
this.velocity.x *= this.bounce.x;
this.velocity.x *= -this.bounce.x;
}
if (this.bounds.y < this.game.world.bounds.y)
if (this.y < this.game.world.bounds.y)
{
this.y = this.game.world.bounds.y;
this.velocity.y *= -1;
this.velocity.y *= this.bounce.y;
this.velocity.y *= -this.bounce.y;
}
else if (this.bounds.bottom > this.game.world.bounds.bottom)
else if (this.bottom > this.game.world.bounds.bottom)
{
this.y = this.game.world.bounds.bottom - this.height;
this.velocity.y *= -1;
this.velocity.y *= this.bounce.y;
this.velocity.y *= -this.bounce.y;
}
},
@@ -173,8 +173,6 @@ Phaser.Physics.Arcade.Body.prototype = {
this.height = this.sourceHeight * this._sy;
this.halfWidth = Math.floor(this.width / 2);
this.halfHeight = Math.floor(this.height / 2);
this.bounds.width = this.width;
this.bounds.height = this.height;
this.offset.setTo(offsetX, offsetY);
},
@@ -195,4 +193,60 @@ Phaser.Physics.Arcade.Body.prototype = {
return this.y - this.lastY;
}
};
};
Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "bottom", {
/**
* The sum of the y and height properties. Changing the bottom property of a Rectangle object has no effect on the x, y and width properties, but does change the height property.
* @method bottom
* @return {Number}
**/
get: function () {
return this.y + this.height;
},
/**
* The sum of the y and height properties. Changing the bottom property of a Rectangle object has no effect on the x, y and width properties, but does change the height property.
* @method bottom
* @param {Number} value
**/
set: function (value) {
if(value <= this.y) {
this.height = 0;
} else {
this.height = (this.y - value);
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "right", {
/**
* The sum of the x and width properties. Changing the right property of a Rectangle object has no effect on the x, y and height properties.
* However it does affect the width property.
* @method right
* @return {Number}
**/
get: function () {
return this.x + this.width;
},
/**
* The sum of the x and width properties. Changing the right property of a Rectangle object has no effect on the x, y and height properties.
* However it does affect the width property.
* @method right
* @param {Number} value
**/
set: function (value) {
if(value <= this.x) {
this.width = 0;
} else {
this.width = this.x + value;
}
},
enumerable: true,
configurable: true
});
+5
View File
@@ -28,6 +28,7 @@ Phaser.Utils.Debug.prototype = {
renderShadow: true,
currentX: 0,
currentY: 0,
currentAlpha: 1,
context: null,
/**
@@ -55,16 +56,20 @@ Phaser.Utils.Debug.prototype = {
this.currentColor = color;
}
this.currentAlpha = this.context.globalAlpha;
this.context.save();
this.context.setTransform(1, 0, 0, 1, 0, 0);
this.context.fillStyle = color;
this.context.font = this.font;
this.context.globalAlpha = 1;
},
stop: function () {
this.context.restore();
this.context.globalAlpha = this.currentAlpha;
},