• Advertisement
Sign in to follow this  

Question about collision detection implementation

This topic is 2198 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

Hi,

I am writing a 2d scrolling shooter game in c++/sdl, I have most of the basics already done, including collision detection, however I am wondering whether the way I am checking for collision is good for the long run, or I will have to rewrite the whole thing later.

What I am talking about is the way I currently have it is that during int (main) I call a global function that checks, loops through all the objects that needed to be checked for collision (player-enemy, enemy-enemy, player_bullet-enemy, enemy_bullet-player).

I am wondering if this centralized/separated way is the way to go, or I should do this more OO-y, that every object should have a collision check method for themselves.

Could someone with more experience with games involving collision detection give me some advice?

Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
The way I do it is that each game object has a list of collision boxes. Collision boxes can be passed any collision box to see if the 2 intersect. This way I can check to see if any 2 given game objects are colliding. I can also do collision pruning and only check collisions between objects of interest this way. The way you describe what you're doing, it sounds like you check collision between everything and everything else, and you definitely don't want to do that.

Share this post


Link to post
Share on other sites
That is pretty much the way I am doing it, using collision boxes, and before doing checks whether the collision box sides intersect I first check if they are close at all (that is pruning, right?). I only check between objects of interest too, I don't think I am doing any unnecessary checks.

What my concern is about whether I should keep my collision check as a central global function, or I should equip all objects with the their own collision checks, so everything checks for itself.

Share this post


Link to post
Share on other sites
Well that's good then. I also do my pruned collision checks in my main loop. In my base update, I update the players, then check for collisions, and react if necessary. I think that's an ok design.

I don't think each object should have its own specialized collision check. Just having a collision box check for collision with another collision box works well. This keeps the collision checking decoupled from any particular game object, which is useful since it allows for it to be applied more generally.

Share this post


Link to post
Share on other sites
Imagine you have 1000+ objects in a level, a not entirely inconceivable possibility. You can't check every object against every other object, because that would take too long. Instead, you should split the scene into axis-aligned cells, and quickly sort objects into the cells based on position. Then at the collision-detection phase, only check the objects in the same cell, and possibly the neighbouring cells, in case the object overlaps a boundary. You can also recycle the cell structure by using it to speed up rendering - quickly determine the visible cells, and then draw only the objects inside or intersecting those cells.

Share this post


Link to post
Share on other sites
Collision groups is the way to go. This way you can quite easily check collision only for those cases that really matter and skip the rest. Also I would imagine that you have nested for loops for all the groups. In cases where you have a list of objects that can collide each other(enemy-enemy) the most important optimization is to not check the same collision twice. easiest way to make this not happen is to make the inner for loop start from i+1 where i is the outter for loops iterator.
Also in a 2d sidescroller if you don't have active stuff outside of the screen you can skip all non active objects in the collision detection and you should have nothing to worry about. If you have 10-200 objects on the screen at the same time you don't really need any fancy optimizations like cell divisions and such.

Share this post


Link to post
Share on other sites
Thanks for the input.
Objects that get out of the screen get flagged as dead and will be removed before the collision check, but as you thought, I do have some nested loops, that I will look over to avoid checking for the same collision twice, from the point of view of both objects.

Share this post


Link to post
Share on other sites
yeah. That is the most you can really optimize in a 2d scroller collision detection without too fancy tricks. couple of years back I was working in a project that had 2000+ objects running at once that needed collision detection with each other. Even with multithreading it took some fancy tricks to get running in realtime. Eventually we managed to get 8000 simultaneous objects to run smoothly on a quad core system even with the path finding and collision avoiding AI running individually for them all by running a slower thread going trough each pair every couple of seconds and making each one a table pointing to all objects nearby and the faster threads just iterating trough these tables. Took months to optimize but it was worth the experience.

Share this post


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

  • Advertisement