Hi all,
I'm new with physics engine (but not in cocos2d, I already helped riq to find and fix some memory leak issues) and I want to make a very simple example which will make me more comfortable for my new iPhone game.
I just want to create a world that some little boxes move perpetually bouncing on every side. Moreover I want the little boxes don't collide each others. Here is my code (in which I delete all iPhone specific code to simplify it) :
-(void) initTheWorld
{
CGSize screenSize = [Director sharedDirector].winSize;
// Define the gravity vector.
b2Vec2 gravity;
gravity.Set(0.0f, 0.0f);
// Do we want to let bodies sleep?
// This will speed up the physics simulation
bool doSleep = true;
// Construct a world object, which will hold and simulate the rigid bodies.
world = new b2World(gravity, doSleep);
world->SetContinuousPhysics(true);
// Define the ground body.
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 0); // bottom-left corner
// Call the body factory which allocates memory for the ground body
// from a pool and creates the ground box shape (also from a pool).
// The body is also added to the world.
b2Body* groundBody = world->CreateBody(&groundBodyDef);
// 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);
}
-(void) addNewBoxWithCoords:(CGPoint)p
{
// Define the dynamic body.
b2BodyDef bodyDef;
bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
b2Body *body = world->CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(0.5f, 1.0f);
b2MassData massData;
massData.mass = 0.01f;
body->SetMassData(&massData);
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 0.0f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 1.0f;
fixtureDef.filter.categoryBits = 0x0008;
fixtureDef.filter.maskBits = 0xFFFF ^ 0x0008;
body->CreateFixture(&fixtureDef);
body->SetLinearVelocity(b2Vec2(10.0f, 10.0f));
}
This code crashes, with the following error, when a box collide a side of the world:
Assertion failed: (kNormal > 1.19209290e-7F), function b2ContactSolver
Moreover I want to add two other boxes (on the lower left and right corner) which they are static and on which the little boxes bounce. So to do it I just add the following code at the end of the initWorld method :
b2BodyDef leftBoxDef;
leftBoxDef.position.Set(25.0f / PTM_RATIO, 25.0f / PTM_RATIO);
b2Body *leftBox = world->CreateBody(&leftBoxDef);
b2MassData leftBoxMassData;
leftBoxMassData.mass = 0.9f;
leftBox->SetMassData(&leftBoxMassData);
b2PolygonShape staticLeftBox;
staticLeftBox.SetAsBox(25.0f / PTM_RATIO, 25.0f / PTM_RATIO);
b2FixtureDef leftBoxFixtureDef;
leftBoxFixtureDef.shape = &staticLeftBox;
leftBoxFixtureDef.filter.categoryBits = 0x0004;
leftBox->CreateFixture(&leftBoxFixtureDef);
b2BodyDef rightBoxDef;
rightBoxDef.position.Set((screenSize.width/PTM_RATIO) - (25.0f / PTM_RATIO), 25.0f / PTM_RATIO);
b2Body *rightBox = world->CreateBody(&rightBoxDef);
b2MassData rightBoxMassData;
rightBoxMassData.mass = 0.9f;
rightBox->SetMassData(&rightBoxMassData);
b2PolygonShape staticRightBox;
staticRightBox.SetAsBox(25.0f / PTM_RATIO, 25.0f / PTM_RATIO);
b2FixtureDef rightBoxFixtureDef;
rightBoxFixtureDef.shape = &staticRightBox;
rightBoxFixtureDef.filter.categoryBits = 0x0004;
rightBox->CreateFixture(&rightBoxFixtureDef);
Someone can help me to understand what I'm doing wrong ?
I start from the box2d sample include in the 0.8.2 cocos2d release.
PS: This is a cross posting from box2d forums ;-) and I will synchronise the two thread.
Regards.