Original Post
I'm trying to implement a blockmap to do collision detection for particles using stl vectors. I've set up an array of std::vectors and then add the pointers to the particles that appear in that array location to the vector there. After I build the map, then I'll go through and compute any collisions. But after it goes through once, then it crashes. I've narrowed it down to the way I'm clearing out the vectors after each pass. But I don't know how to handle the vectors correctly. I want to be able to clear out the vectors in each array location, but I'm doing it wrong. Edit: I should add that the crash happens in the fill blockmap section after I move to a new location. If I keep adding particles to the original location, it works just fine. It's only when I add particles to a new location that every goes to hell Here's the code I've written: Any ideas what I'm doing wrong?
//make the blockmap
std::vector<Particle*> blockmap[BLOCK_MAP_SIDE_SIZE * BLOCK_MAP_SIDE_SIZE];
//fill blockmap
if(sparks.size() > 1)
{
for(std::vector<Particle>::iterator iter = sparks.begin(); iter != sparks.end(); iter++)
{
blockmap[iter->Get_BlockPos()].push_back(iter->Get_Pointer());
}
}
//compute collisions
for(int i = 0; i < BLOCK_MAP_SIDE_SIZE * BLOCK_MAP_SIDE_SIZE; i++)
{
if (blockmap.size() > 1)
{
for(std::vector<Particle*>::iterator iter = blockmap.begin(); iter != blockmap.end(); iter++)
{
//do collisiony stuff here
}
}
blockmap.clear();
}