World Limits Collision Detection

What ways are there I could use to handle collision detection at the limits of a collision area?

Basically, my 2D platformer uses a grid for the broad-phase of my collision detection of moving objects against the static environment objects, each grid square stores a list of all the objects that are at least partially within them.

All moving objects work out which grids squares they cover (currently no more than 4 since none move very fast) in a single update and then checks against all objects in each square, skipping any duplicates.

Since the collision grid is finite, if an object goes outside the area it covers the program crashes (since the grid number it is looking for is invalid), so I’m wondering about what methods I can use to handle this.

I’m currently thinking of three possible ideas.

  1. Just ignore checks against invalid grid squares and either don’t worry about objects that go outside the area or remove them.

  2. Add collision objects around the grid area to ensure objects never leave. (I think this one is kindly sloppy, and I’d need to include the first part of #1 for stability anyway)

  3. Add special case code to perform collision against the edges of the grid area to ensure they never leave it and never try to collide with invalid grid squares. (potentially wasteful since the code won’t execute very often)

What do people think of my ideas here?

Are there other methods I haven’t thought of?

Thanks.

Before checking against the other objects see if the square actually exists in whatever container holds your grids.
For example
if(Grid[300,300] != null{
Docollision();
}else{
// Do your whatever you want to do when the object is outside Could just do return;
}

One thing I always did in 3D console action games was to put a death box underneath the world. If any character managed to fall out of the world, it died as soon as it touched the death box. This catered for objects falling through the collision mesh (for various reasons) or falling off the edge of the world (for other reasons).

In your case, when you calculate the grid cell the object would occupy, your next step is to check that it is within the bounds of the collision grid. If it is outside one or both dimensions of the collision grid, it is outside the world and is therefore no longer in a valid state. Remove the object from the game or snap it back to within the valid bounds of the game.

Thanks for the replies, glad to now I’m going in a similar direction to the professionals.

1 Like