Box2D and C++ compile issues

The entertaining “exploding bomb” example from Nick Vellios provides an interesting variation on how to use the physics engine to apply radial forces to objects in your world away from a specific point (as in an explosion).

As a part-time coder, I am easily thrown by compiler errors in what is ostensibly well-worn Box2D code, and the project at on my machine initially had two compile errors. (I say “on my machine, but it does have all the latest versions of everything.)

1.     CCLayer.m in the cocos2d subdirectory of cocos2d Sources had a method named “initWithColor” which conflicted with another method of that name elsewhere (with different arguments).  Fortunately i could easily find a working fix that was only required exactly at the point where the error was flagged. I put in the line below, commenting out their line, which was enough to direct the compiler to the correct initWithColor.

return [[(CCColorLayer*)[self alloc] initWithColor:color] autorelease]; //  fix

// return [[[self alloc] initWithColor:color] autorelease];  <– errant code

2.  Somewhat more trouble was an error I have found in many Box2D examples that, at first, prevented me from using the project.  The short form of the error message was “error: Semantic Issue: Variable length array of non-POD element type…” and specifically referring to an array declaration such as:

b2Vec2 vertices[vertexCount];

for( int i=0;i<vertexCount;i++) {

vertices[i] = old_vertices[i];

vertices[i] *= mRatio;


which, being C++, looks like a (potentially) variable-length array.  These are, strictly speaking, not allowed by C++. But if, at run time, the variable for array size has been assigned an appropriate value, some versions of C++ compilers (that want to support both C and C++) allow this sort of thing. Odd that Box2D would _rely_ on that.  The solution that was easiest was to switch the Xcode project to use the optional LLVM GCC compiler (rather than the default Apple compiler. (Do this by going to BUILD OPTIONS of the Build Settings, and specifying in the drop down for “Compiler for C/C++/Objective-C” the value “LLVM GCC 4.2”  rather than default APPLE compiler.

A good discussion of C++ arrays and this issue was found here.   Presently my main build system is doing many strange things, and at times i get “clang error with exit code 1” followed by a crash or hung compiler; hoping not to have to be constantly switching compiler options and choices to be able to build Cocos2d and Box2d projects once i get this all straight.

All in all I think this small “bomb explosion” project is quite good — focused on just demonstrating a single concept and not cluttered with other distractions.  I am on the hunt for a Box2D demo that will help me re-create my triangular Box2D “ramp” objects from my Corona WordPiles prototype into my emerging native iOS WordPiles prototype.


Notes: a) this project uses cocos2d v0.99.3  and Open GL ES-CM 1.1.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s