Create Composite Object with Box2D
With Box2D, it’s very easy to create the composite object, which is composed of different standard small objects (e.g, polygon, circle, etc). This feature is useful when the shape of your sprite is complex. In this article, I will present how to achieve it.
Method:
1. Create a body;
2. Using the body to create the shapes of all components.
3. Setup body’s mass.
Example:
int matrix[2][2]= { {1,0}, {1,1} }; b2BodyDef boxBodyDef; boxBodyDef.position.Set(150.0f/PTM_RATIO, 400.0f/PTM_RATIO); boxBody = world->CreateBody(&boxBodyDef); for(int i = 0; i<2; i++) { for(int j=0; j<2; j++) { if(matrix[i][j] == 1){ b2PolygonDef* boxShapeDef = new b2PolygonDef; boxShapeDef->SetAsBox(50.0f/PTM_RATIO/2, 50.0f/PTM_RATIO/2, b2Vec2(i*50.0f/PTM_RATIO,j*50.0f/PTM_RATIO), 0); boxShapeDef->density = 1.0f; boxShapeDef->friction = 0.3f; boxShapeDef->restitution = 0.1f; boxBody->CreateFixture(boxShapeDef); } } } boxBody->SetMassFromShapes();
In the codes above, we created a composite object with the help of an array matrix, its values determines where a box should be added. Another point should be noted is the method SetAsBox. You need to specify the location of the box in local coordinate with the third parameter.

—————————
Download: You can download the source codes from here.
Categories: iPhone Development

Hi,
I was wondering if you have tried to install Cocos2D 8.1. I did, and I am trying to include Box2D library to the default project template, but have had a tough time doing so. Any tips? Thanks in advance,
Jo
This was from another post I found. Can not take the credit for it but it works.
1) Load cocos2d-iphone.xcodeproj so cocos2d is in XCode. (This may not be necessary.)
2) Run the install_template.sh so there is a cocos2d-0.8.1 application template.
3) Use that template to create a new project.
4) In the new project, under Other Sources, bring up the context menu Add > Existing files… and add the Box2D directory (Location: cocos2d-iphone-0.8.1/external/Box2d/Box2D).
5) In the new project, under Other Sources, bring up the context menu Add > Existing files… and add GLES-Render.h and GLES-Render.mm (Location: cocos2d-iphone-0.8.1/tests/Box2DTestBed-Cocos2dIntegration).
6) Select your project from the Groups & Files list on the left hand side. Click the blue Info button on the top center of the window. Select build from the top tabs (right under Project “yourprojectname” Info). Scroll down to Search Paths Edit Header Search Paths. To quote cjl, “[...] add a recursive header search path. I used a single dot, which resolves to something like ‘./’”
7) Change your projectnameAppDelegate.m to projectnameAppDelegate.mm .
You should now be able to add Box2D elements to your game. Again, be aware that the methods of the Box2D in cocos2d 0.8.1 can be different than what was in cocos2d 0.8.0 or on sites around the web. Check out the Box2D examples in cocos2d 0.8.1 for the correct signatures.
Thanks for all the previous comments which helped me get a working project.
Steve