Graphics Reference
In-Depth Information
// Now scan through list and unlink object 'obj'
int bucket= obj- > bucket;
Object *p = grid- > objectBucket[bucket];
// Special-case updating list header when object is first in list
if (p == obj) {
grid- > objectBucket[bucket] = obj- > pNextObject;
return;
}
// Traverse rest of list, unlinking 'obj' when found
while (p) {
// Keep q as trailing pointer to previous element
Object *q = p;
p=p- > pNextObject;
if (p == obj) {
q- > pNextObject = p- > pNextObject;
// unlink by bypassing
return;
}
}
assert(0);
// No such object in hgrid
}
Checking an object for collision against objects in the hgrid can be done as follows.
It is assumed that nothing is known about the number of objects tested at a time,
and thus all grid levels are traversed (except when it can be concluded that there are
no more objects to be found in the grid).
// Test collisions between object and all objects in hgrid
void CheckObjAgainstGrid(HGrid *grid, Object *obj,
void (*pCallbackFunc)(Object *pA, Object *pB))
{
float size = MIN_CELL_SIZE;
int startLevel = 0;
uint32 occupiedLevelsMask = grid->occupiedLevelsMask;
Point pos = obj->pos;
// If all objects are tested at the same time, the appropriate starting
// grid level can be computed as:
// float diameter = 2.0f * obj- > radius;
// for ( ; size * SPHERE_TO_CELL_RATIO < diameter; startLevel++)
//
size *= CELL_TO_CELL_RATIO;
//
occupiedLevelsMask >>= 1;
//
 
Search WWH ::




Custom Search