Removing Vector Items In Loop?

Sage
Posts: 1,066
Joined: 2004.07
Post: #1
Is it possible? I use an std::vector to store some particles which I update using a simple for loop. In the same loop, I'd like to check if they have left the screen and, if they have, remove them. Unfortunately this isn't an easy task (as I see it). This is my code which I know doesn't work. Is there a way to accomplish the same thing without making my program crash and burn?

Code:
for( vector<SimParticle*>::iterator itr = particles.begin(); itr != particles.end(); itr++ ) {
        if( ( *itr ) ) {
            ( *itr )->integrateForceAndTorque( timeStep );
            
            float x = ( *itr )->getPosition().x();
            float y = ( *itr )->getPosition().y();
            
            if( x < 0 || x > screenWidth || y < 0 || y > screenHeight ) {
                delete ( *itr );
                particles.erase( itr );
            }
        }
    }
Quote this message in a reply
Member
Posts: 161
Joined: 2005.07
Post: #2
Whenever I want a running table of the on-screen particles (where particles can be added and removed at will), I add a bool to the particle struct that just specifies whether the particle exists or not. So the algorithm would be something like this:

1) initialize the array of particles, setting all the "exists" bools to false
2) to remove a particle, just set the exists property to false
3) when adding particles, just overwrite the first particle that has exists = false.
4) when doing anything else with the particles, like updating or drawing it, just make sure it exists first.

No actual inserting or deleting is actually done.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #3
I attempted to implement this, but I get problems when I try adding lots of particles to the manager. Because it's just a linear search, when I add, say, 3000 particles, it takes a while for my computer to add them all because each one added requires another for loop to check for dead particles which is very inefficient. Is there a way to do this without sacrificing speed?
Quote this message in a reply
Member
Posts: 320
Joined: 2003.06
Post: #4

Chopper, iSight Screensavers, DuckDuckDuck: http://majicjungle.com
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #5
Don't have a dynamic list of particles, it's going to be slow.

Simply declare a a static array of the maximum amount of particles you're going to have, and keep a counter for the number of particles that are alive. When one dies out, move the one at the end of the list to the spot of the one that just died and decrement the counter.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #6
reubert Wrote:http://www.idevgames.com/forum/archive/i...-9108.html
Thanks. I've switched over to using lists and using the iterator returned from the erase method and it works perfectly.

I'm going to try out the static array to see if there's any noticeable difference in performance. I currently can get up to about 20-30 thousand particles with an std::list before seeing the framerate drop.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Sorting items in an NSArray Taxxodium 5 5,255 Feb 25, 2005 03:14 PM
Last Post: Taxxodium
  Removing delay until key repeat hyperutila 4 4,058 Jan 16, 2004 01:36 PM
Last Post: FCCovett