Game Development Reference
In-Depth Information
Creating edges
If a shape can only be hit on one side, an edge is probably what you need. Here is how we
create edges in our game:
b2BodyDef tableBodyDef;
tableBodyDef.position.Set(0, 0);
b2Body* tableBody = _world->CreateBody(&tableBodyDef);
// Define the table edges
b2EdgeShape tableBox;
// bottom edge
tableBox.Set(b2Vec2(_screenSize.width * 0.14f/PTM_RATIO,
_screenSize.height * 0.09f/PTM_RATIO),
b2Vec2(_screenSize.width * 0.86f/PTM_RATIO,
_screenSize.height * 0.09f/PTM_RATIO));
tableBody->CreateFixture(&tableBox,0);
// top edge
tableBox.Set(b2Vec2(_screenSize.width * 0.14f/PTM_RATIO,
_screenSize.height * 0.91f/PTM_RATIO),
b2Vec2(_screenSize.width * 0.86f/PTM_RATIO,
_screenSize.height * 0.91f/PTM_RATIO));
tableBody->CreateFixture(&tableBox,0);
So the same
b2Body
object can have as many edges as you need. You set an edge with its
start and end points (in this case, the
b2Vec2
structures) and add it as a fixture to the body,
with a density of
0
.