Jump to content

  • Log In with Google      Sign In   
  • Create Account


Question about collision detection implementation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 chondee   Members   -  Reputation: 135

Like
0Likes
Like

Posted 13 January 2012 - 04:42 PM

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!

Sponsor:

#2 homer_3   Members   -  Reputation: 173

Like
0Likes
Like

Posted 13 January 2012 - 04:54 PM

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.

#3 chondee   Members   -  Reputation: 135

Like
0Likes
Like

Posted 13 January 2012 - 05:10 PM

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.

#4 homer_3   Members   -  Reputation: 173

Like
1Likes
Like

Posted 13 January 2012 - 05:57 PM

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.

#5 hupsilardee   Members   -  Reputation: 486

Like
1Likes
Like

Posted 14 January 2012 - 06:30 AM

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.

#6 PunCrathod   Members   -  Reputation: 268

Like
1Likes
Like

Posted 16 January 2012 - 09:35 PM

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.

#7 chondee   Members   -  Reputation: 135

Like
0Likes
Like

Posted 16 January 2012 - 10:08 PM

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.

#8 PunCrathod   Members   -  Reputation: 268

Like
0Likes
Like

Posted 17 January 2012 - 12:51 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS