首先我们需要拷贝GLES-Render.cpp,GLES-Render.h到我们工作的目录,
这个两个文件可以在coco2d-x的示例代码中的tests\Box2DTestBed中可以找到。我是在ubuntu下写的,要在mk文件中添加这个CPP文件才能编译使用。
- #ifndef __BOX_LAYER_H__
- #define __BOX_LAYER_H__
- #include "cocos2d.h"
- #include "Box2D/Box2D.h"
- #include "GLES-Render.h" //这里包含
- #define PTM_RATIO 32
- class BoxLayer : public cocos2d::CCLayer
- {
- public:
- b2World * world; //物理世界
- GLESDebugDraw *debugDraw; //这里新建示例
- virtual bool init();
- static cocos2d::CCScene * scene();
- void update(float dt); //这个是控制box2d世界的刷新器
- void draw(); //这里需要一个关键的draw函数,
- CREATE_FUNC(BoxLayer);
- };
- debugDraw = new GLESDebugDraw(PTM_RATIO); //这里新建一个 debug渲染模块
- world->SetDebugDraw(debugDraw); //设置
- uint32 flags = 0;
- flags += b2Draw::e_shapeBit ;
- //b2Draw::e_centerOfMassBit; //获取需要显示debugdraw的块
- //b2Draw::e_aabbBit; //AABB块
- //b2Draw::e_centerOfMassBit; 物体质心
- //b2Draw::e_jointBit; //关节
- //b2Draw::e_shapeBit; 形状
- debugDraw->SetFlags(flags); //需要显示那些东西
- draw(); //画出来
- scheduleUpdate();//这个一定要写
draw()方法:
- void BoxLayer::draw()
- {
- glDisable(GL_TEXTURE_2D);
- glDisableClientState(GL_COLOR_ARRAY);
- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
- world->DrawDebugData(); //这个一定要写
- glEnable(GL_TEXTURE_2D);
- glEnableClientState(GL_COLOR_ARRAY);
- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- }
update()方法:
- void BoxLayer::update()
- {
- world->Step(dt,7,7);
- }
本文详细介绍了如何在Cocos2d-x项目中集成Box2D物理引擎,包括必要的文件拷贝、代码配置及调试技巧。通过具体步骤指导读者实现Box2D在游戏开发中的应用。
1279

被折叠的 条评论
为什么被折叠?



