Patched the QuadTree, now working a whole lot better but am worried it's still working out too many collisions.

This commit is contained in:
Richard Davey
2013-09-04 04:54:43 +01:00
parent 3f3b6bfd35
commit b2263c16c6
3 changed files with 84 additions and 26 deletions
+32 -24
View File
@@ -44,14 +44,14 @@ Phaser.QuadTree = function (x, y, width, height, maxObjects, maxLevels, level) {
this.level = level || 0;
this.bounds = {
x: x,
y: y,
x: Math.round(x),
y: Math.round(y),
width: width,
height: height,
subWidth: Math.floor(width / 2 ),
subHeight: Math.floor(height / 2 ),
right: x + Math.floor(width / 2 ),
bottom: y + Math.floor(height / 2 )
subWidth: Math.floor(width / 2),
subHeight: Math.floor(height / 2),
right: Math.round(x) + Math.floor(width / 2),
bottom: Math.round(y) + Math.floor(height / 2)
};
this.objects = [];
@@ -65,18 +65,20 @@ Phaser.QuadTree.prototype = {
* Split the node into 4 subnodes
*/
split: function() {
this.level + 1;
// top right node
this.nodes[0] = new Phaser.QuadTree(this.bounds.right, this.bounds.y, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level + 1);
this.nodes[0] = new Phaser.QuadTree(this.bounds.right, this.bounds.y, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level);
// top left node
this.nodes[1] = new Phaser.QuadTree(this.bounds.x, this.bounds.y, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level + 1);
this.nodes[1] = new Phaser.QuadTree(this.bounds.x, this.bounds.y, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level);
// bottom left node
this.nodes[2] = new Phaser.QuadTree(this.bounds.x, this.bounds.bottom, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level + 1);
this.nodes[2] = new Phaser.QuadTree(this.bounds.x, this.bounds.bottom, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level);
// bottom right node
this.nodes[3] = new Phaser.QuadTree(this.bounds.right, this.bounds.bottom, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level + 1);
this.nodes[3] = new Phaser.QuadTree(this.bounds.right, this.bounds.bottom, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level);
},
@@ -89,30 +91,36 @@ Phaser.QuadTree.prototype = {
var index = -1;
// Duplicated comparisons, but they only get checked once in the flow so it saves creating temp. vars
// var verticalMidpoint = this.bounds.x + (this.bounds.width / 2);
var verticalMidpoint = this.bounds.right;
// var horizontalMidpoint = this.bounds.y + (this.bounds.height / 2);
var horizontalMidpoint = this.bounds.bottom;
var topQuadrant = (rect.y < this.bounds.bottom && rect.bottom < this.bounds.bottom);
var bottomQuadrant = (rect.y > this.bounds.bottom);
// rect can completely fit within the left quadrants
if (rect.x < this.bounds.bottom && rect.right < this.bounds.bottom)
if (rect.x < verticalMidpoint && rect.right < verticalMidpoint)
{
if ((rect.y < this.bounds.right && rect.bottom < this.bounds.right))
if (topQuadrant)
{
// rect can completely fit within the top quadrants
index = 1;
}
else if ((rect.y > this.bounds.right))
else if (bottomQuadrant)
{
// rect can completely fit within the bottom quadrants
index = 2;
}
}
else if (rect.x > this.bounds.bottom)
else if (rect.x > verticalMidpoint)
{
// rect can completely fit within the right quadrants
if ((rect.y < this.bounds.right && rect.bottom < this.bounds.right))
if (topQuadrant)
{
index = 0;
}
else if ((rect.y > this.bounds.right))
else if (bottomQuadrant)
{
index = 3;
}
@@ -135,7 +143,7 @@ Phaser.QuadTree.prototype = {
// if we have subnodes ...
// if (typeof this.nodes[0] !== 'undefined')
if (this.nodes[0])
if (this.nodes[0] != null)
{
index = this.getIndex(body.bounds);
@@ -152,7 +160,7 @@ Phaser.QuadTree.prototype = {
{
// Split if we don't already have subnodes
// if (typeof this.nodes[0] === 'undefined')
if (!this.nodes[0])
if (this.nodes[0] == null)
{
this.split();
}
@@ -180,9 +188,9 @@ Phaser.QuadTree.prototype = {
* @param Object pRect bounds of the object to be checked, with x, y, width, height
* @Return Array array with all detected objects
*/
retrieve: function (rect) {
retrieve: function (sprite) {
var index = this.getIndex(rect);
var index = this.getIndex(sprite.body.bounds);
var returnObjects = this.objects;
// if we have subnodes ...
@@ -192,14 +200,14 @@ Phaser.QuadTree.prototype = {
// if rect fits into a subnode ..
if (index !== -1)
{
returnObjects = returnObjects.concat(this.nodes[index].retrieve(rect));
returnObjects = returnObjects.concat(this.nodes[index].retrieve(sprite));
}
else
{
// if rect does not fit into a subnode, check it against all subnodes
for (var i = 0, len = this.nodes.length; i < len; i++)
{
returnObjects = returnObjects.concat(this.nodes[i].retrieve(rect));
returnObjects = returnObjects.concat(this.nodes[i].retrieve(sprite));
}
}
}