
286 Chapter 12 Collision Detection
two objects, it should be added to the occupied list. Likewise, when we remove an
object from a cell, if the cell now contains just one object, it should be removed fro m
the list:
class Grid
{
// ... Previous code as before ...
std::vector<ObjectSet*> activeSets;
def add(Object* object)
{
unsigned location = getLocationIndex(object->center);
ObjectSet *set = locations + location;
if (set.size() == 1) activeSets.insert(set);
set.add(object);
}
def remove(Object* object)
{
unsigned location = getLocationIndex(object->center);
ObjectSet *set = locations + location;
if (set.size() == 2) activeSets.erase(set);
set.remove(object); ...