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
+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;
},