Jump to content
  • Advertisement
Sign in to follow this  
manunderground

Collision detection along a grid

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I've implemented a collision detection system which divides the screen into a grid and then places each body into a cell in the grid. This works well enough for me so far except for one problem: I can't have bodies be the size of a cell without causing collisions with adjacent bodies. I recognize that by design I will be avoiding bodies which are larger than the size of a cell, but I'd like to include bodies that are equal to the size of a cell so that I can create continuous bodies which appear to extend beyond a cell. The problem occurs when I try to find which neighboring cells to check for collisions. I look at each corner for the body; when a body's width is equal to that of a cell its top right corner falls out of the cell. Here's the problem code:
                // x, y is the corner of some body
		/* Calculate which column and row the point falls under. */
		int column = (int) Math.floor(x / cellWidth);
		int row = (int) Math.floor(y / cellHeight);
		Cell target = cells[column][row];
		return target;
Any insight much appreciated!

Share this post


Link to post
Share on other sites
Advertisement
Found a solution. When I calculate neighbors I subtract one from the current cell's dimensions. I suppose this could be thought of as adjusting for the zero index.

Share this post


Link to post
Share on other sites
Why not create a simple axis aligned bounding box for each entity you want to check for collisions with.

Obviously you could use a quad tree approach to narrow down which collisions need testing.

Considering the current method your using, I think its safe to assume that box->box collision will be enough and as your on a 2d plane I imagine it is quite easy to test for collisions.

You could have an Axis Aligned Bounding Box class with members maxX, maxY, minX, minY and just have a member function that checks for collision, this will all work preety well if you have a reasonable way to obtain the max and min bounds of an entity

I think something like this would work fine:


bool hasCollided(const BoundingBox& box)
{
if(box.maxX > this->maxX > box.minX ||
box.maxX > this->minX > box.minX)
{
//X Collision
if(box.maxY > this->maxY > box.minY ||
box.maxY > this->minY > box.minY)
{
//X and Y Collison
return true;
}
}
return false;
}






You could just simply add your bounds to any object as a member, i imagine this will also make your code more readable as you could end up with something like this in practice


class car;
class tree;

if( car.getBounds().hasCollided(tree.getBounds()) )
{
//Collision occured so do something
}




Good luck.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!