Skip to content

Instantly share code, notes, and snippets.

@Shaptic
Last active December 15, 2015 12:18
Show Gist options
  • Save Shaptic/5258800 to your computer and use it in GitHub Desktop.
Save Shaptic/5258800 to your computer and use it in GitHub Desktop.
CQuadTree::Update()
void CQuadTree::Update()
{
// Iterate over every object in the tree.
// This is why we stored them locally in Insert()
for(size_t i = 0; i < mp_allBodies.size(); ++i)
{
// Object has moved.
if(mp_allBodies[i]->needs_update)
{
// Remove the reference to the object from its current node.
// Many thanks to @LorenSchmidt for the catch.
for(auto i = mp_allBodies[i]->pNode->nodeObjects.begin();
i != mp_allBodies[i]->pNode->nodeObjects.end();
/* no third part */)
{
if(*i == mp_allBodies[i])
{
i = mp_allBodies[i]->pNode->nodeObjects.erase(i);
// If you can guarantee that you wont Insert() the same object
// multiple times, you can "break;" out of the loop here.
}
else
{
++i;
}
}
// RInsert() will keep trying to find a node by moving up the tree
// until it hits the root node. It will return false if it doesn't
// fit into the root, either, so that means the object has been moved
// off-screen, or off-tree.
this->RInsert(mp_allBodies[i], mp_allBodies[i]->pNode->mp_Parent);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment