Flying every day, using box2d for physical features. There are many tutorials on box2d online. We recommend the official manual, Chinese version, and address. I will not repeat the introduction and write something I understand.
The cocos2d-x can be perfectly integrated with box2d C ++, which is very convenient to integrate.
#include Box2D.h
You can include it. Here is a problem, the cocos2d-x2.2.1 version NDK compiled Android version, you need to change, or you can not find the box2d header file.
#include Box2D/Box2D.h
Enable debug rendering for box2d
Generally in order to facilitate debugging box2d, will be the physical world of Objects drawn out, the official debug draw, cocos2dx inside a debug tool class GLESDebugDraw, in the test project, file name GLES-Render.h, add in.
The procedure is as follows:
1. A box2d physical world b2World, a debug Renderer, and GLESDebugDraw are required. The demo is as follows:
//// TestBox. h // TestBox2d /// Created by lerry on 14-3-4. // Copyright (c) 2014 GoonearTechnology Co ., ltd. all rights reserved. // # ifndef _ TestBox2d _ TestBox __# define _ TestBox2d _ TestBox __# include cocos2d. h # include Box2D. h # include GLES-Render.h # define RATIO 96 class TestBox: public cocos2d: CCLayer {cocos2d: CCSprite * role; b2World * mWorld; GLESDebugDraw * mDebugDraw; cocos2d: CCSize visualSize; p Rivate: void draw (); void drawBox (); public :~ TestBox (); virtual void update (float dt); virtual bool init (); virtual void onEnter (); virtual void onExit (); virtual bool ccTouchBegan (cocos2d :: CCTouch * touch, cocos2d: CCEvent * event); virtual void ccTouchMoved (cocos2d: CCTouch * touch, cocos2d: CCEvent * event); virtual void ccTouchEnded (cocos2d :: CCTouch * touch, cocos2d: CCEvent * event); void initWorld (); void setDebug (bool isDebug); static cocos2d: CCScene * scene (); CREATE_FUNC (TestBox) ;}; # endif/* defined (_ TestBox2d _ TestBox __)*/
2. Build the world, build the world box, and enable the debug mode
void TestBox::initWorld(){ mWorld = new b2World(b2Vec2(0, -10)); mWorld->SetAllowSleeping(true); mWorld->SetContinuousPhysics(true); setDebug(true);}
3. If debug mode is enabled, debug rendering is enabled.
void TestBox::setDebug(bool isDebug){ if (isDebug) { mDebugDraw = new GLESDebugDraw(RATIO); mWorld->SetDebugDraw(mDebugDraw); uint32 flags = 0; flags += b2Draw::e_shapeBit; flags += b2Draw::e_jointBit; flags += b2Draw::e_pairBit; flags += b2Draw::e_centerOfMassBit; mDebugDraw->SetFlags(flags); draw(); }}void TestBox::draw(){ ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position ); kmGLPushMatrix(); mWorld->DrawDebugData(); kmGLPopMatrix();}
4. initialize the world in init and call the update function.
The Code is as follows:
//// TestBox. cpp // TestBox2d /// Created by lerry on 14-3-4. // Copyright (c) 2014 GoonearTechnology Co ., ltd. all rights reserved. // # include TestBox. hUSING_NS_CC; TestBox ::~ TestBox () {delete mWorld; mWorld = NULL; delete mDebugDraw; mDebugDraw = NULL;} CCScene * TestBox: scene () {CCScene * s = CCScene: create (); testBox * test = TestBox: create (); s-> addChild (test); return s;} bool TestBox: init () {if (! CCLayer: init () {return false;} visualSize = CCDirector: sharedDirector ()-> getVisibleSize (); initWorld (); drawBox (); scheduleUpdate (); return true;} void TestBox: update (float dt) {int velocityIterat = 8; int positionIterat = 1; mWorld-> Step (dt, velocityIterat, positionIterat ); for (b2Body * B = mWorld-> GetBodyList (); B = B-> GetNext () {if (B-> GetUserData ()! = NULL) {CCSprite * sprite = (CCSprite *) B-> GetUserData (); sprite-> setPosition (ccp (B-> GetPosition (). x * RATIO, B-> GetPosition (). y * RATIO); sprite-> setRotation (-1) * CC_RADIANS_TO_DEGREES (B-> GetAngle () ;}} void TestBox: drawBox () {// floor b2Body * ground = NULL; b2BodyDef bd; ground = mWorld-> CreateBody (& bd); // bottom side b2EdgeShape shape; shape. set (b2Vec2 (0.1f, 0.1f), b2Vec2 (visualSize. width/RATIO-0.1f, 0.1f); ground-> CreateFixture (& shape, 0.0f); shape. set (b2Vec2 (0.1f, 0.1f), b2Vec2 (0.1f, visualSize. height/RATIO-0.1f); ground-> CreateFixture (& shape, 0.0f); shape. set (b2Vec2 (0.1f, visualSize. height/RATIO-0.1f), b2Vec2 (visualSize. width/RATIO-0.1f, visualSize. height/RATIO-0.1f); ground-> CreateFixture (& shape, 0.0f); shape. set (b2Vec2 (visualSize. width/RATIO-0.1f, 0.1f), b2Vec2 (visualSize. width/RATIO-0.1f, visualSize. height/RATIO-0.1f); ground-> CreateFixture (& shape, 0.0f);} {role = CCSprite: create(poop_1.png); CCSize size = role-> getContentSize (); role-> setPosition (ccp (visualSize. width/2, visualSize. height/2); b2BodyDef bodyDef; bodyDef. type = b2_dynamicBody; bodyDef. userData = role; bodyDef. position. set (visualSize. width/2/RATIO, visualSize. height/2/RATIO); b2Body * body = mWorld-> CreateBody (& bodyDef); b2PolygonShape shape; shape. setAsBox (size. width/2/RATIO, size. height/2/RATIO); b2FixtureDef fixDef; fixDef. shape = & shape; body-> CreateFixture (& fixDef); addChild (role) ;}} void TestBox: initWorld () {mWorld = new b2World (b2Vec2 (0, -10); mWorld-> SetAllowSleeping (true); mWorld-> SetContinuousPhysics (true); setDebug (true);} void TestBox: setDebug (bool isDebug) {if (isDebug) {mDebugDraw = new RATIO (RATIO); mWorld-> SetDebugDraw (mDebugDraw); uint32 flags = 0; flags + = b2Draw: e_shapeBit; flags + = b2Draw:: e_jointBit; flags + = b2Draw: e_pairBit; flags + = b2Draw: e_centerOfMassBit; mDebugDraw-> SetFlags (flags); draw () ;}} void TestBox: draw () {vertex (vertex); kmGLPushMatrix (); mWorld-> DrawDebugData (); kmGLPopMatrix ();} bool TestBox: ccTouchBegan (cocos2d: CCTouch * touch, cocos2d :: CCEvent * event) {CCLog (touch); return true;} void TestBox: ccTouchMoved (cocos2d: CCTouch * touch, cocos2d: CCEvent * event) {} void TestBox :: ccTouchEnded (cocos2d: CCTouch * touch, cocos2d: CCEvent * event) {} void TestBox: onEnter () {CCDirector * pDirector = CCDirector: shareddire (); pDirector-> getTouchDispatcher ()-> addTargetedDelegate (this, 0, true); CCLayer: onEnter ();} void TestBox: onExit () {CCDirector * pDirector = ccdire :: shareddire(); pDirector-> getTouchDispatcher ()-> removeDelegate (this); CCLayer: onExit ();}
Isn't there any comments? Good code doesn't need comments.