How do I detect multiple sprite collisions when there are >10 sprites?

Posted by yao jiang on Game Development See other posts from Game Development or by yao jiang
Published on 2012-12-11T20:55:19Z Indexed on 2012/12/11 23:15 UTC
Read the original article Hit count: 406

I making a small program to animate the astar algorithm. If you look at the image, there are lots of yellow cars moving around. Those can collide at any moment, could be just one or all of them could just stupidly crash into each other.

How do I detect all of those collisions? How do I find out which specific car has crash into which other car?

I understand that pygame has collision function, but it only detects one collision at a time and I'd have to specify which sprites.

Right now I am just trying to iterate through each sprite to see if there is collision:

for car1 in carlist:
    for car2 in carlist:
        collide(car1, car2);

This can't be the proper way to do it, if the car list goes to a huge number, a double loop will be too slow.

enter image description here

© Game Development or respective owner

Related posts about collision-detection

Related posts about python