Public Group

# Checking Particle collisions with Sprites

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

## Recommended Posts

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

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites
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]

##### Share on other sites
Whats the difference in ++j and j++?
Thanks for the tidbit/nitpicking [lol]

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

toXic1337

##### Share on other sites
++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.

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

1. 1
Rutin
38
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633350
• Total Posts
3011470
• ### Who's Online (See full list)

There are no registered users currently online

×