The physical world of box2d has a world to maintain. You can create simple objects, circles, rectangles, triangles, and polygon. Polygon supports only Convex Polygon and a maximum of 8 vertices.
There are three types of box2d objects: dynamic, static, and kinematic ).
Dynamic objects can be forced, collided, and moved.
Static objects cannot be moved, they cannot be moved, and they can be involved in a collision.
A kinematics object is similar to a static object, but it can be moved.
There are three steps to create an object,
1. The body of an object defines b2BodyDef. This struct describes the object type, position coordinate, angle, and speed.
2. Use b2BodyDef to create a body.
3. With the body, you still cannot see this object, and you also need to define the shape. This shape is added to an object through a fixtures.
// body def b2BodyDef bDef; bDef.type = b2_dynamicBody; bDef.position = b2Vec2(visibleSize.width / 2 / RATIO + 5, visibleSize.height / 2 / RATIO); // body b2Body * birdBody = world->CreateBody(&bDef); // shape b2PolygonShape birdShap; CCSize size = bird->getContentSize(); birdShap.SetAsBox((65) / 2 / RATIO, (65) / 2 / RATIO, b2Vec2(45 / 2 / RATIO, 27 / 2 / RATIO), 0); // fixtures b2FixtureDef birdFixtureDef; birdFixtureDef.shape = &birdShap; // fix to body birdBody->CreateFixture(&birdFixtureDef);
Bind the genie to an object:
An genie is bound to an object and is set in the Object body definition b2BodyDef,
role = CCSprite::create(poop_1.png); bodyDef.userData = role;
However, this is not enough. You will find that the genie and the object will be separated and will not move together. This also requires changing the coordinates of the genie Based on the object location. For details, refer to the update function of the previous code.
The official company has a B2Sprite class that binds the genie and the body together. I will add this class later to make it easier to use and don't need to define a lot of things.