Brand new highly optimised QuadTree added, populated by the game loop and world ready to be queried as often as you want in your game loop, without creating hundreds of new QuadTrees each frame.

This commit is contained in:
Richard Davey
2013-09-04 03:48:15 +01:00
parent 2a353a0327
commit 3f3b6bfd35
8 changed files with 362 additions and 83 deletions
+24 -23
View File
@@ -8,9 +8,16 @@ Phaser.Physics.Arcade = function (game) {
this.bounds = new Phaser.Rectangle(0, 0, game.world.width, game.world.height);
/**
* Used by the QuadTree to set the maximum number of objects
* @type {number}
*/
this.worldDivisions = 6;
this.maxObjects = 10;
/**
* Used by the QuadTree to set the maximum number of levels
* @type {number}
*/
this.maxLevels = 4;
this.LEFT = 0x0001;
this.RIGHT = 0x0010;
@@ -25,6 +32,8 @@ Phaser.Physics.Arcade = function (game) {
this.OVERLAP_BIAS = 4;
this.TILE_OVERLAP = false;
this.quadTree = null;
// avoid gc spikes by caching these values for re-use
this._obj1Bounds = new Phaser.Rectangle;
this._obj2Bounds = new Phaser.Rectangle;
@@ -126,6 +135,20 @@ Phaser.Physics.Arcade.prototype = {
return velocity;
},
preUpdate: function () {
// Create our tree which all of the Physics bodies will add themselves to
this.quadTree = new Phaser.QuadTree(this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels);
},
postUpdate: function () {
// Clear the tree ready for the next update
this.quadTree.clear();
},
/**
@@ -139,28 +162,6 @@ Phaser.Physics.Arcade.prototype = {
*/
overlap: function (object1, object2, notifyCallback, processCallback) {
if (object1 == null)
{
object1 = this._game.world.group;
}
if (object2 == object1)
{
object2 = null;
}
Phaser.QuadTree.divisions = this.worldDivisions;
var quadTree = new Phaser.QuadTree(this._game.world.bounds.x, this._game.world.bounds.y, this._game.world.bounds.width, this._game.world.bounds.height);
quadTree.load(object1, object2, notifyCallback, processCallback);
var result = quadTree.execute();
quadTree.destroy();
quadTree = null;
return result;
},
+44
View File
@@ -58,6 +58,8 @@ Phaser.Physics.Arcade.Body = function (sprite) {
this.allowRotation = false;
this.allowGravity = true;
this.collideWorldBounds = false;
this.lastX = sprite.x;
this.lastY = sprite.y;
@@ -91,10 +93,50 @@ Phaser.Physics.Arcade.Body.prototype = {
this.bounds.x = this.x;
this.bounds.y = this.y;
if (this.allowCollisions & this.ANY)
{
this.game.physics.quadTree.insert(this);
}
},
checkWorldBounds: function () {
if (this.bounds.x < this.game.world.bounds.x)
{
this.x = this.game.world.bounds.x;
this.velocity.x *= -1;
this.velocity.x *= this.bounce.x;
}
else if (this.bounds.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;
}
if (this.bounds.y < this.game.world.bounds.y)
{
this.y = this.game.world.bounds.y;
this.velocity.y *= -1;
this.velocity.y *= this.bounce.y;
}
else if (this.bounds.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;
}
},
postUpdate: function () {
if (this.collideWorldBounds)
{
this.checkWorldBounds();
}
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);
@@ -122,6 +164,7 @@ Phaser.Physics.Arcade.Body.prototype = {
},
/*
hullWidth: function () {
if (this.deltaX() > 0)
@@ -173,6 +216,7 @@ Phaser.Physics.Arcade.Body.prototype = {
}
},
*/
deltaAbsX: function () {
return (this.deltaX() > 0 ? this.deltaX() : -this.deltaX());