Sign in to follow this  
Ganoosh_

Collision detection

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Ganoosh_
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?
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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this