using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
namespace QtreeTest
{
public class QTreeNode
{
QTreeNode[] childNodes = new QTreeNode[4];
public List<Sprite> sprites = new List<Sprite>();
int quad = -1;
int generation = 0;
public QTreeNode parent = null;
public Rectangle areaRect;
QTreeNode(int x, int y, int w, int h, QTreeNode parent = null, int generation = 0, int quad = -1)
{
areaRect = new Rectangle(x, y, w, h);
this.parent = parent;
this.quad = quad;
}
void update()
{
if(sprites.Capacity == 2)
{
detectCollision();
}
else if(sprites.Capacity > 2 && generation <= 4)
{
while (sprites.Capacity > 2)
{
for (int i = 0; i < 3; i++)
{
switch (i)
{
case 0:
childNodes[i] = new QTreeNode(0, 0, this.areaRect.Width / 2, this.areaRect.Height / 2, this, this.generation + 1, 1);
foreach (Sprite s in sprites)
{
if(s.collisionRect.Intersects(childNodes[i].areaRect))
{
this.sprites.Add(s);
this.sprites.Remove(s);
}
}
break;
case 1:
childNodes[i] = new QTreeNode(this.areaRect.Width / 2, 0, this.areaRect.Width, this.areaRect.Height / 2, this, this.generation + 1, 2);
foreach (Sprite s in sprites)
{
if (s.collisionRect.Intersects(childNodes[i].areaRect))
{
this.sprites.Add(s);
this.sprites.Remove(s);
}
}
break;
case 2:
childNodes[i] = new QTreeNode(this.areaRect.Width / 2, this.areaRect.Height / 2, this.areaRect.Width, this.areaRect.Height, this, this.generation + 1, 3);
foreach (Sprite s in sprites)
{
if (s.collisionRect.Intersects(childNodes[i].areaRect))
{
this.sprites.Add(s);
this.sprites.Remove(s);
}
}
break;
case 3:
childNodes[i] = new QTreeNode(0, this.areaRect.Height / 2, this.areaRect.Width, this.areaRect.Height, this, this.generation + 1, 4);
foreach (Sprite s in sprites)
{
if (s.collisionRect.Intersects(childNodes[i].areaRect))
{
this.sprites.Add(s);
this.sprites.Remove(s);
}
}
break;
}
}
}
}
else if (generation > 4)
{
detectCollision();
}
}
public void nodeSpriteCheck(Sprite s)
{
if (s.collisionRect.Intersects(this.areaRect))
{
for (int i = 0; i < s.interGrid.Length - 1; i++)
{
if (s.interGrid[i] == null)
{
s.interGrid[i] = this;
break;
}
}
childNodes[1].nodeSpriteCheck(s);
childNodes[2].nodeSpriteCheck(s);
childNodes[3].nodeSpriteCheck(s);
childNodes[4].nodeSpriteCheck(s);
}
}
private void deleteEmptyChildren()
{
if (hasChild())
{
if (childNodes[1].sprites.Capacity == 0 && childNodes[2].sprites.Capacity == 0 && childNodes[3].sprites.Capacity == 0 && childNodes[4].sprites.Capacity == 0)
{
for (int i = 0; i < 4; ++i)
{
childNodes[i] = null;
}
}
}
}
public bool hasChild()
{
if (childNodes[0] != null)
{
return true;
}
return false;
}
public bool detectCollision()
{
if (sprites[0] == null || sprites[1] == null)
{
return false;
}
else if (sprites[0].collisionRect.Intersects(sprites[1].collisionRect))
{
sprites[0].react(sprites[1]);
sprites[1].react(sprites[0]);
return true;
}
else
{
Exception ex = new Exception("QTree Error: Collission Detect failed");
throw ex;
}
}
}
}
If any of you have any comments/tips/anything to say I would be glad to hear from you.

New Topic/Question
Reply




MultiQuote







|