Editing the Step method within b2World.cpp

General discussion about Box2D tutorials
Post Reply
therealcos
Posts: 16
Joined: Tue Aug 20, 2013 7:30 am

Editing the Step method within b2World.cpp

Post by therealcos »

I'm trying to destroy a body in my world upon collision with another body, and I'm doing so (following your tutorials) by putting the body to be destroyed into a vector so that I can call DestroyBody on it after the current timestep. I'm just having trouble figuring out where exactly in the code to call it.

I tried editing the Step method in b2World.cpp directly, calling DestroyBody at the end of this method, but I'm getting a series of errors by doing it this way. I try to import my WorldLayer.h file to b2World, but NSObjCRuntime.h complains with about 19 errors just when I import this class. Even when I set the b2World file type to Objective-C++ source, even more cascading errors begin to appear elsewhere.

Is editing b2World directly even the best way to do this? If so, how do I approach these cascading errors? If not, where is the best place in my code to call DestroyBody on all the bodies in the vector?

By the way, thanks so much for all of your help so far - you've been a total life saver!
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Editing the Step method within b2World.cpp

Post by iforce2d »

Editing the Box2D files is not necessary. As you've already said, the best place to call it is "after the timestep". Like this :D

Code: Select all

world->Step( ... );
// here !
I'm pretty sure there is a tutorial about removing bodies safely, not sure if that is the one you are talking about, but it should not be too complicated.
Most of the time it seems the real issue people have with this is where to put the vector of bodies to be destroyed so that it can be accessed from both the main code (where Step is called from) and also inside the contact listener as well. This could be done by making the vector a member variable of the contact listener class.
By the way, a set is probably a better choice than a vector because it will ensure that the list of bodies to be destroyed does not contain duplicates.
Post Reply