Here are a few notes I found myself wondering about when digging into Box2D + cocos2D.
First things first, your classes that use Box2D will have to have the extension .mm, this tells xcode that you are mixing objective-c with cpp.
The most important thing to note is that Box2D uses meters, and Cocos2D/Iphone of course uses pixels. Well Riq, the creator of Box2D has kindly figured out for us that there are 32 pixels in a Box2D meter.
So first things first, always make sure all units are relative to this ratio, so you might as well set it at the top, as riq (cocos2d creator) does.
// Constants #define PTM_RATIO 32
Create a world
b2Vec2 gravity; gravity.Set(0.0f, -10.0f); _world = new b2World(gravity, true); _world->SetContinuousPhysics(true);
// Definition b2BodyDef groundBodyDef; groundBodyDef.position.Set(0,0); // bottom left corner // Body b2Body* groundBody = _world->CreateBody(&groundBodyDef);</code>
// Define the ground box shape
b2PolygonShape groundBox; //Bottom groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(screenSize.width/PTM_RATIO,0)); groundBody->CreateFixture(&groundBox); //Top groundBox.SetAsEdge(b2Vec2(0, screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO, screenSize.height/PTM_RATIO)); groundBody->CreateFixture(&groundBox); //Left groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(0,0)); groundBody->CreateFixture(&groundBox); //Right groundBox.SetAsEdge(b2Vec2(screenSize.width/PTM_RATIO, screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO, 0)); groundBody->CreateFixture(&groundBox);
One HUGE thing that really took me sometime is that the Box2D version in the 8.1 release is so bleeding edge new, that much of the reference material you’ll find online for Box2D for creating objects no longer works, or works differently.
So here is for example how you would create a circle and add it to the world.
// Note newCircle refers to a circle sprite I made float cx = CCRANDOM_0_1() * [[Director sharedDirector] winSize].width; float cy = CCRANDOM_0_1() * [[Director sharedDirector] winSize].height;
newCircle.position = ccp(cx, cy); [_spriteManager addChild:newCircle z:0]; float circleDensity = 2.5f; float circleRadius = 0.41f; // Remember anything related to size/position must have PTM_RATIO taken into account // Create a circle in box2d b2CircleShape shape; b2BodyDef bodyDef; b2Body* ballBody; shape.m_radius = circleRadius; bodyDef.position.Set(cx/PTM_RATIO, cy/PTM_RATIO); ballBody = _world->CreateBody(&bodyDef); ballBody->CreateFixture(&shape, circleDensity); ballBody->SetMassFromShapes(); ballBody->SetUserData(newCircle);
Also since cocos2d cordinates are different that Box2D you’ll need to use this to position your object.
CGPoint cocosPosition = CGPointMake(b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
Finally this little simple one, is really sad that It stumped me, until actually this morning. However I don’t really know cpp, and some of the syntactic sugar escapes me. For example, I don’t really know what a struct…actually is. I’ve seen it around, and I use it here and there so far.
It turns out a struct is kind of like a different way of creating a class, where the properties are public.
So when it came time to create a new b2vec2, which is kind of like a CGPoint, which is kind of like a Point. I didn’t know how to create one! You’ll be using these a lot, so here’s how, to create one.
b2Vec2 vector; gravity.Set(10.0f, 100.0f);
Tags: note gotcha, Uncategorized
Coconuttttttttttttttttttttttttttttttttttttttttttttttt