mardi 21 juin 2016

Best data structure for collision detection using cell lists / tiles /grids in C++

I am new to c++ and based on this question: Collision detection of huge number of circles i want to implement cell lists, also described here: https://en.wikipedia.org/wiki/Cell_lists to get better performance out of an collision detection algorithm. There are probably a lot of different algorithm around this subject but i would like to focus on this one.

Right now i am computing the collision between n spheres with the same size on a 2D area. I divided the area into cells which form a grid. The grid is a 2D array for the cell position and the 3 is a vector to store the number of the spheres which are currently in that grid cell.

std::array<std::array<std::vector<int>, cols>, rows> grid;

I can get the position of each ball in the grid by calculating:

row = x/cell_width;
col = y/cell_height;

I can then add that ball number to the corresponding cell:

grid[row][col].push_back(ball);

Then i check for every grid cell if there are balls in it. If yes, i join all the balls from the actual and surrounding cells to form one vector (gridballs) and brute check for collisions in this group.

for( int i = 1; i <rows; i = i + 1 ){
        for( int j = 1; j <cols; j = j + 1 ){
            if (grid[i][j].size()!=0){ 
                gridballs=grid[i][j];
                int nextx[]={1,1,0,-1,-1,-1,0,1}; 
                int nexty[]={0,1,1,1,0,-1,-1,-1};
                for( int k = 1; k <8; k = k + 1 ){
                    if (grid[i+nextx[k]][j+nexty[k]].size()!=0){   
                        gridballs.insert(gridballs.end(), grid[i+nextx[k]][j+nexty[k]].begin(), grid[i+nextx[k]][j+nexty[k]].end());
                    }
                }
         collisioncheck (gridballs);
        }
    }
}

}

Following questions came up while doing this:

What would be a better structure than using vectors in a 2D array? Everything in the grid is always subject to change and i suppose constantly adding and resetting vectors takes up a lot of time?

Dont i loose a lot of performance by checking a lot of cells several times by always including the surrounding cells each time? Is there a way around this or did i miss something?

What would be an efficient way to make sure that i dont test a pair of spheres twice?

Thank you very much for the help!

Aucun commentaire:

Enregistrer un commentaire