Original Post
Hello, I need your help. I´m developing a quadtree class for a small game engine. Currently my project targets normal Windows but in the future I will migrate the code to compile for Windows Mobile. So don´t wonder about using fixed-point math. The creation of a quadtree runs fine but my deletion method works not right. Every quadtree-node has a pointer to its parent and 4 pointers to its children. The method should delete this node and all its children. The quadrant order is 1 -> top left, 2->top right, 3->bottom left, 4->bottom right. Please have a look at my code. I´m staring at it but I can´t find anything wrong. I don´t know if I can do "delete this" at the last line of the code? Thank you for your help!
// Delete a QuadtreeNode and its children
void QuadtreeNode::DeleteQuadtree()
{
for(int i = 0; i < 4; i++)
{
// Recursively delete children of this node
if(mpChild)
{
mpChild->DeleteQuadtree();
}
}
// Update parents child pointer to NULL
if(mpParent)
{
// Compute the quadrant the obects AAR is in
// and set parents child pointer to NULL
if(mCenter.y > mpParent->mCenter.y)
{
if(mCenter.x < mpParent->mCenter.x)
mpParent->mpChild[0] = NULL;
else
mpParent->mpChild[1] = NULL;
}
else
{
if(mCenter.x < mpParent->mCenter.x)
mpParent->mpChild[2] = NULL;
else
mpParent->mpChild[3] = NULL;
}
}
// Delete this node
delete this;
}