Create a project box2dtest and remove the background and genie objects. First, define a screen pixel and physical world length conversion macro in the helloworldscene. h header file, and introduce the box2d header file.
#define PIXEL_TO_METER 30 #include "Box2D/Box2D.h"
Then declare several functions and variables.
Void createworld (); // create the physical world void createwall (); // create the surrounding wall void createball (); // create a Rigid Body
B2world * World;
B2body * wallbody;
Float32 walllineoffset;
Createworld implementation
Void helloworld: createworld () {b2vec2 gravity; gravity. set (0.0f,-9.0f); // the physical world Gravity Vector. Here we create a downward Gravity Vector b2bodydef * bodydef = new b2bodydef (); World = new b2world (gravity ); // create the physical world object world-> setallowsleeping (true) based on the gravity vector; // allow sleep world-> setwarmstarting (true); // The initial state will be affected by gravity}
Createwall implementation
Void helloworld: createwall () {ccsize S = ccdirector: shareddirector ()-> getvisiblesize (); b2bodydef walldef; // you can define walldef as a rigid body. position. set (); // position wallbody = World-> createbody (& walldef); // create a rigid body (surrounded by walls) b2edgeshape wallshape; // shape // lower walllineoffset facilitates the adjustment of the display position wallshape. set (b2vec2 (0, 0 + walllineoffset), b2vec2 (S. width/pixel_to_meter, 0 + walllineoffset); // set the shape and size of wallbody-> createfixture (& wallshape, 0); // connect the shape to the frame/wallshape. set (b2vec2 (0, S. height/PIXEL_TO_METER-wallLineOffset), b2vec2 (S. width/pixel_to_meter, S. height/PIXEL_TO_METER-wallLineOffset); wallbody-> createfixture (& wallshape, 0); // left wallshape. set (b2vec2 (0 + walllineoffset, S. height/pixel_to_meter), b2vec2 (0 + walllineoffset, 0); wallbody-> createfixture (& wallshape, 0); // right wallshape. set (b2vec2 (S. width/PIXEL_TO_METER-wallLineOffset, S. height/pixel_to_meter), b2vec2 (S. width/PIXEL_TO_METER-wallLineOffset, 0); wallbody-> createfixture (& wallshape, 0 );}
Createball implementation
Void helloworld: createball () {ccsize S = ccdirector ctor: shareddirector ()-> getvisiblesize (); For (INT I = 0; I <5; I ++) {b2bodydef balldef; balldef. type = b2_dynamicbody; // set the rigid body to a dynamic object b2circleshape circleshape; // The shape b2fixturedef ballfixturedef used by the rigid body collision; circleshape. m_p.set (1.0 + I, 5.0); circleshape. m_radius = 0.5f; // set ballfixturedef for the framework of the rigid-body connection. shape = & circleshape; ballfixturedef. density = 1.0; ballfixturedef. restitution = 0.8; // set the gravity recovery coefficient to ballfixturedef. friction = 0.3f; balldef. position. set (S. width/2/pixel_to_meter, 20/pixel_to_meter); b2body * ball = World-> createbody (& balldef); // create a rigid body ball based on the definition of a rigid body-> createfixture (& ballfixturedef ); // associate the frame with the rigid body }}
Add the following code to the init method:
wallLineOffset = 0.5; this->createWorld(); this->createWall(); this->createBall();
Finally, let's see what the situation is.
The world of objects we created is not displayed. In the next article, we will display the physical world.