cocos2dx之box2d画刚体外边框
2016-04-01 16:08
357 查看
其实这些代码都可以在示例项目中找到,我以3.0引擎为例把代码贴出来:
首先到工程到引擎目录下tests/cpp-tests/Classes/Box2DTestBed中将GLES-Render.cpp和GLES-Render.h拷贝到工程中
其次在.h中添加#include "GLES-Render.h"并加入以下代码
然后在.cpp中
最后,在创建b2world对象的时候加入这段代码
现在运行一下,就可以画出来了
不要忘了在onExit()中将这些new出来的对象删除
首先到工程到引擎目录下tests/cpp-tests/Classes/Box2DTestBed中将GLES-Render.cpp和GLES-Render.h拷贝到工程中
其次在.h中添加#include "GLES-Render.h"并加入以下代码
<span style="font-family: Arial, Helvetica, sans-serif;"> virtual void draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated) override;</span>
kmMat4 _modelViewMV; void onDraw(); CustomCommand _customCommand;
<pre name="code" class="cpp"> GLESDebugDraw* m__debugDraw;
然后在.cpp中
void MyGameLayer::draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated) { Layer::draw(renderer, transform, transformUpdated);//我的<span style="font-family: Arial, Helvetica, sans-serif;">MyGameLayer类继承自Layer</span> GL::enableVertexAttribs( cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION ); kmGLPushMatrix(); kmGLGetMatrix(KM_GL_MODELVIEW, &_modelViewMV); _customCommand.init(_globalZOrder); _customCommand.func = CC_CALLBACK_0(MyGameLayer::onDraw, this); renderer->addCommand(&_customCommand); kmGLPopMatrix(); } void MyGameLayer::onDraw() { kmMat4 oldMV; kmGLGetMatrix(KM_GL_MODELVIEW, &oldMV); kmGLLoadMatrix(&_modelViewMV); m__world->DrawDebugData();//m__world是b2world对象 kmGLLoadMatrix(&oldMV); }
最后,在创建b2world对象的时候加入这段代码
m__debugDraw = new GLESDebugDraw(32.0); m__world->SetDebugDraw(m__debugDraw); uint32 flags = 0; flags += b2Draw::e_shapeBit; //flags += b2Draw::e_jointBit; //flags += b2Draw::e_aabbBit; //flags += b2Draw::e_pairBit; //flags += b2Draw::e_centerOfMassBit; m__debugDraw->SetFlags(flags);
现在运行一下,就可以画出来了
不要忘了在onExit()中将这些new出来的对象删除
相关文章推荐
- android JNI 与cocos结合篇(二)
- Cocos2dx引擎9-注册事件监听器
- cocos2dx lua UI栈
- cocos2dx之box2d创建椭圆刚体
- cocos ios,other linker flags 加入 -objc 编译报错解决方案
- cocos2dx3.3视频播放的实现VideoPlayer的使用
- Cocos2d-x 3.4在AndroidStudio上编译配置
- cocos2d-x拼图游戏
- cocos2dx 防御式编程模式
- Cocos游戏引擎VRDemo正式放出 助力虚拟现实游戏开发
- Cocos2dx学习笔记12:cocos2dx进度条(ProgressTimer)
- Cocos2dx学习笔记11:cocos2dx调度器(scheduler)
- Cocos2dx学习笔记10:cocos2dx序列帧动画(1)
- CocoStudio使用笔记2:cocos2dx3.9使用CocoStudio制作的进度条LoadingBar
- CocoStudio使用笔记1:cocos2dx3.4加载CocoStudio导出的csb文件
- cocos: 无限循环滚动背景
- Cocos2d-JS V3.10 一个小bug提示
- cocos-js添加微信js sdk
- cocos2dx 内存管理
- Cocos2d-x 3.0正式版及android环境搭建详细教程