I'm going through the code from Cinder's Box2D template, and want to modify the code so I can remove boxes to the screen as well as add them. I'm new to C++, but based on what I've learned from other SO posts I'm trying to delete boxes from the vector using this if case:
for( vector<b2Body*>::iterator boxIt = mBoxes.begin(); boxIt != mBoxes.end(); ++boxIt ) {
if( (*boxIt)->GetPosition().x > scaledX){
boxIt = mBoxes.erase(boxIt);
}
else {
Vec2f pos( (*boxIt)->GetPosition().x, (*boxIt)->GetPosition().y );
float t = toDegrees( (*boxIt)->GetAngle() );
glPushMatrix();
gl::translate( pos );
gl::rotate( t );
Rectf rect( -BOX_SIZE, -BOX_SIZE, BOX_SIZE, BOX_SIZE );
gl::drawSolidRect( rect );
glPopMatrix();
}
}
But this is causing bad access crash when "(*boxIt)->GetPosition().x" executes the second time. Any ideas?