I have attached below the setup with DebugDraw enabled. We're talking about box2d+cocos2d-iphone.
The edges are defined like this:
Code:
// Define the ground body.
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 0);
b2Body *groundBody = world->CreateBody(&groundBodyDef);
// Define the ground box shape.
b2PolygonShape groundBox;
CGSize worldSize=CGSizeMake(screenSize.width/PTM_RATIO, screenSize.height/PTM_RATIO); //Change values as needed for your world size.
//-------------------------------------------------------------------
// bottom
//-------------------------------------------------------------------
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(worldSize.width,0));
groundBody->CreateFixture(&groundBox);
//-------------------------------------------------------------------
// top
//-------------------------------------------------------------------
groundBox.SetAsEdge(b2Vec2(0,worldSize.height), b2Vec2(worldSize.width,worldSize.height));
groundBody->CreateFixture(&groundBox);
//-------------------------------------------------------------------
// left
//-------------------------------------------------------------------
groundBox.SetAsEdge(b2Vec2(0,worldSize.height), b2Vec2(0,0));
groundBody->CreateFixture(&groundBox);
//-------------------------------------------------------------------
// right
//-------------------------------------------------------------------
groundBox.SetAsEdge(b2Vec2(worldSize.width,worldSize.height), b2Vec2(worldSize.width,0));
groundBody->CreateFixture(&groundBox);
The moving body is declared like this:
Code:
bodyDef.angle = 15 * 0.0174532925;
bodyDef.type = b2_kinematicBody;
bodyDef.position.Set(5.0f, 300/PTM_RATIO);
b2Vec2 speed(5.0f, 5.0f);
bodyDef.linearVelocity = speed;
body = world->CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(0.5f, 0.5f);
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
// Set the box density to be non-zero, so it will be dynamic.
fixtureDef.density = 0.5f;
// Override the default friction.
fixtureDef.friction = 0.3f;
fixtureDef.restitution = 0.9f;
fixtureDef.userData = (void*)1;
// Add the shape to the body.
body->CreateFixture(&fixtureDef);
The problem is that the body gets out of the edges of the screen
🙁If I make it dynamic, it is ok, but I would like it to be kinematic.
Thanks!