Collision Detection with Many Objects

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Collision Detection with Many Objects

Post by XianForce »

I have many objects that will be in motion, but I only have up to 5 that would need to have collision checks for. How do I check for collisions for these objects, while not having to check for collisions between it and every other object?
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Collision Detection with Many Objects

Post by MarauderIIC »

Put them in a special list?
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Bludklok
Chaos Rift Junior
Chaos Rift Junior
Posts: 241
Joined: Tue Apr 14, 2009 1:31 am
Current Project: EnigmaCore
Favorite Gaming Platforms: PC, N64, Playstation1, Playstation2
Programming Language of Choice: C++
Location: New Jersey
Contact:

Re: Collision Detection with Many Objects

Post by Bludklok »

I've thought about this as well but I never came to a conclusion.

My idea is that you could split the screen into decent size sections (If you had a 100 x 100 screen you split it into 50 x 50 sections so you have 4 sections). Number the sections and label the object according to what section they are in. Then just do a check for which ever section you want.

Thats the best I could come up with.
Youtube
Website
Current project: Enigma Core
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Collision Detection with Many Objects

Post by Bakkon »

Have them hold a hasCollision flag and check for that before checking collision.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Collision Detection with Many Objects

Post by avansc »

mar had the best solution. just make a specialy list that only contain object that will collide.
if you have 100 object that wont collide and 5 that will. and you check for a collision flag on all of them you are wasting alot of cpu cycles.
and the splitscreen is not your idea. its called quad tree decomposition.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: Collision Detection with Many Objects

Post by RyanPridgeon »

Do you mean they dont collide because they're not supposed to, or because they're not near each other?

To optimise so that only nearby objects are tested, you can use a quad/oct tree
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Collision Detection with Many Objects

Post by hurstshifter »

A quad-tree would probably be a good idea if you had many objects that could collide, but with a small number like 5 or so Marauder's idea of a special list of collision-specific objects is probably your best bet.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Collision Detection with Many Objects

Post by XianForce »

MarauderIIC wrote:Put them in a special list?
Yeah, but that's not what I was talking about exactly. It's like I have a bunch of Enemy NPCs that don't collide with each other, but the player can can collide with them. So I don't want to check for every enemy there is. So I'm looking for something like the quad tree composition thing avansc was talking about.

Thanks guys =D
User avatar
AerisAndMe
ES Software Engineer
ES Software Engineer
Posts: 381
Joined: Tue Apr 07, 2009 9:29 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: PC, SNES, PS3
Programming Language of Choice: C/++
Location: Madison AL
Contact:

Re: Collision Detection with Many Objects

Post by AerisAndMe »

I'm working on implementing a quad tree for ES coarse-collision resolutions right now too. In our case, a quad tree is the way to go because of its flexibility and its dynamics.

If there aren't going to be vast numbers of collisions happening at a time, a grid is FAR easier to implement and functions similarly.

The biggest difference between something like a quad tree (oct for 3D of course), or binary partitioning even, and a grid is that a grids cells are static and a tree's cells can be programmed to adjust based on the density of collidables in that area.

For example, if you set your grid's cells to be 256x256, and your average object is 32x32, then there can be several collisions happening in every grid, each of which must now be resolved using the brute force (check every object with every other object) method. If your grid's cells are 64x64 and there are very few objects in the grid, you may lose efficiency because the number of cells ends up being huge.

After all, it depends on your purpose. Trees are, of course, heavier and harder to implement.
Post Reply