Checking Particle collisions with Sprites

Started by
5 comments, last by GameDev.net 18 years, 12 months ago
Hey everybody! I posted a vector question in this thread. a couple of days ago and in that thread I came upon a way to check collisions with particles and sprites without a lot of pain. However, I feel it is fairly inefficient.

for (std::vector<Sprite*>::iterator i=SpritePool.begin(); i!=SpritePool.end(); i++)
{
   for(std::vector<Particle*>::iterator j=ParticlePool.begin(); j!=ParticlePool.end(); j++)
   {
      //update particles / sprite-particle collision
      //kill particles when necessary
   }

//update sprites / sprites-sprite collision
//kill sprites when necessary

}

See, I have a particlepool vector and a spritepool vector, and I need to check collisions between elements in the two. This is the best way I can think to do it without re-writing the whole thing to throw everything into one big vector. Give me your thoughts and maybe things you've done related to this. Thanks in advance! toXic1337
toXic1337
Advertisement
The best solution I can think of is to divide the world up into sections and then do your collisions between objects that are in the current or neighbouring sections. You could do this using some kind of tree (like a quad tree) or you could just use a plain 2d array of world sections. You would have to change your code around a bit but its better than your current code where the ammount of checking that has to be done for each particle scales linearly for each object in the entire world, limiting the size of world you can use.

Hope that helps
I understand what you're saying, and it is a good way to limit the number of calculations that are being done each cycle but..

My world is only 800x600, so dividing it up won't really help all that much. I suppose I could divide it into 200x200px sections and that would help. But will the basic algorithm I've stated even work correctly?

It seems as though but it's hard to tell without testing (in class ATM).

Any other ideas? I mean is there a common way of testing particle-sprite collisions? Should they all be in the same vector/list?

Thanks by the way,
toXic1337
toXic1337
If the world is small enough, which 800x600 is, then you should be grand with checking everything against everything like you are doing at the moment. You would only need to make the system more complicated if the world's size was arbitrary.

And I wouldn't be too worried about speed here. Just make sure hat you do bounding box checks first and then per-pixel checks if those succeed if you are using per-pixel collision detection. You'll be surprised with how much you can actually do each frame as long as you aren't too sloppy.

[nitpicking]
And its better and faster to use ++i and ++j instead of i++ and j++ in for loops.
[/nitpicking]
Whats the difference in ++j and j++?
Thanks for the tidbit/nitpicking [lol]

Thanks for your input stro! ratings++; ... er... ++ratings; [lol]

toXic1337
toXic1337
++j returns j after increment. j++ returns j before.
int    intplusplus(int j){//////int  rtn;rtn=j;j=j+1;return(rtn);}int    plusplusint(int j){//////j=j+1;return(j);}


I'm not sure how much difference that makes [especially after optimization] but the thinking is that ++j is one less copy, and less memory management fiddling.

cant remeber where i read it but i think the pre and post increment are only really different on stl containers...

This topic is closed to new replies.

Advertisement