# Collision detection

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

## Recommended Posts

Just looking for some advice on 2D collision detection. I have no scrolling no tiles, just enemies that I wanna have collide against each other and bounce off each other when they hit. Basically similar to a topdown pool table is the best way to describe it. What's the best type of collision detection I should use for this? Also, should I setup tiles and test enemies in each tile, or just leave it? I know it speeds it up, but it's currently running insanely fast, so I'm not gonna worry about speed just yet. Just wanna know if it'd be better/easier. Also, when I check an enemy to waht enemies are touching it, then the next and that other enemy was already tested on it, how do I know not to test that enemy again, should I just have a list for each enemy, or is there a better way? Thanks in advance

##### Share on other sites
It's difficult to give you concrete examples if we don't have the slightest clue about how you handle enemies, movement, ect.

I assume you have some identification for each enememy element, are they individual objects of an enemy class?
If you so should store the position coordinates of the enemies in their objects, and have a flag variable that activates and disables coallision test against that enemy.

All you would need to do is looping through the enemy objects, check if the collision test flag is true and then check that enemy against all other enemy objects whose collision flag is still true. Loop it through all enemies.
For the collision you just need to see if they share the same positions on your map or are "too close" which should be your own definition depending on graphical models.
The movement of the enemies should reset the collision flag automatically.

The problem's complexity is O(2^n) though, n being number of your enemies, consider this if you are going to work with many enemies at same time.

And another problem except for the performance is what happens if three enemies collide at the same time with each other, two would bounce off while the third would remain unaffected because both enemies he has collided with would have set their collision flag to false.

[Edited by - kiome on October 5, 2006 7:33:18 PM]

##### Share on other sites
Yeah, that's the problem, so many enemies could all be colliding, I don't know how I'd be able to detect that

##### Share on other sites
Quote:
 Original post by Ganoosh_Yeah, that's the problem, so many enemies could all be colliding, I don't know how I'd be able to detect that

What kind of numbers are you expecting to be on screen at same time?
You have to test your code on some worst case scenario and see if you are comportable with the performance, don't bother optimizing as long as you are not sure if it needs optimization.

##### Share on other sites
Quote:
Original post by kiome
Quote:
 Original post by Ganoosh_Yeah, that's the problem, so many enemies could all be colliding, I don't know how I'd be able to detect that

What kind of numbers are you expecting to be on screen at same time?
You have to test your code on some worst case scenario and see if you are comportable with the performance, don't bother optimizing as long as you are not sure if it needs optimization.

Mainly between 5 and probably 100 if I even have that many. With what I currently have, I ran a test with 300 and it's still staying aroudn 150fps. So it'll most likely stay over or around 1000 or in the later hundreds, so it should be fine.

##### Share on other sites
Pool Hall Lessons

Very useful article on circle/circle and sphere/sphere collision reactions.

##### Share on other sites
Bump

Thanks JBourrie, that link was awesome.
Only problem I'm having right now is if the enemies are moving too fast, they don't "collide", and either go through each other, or some crazy stuff happens. How can I make them still contact even when they're going too fast?

##### Share on other sites
Quote:
 Original post by Ganoosh_BumpThanks JBourrie, that link was awesome.Only problem I'm having right now is if the enemies are moving too fast, they don't "collide", and either go through each other, or some crazy stuff happens. How can I make them still contact even when they're going too fast?
You'll need to either subdivide the timestep such that the circle displacements are less than their radii, or perform a continuous (swept) test, the latter of which may be covered in the article linked above as well (I'm not sure).

1. 1
2. 2
3. 3
Rutin
19
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633657
• Total Posts
3013197
×