This topic is 1140 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi Guys,

I read up on spatial partition, created a grid structure with std::vector<Cell> cells where each cell size is 100 x 100 (dealing with 2D here) and at each update, i check when an entity has switched cells. I think so far so good.

The problem I believe lies below:

void updateGrid(Grid *grid, std::vector<MovingEntity*> entities,std::vector<Manifold> &manifolds){

for(int y=0;y<grid->num_y_cells;y++){
for(int x=0;x<grid->num_x_cells;x++){

for(int _y=y-1;_y<=y+1;_y++){
for(int _x=x-1;_x<=x+1;_x++){
if(_x<0 || _y<0 || _x>=grid->num_x_cells || _y>=grid->num_y_cells)
continue;

for(int out=0;out<grid->cells[_y].entities.size();out++){
for(int in=0;in<grid->cells[_x].entities.size();in++){

MovingEntity *e1=grid->cells[_y].entities[out];
MovingEntity *e2=grid->cells[_x].entities[in];

if(e1==e2)continue;

e1->aabb.init(e1->pos,e1->half_extents);
e2->aabb.init(e2->pos,e2->half_extents);
Contact contact;
if(myAABBvsAABB(e1->aabb,e2->aabb,contact)){

manifolds.push_back(Manifold(e1,e2,contact));
}
}
}
}
}
}
}
}


I iteratate over a 1D vector (using the 2d trick) check all neighbouring cells for collision, and attempt to create a contact if one exists.

My algo above unfortunatelly is not doing anything. No collisions are detected, even when I fill the screen with AABBs.

Could someone tell me please where the error is?

Thanks,

Mike

##### Share on other sites
I don't understand your trick so the moving entities make no sense to me.
&nbsp;
A simple way to debug this is to have one entity, hack some code to detect it should cross the border (based on eg coordinate of it, and very preferably NOT on border detection code you already have), and have a break point in the debugger, or dump the entire grid, or whatever you like to use for debugging.
&nbsp;
Then you can see the situation at that point in time, and figure out what is not what it should be.
&nbsp;
Alternatively, you can run additional check code that all entities are really in the grid they should be, as part of the game loop.
When that check fails, do the above, or fail the program, or whatever.
&nbsp;
Edit: http://www.gamedev.net/page/resources/_/technical/general-programming/debugging-follow-the-data-r3913 may be useful for understanding how to debug Edited by Alberth

##### Share on other sites
On both entities, they either use obly x OR y coord, so you gather always the same edges of the grid, but never its interior.

Seems you forgot to combine indices like cellIndex = x + y * grid->num_x_cells ?

##### Share on other sites

Thanks for the help guys. I managed to solve the problem.

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633665
• Total Posts
3013239
×