在上一篇文章中我们创建了的一个物理世界,当物理世界中的刚体一个也没有显示出来。为显示物理世界中的物体,我们需要引入GLES-Render(调试Box2D使用)。这两个文件可以再

%Cocos_Home%\samples\Cpp\TestCpp\Classes\Box2DTestBed中找到,将这两个文件拷贝到项目中并引入工程中。

再声明一个变量

    b2World* world;
b2Body* wallBody ; float32 wallLineOffset ; GLESDebugDraw* m_debugDraw; //调试物理世界绘制对象

声明两个函数

    void printDebugDraw(bool isPrint);
virtual void draw();

printDebugDraw实现

void HelloWorld::printDebugDraw(bool isPrint){
if(!isPrint)
return;
//根据像素与米的转换系数创建调试绘制对象
m_debugDraw = new GLESDebugDraw( PIXEL_TO_METER );
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); }

draw实现

void HelloWorld::draw()
{
CCLayer::draw(); ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position ); kmGLPushMatrix(); world->DrawDebugData(); kmGLPopMatrix(); }

修改createWorld

void HelloWorld::createWorld()
{
b2Vec2 gravity;
gravity.Set(0.0f,-9.0f); //物理世界重力向量,这里创建一个向下的重力向量
b2BodyDef* bodydef = new b2BodyDef();
world = new b2World(gravity); //根据重力向量创建物理世界对象 world->SetAllowSleeping(true); //允许休眠
world->SetWarmStarting(true); //初始状态将受到重力影响 printDebugDraw(true);
}

现在运行看看效果吧

物理世界的物体就显示出来了,但那几个球怎么没有动呢?不用急,是因为我们还没有让物理世界运行起来。

  让物理世界运行起来

添加函数

void HelloWorld::update(float dt)
{
//速度迭代次数
int velocityIterations = 8;
//位置迭代次数
int positionIterations = 1; world->Step(dt, velocityIterations, positionIterations); //dt为时间步长 }

在init方法中添加代码

bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
} wallLineOffset = 0.5; this->createWorld();
this->createWall();
this->createBall(); this->scheduleUpdate(); //不断调用update函数
…… return true;
}

再运行看看效果

最新文章

  1. [NOIP2011]Mayan游戏 题解
  2. matlab初学之plot颜色和线型
  3. Linux下,使用Git管理 dotfiles(配置文件)
  4. <marquee>属性详解
  5. Linux基础教程之/dev/null和/dev/zero的区别及其用法
  6. EXTJS API
  7. 4种检测是否支持HTML5的方法,你知道几个?
  8. 调查程序phpquestionnaire 2.2中文安装注意
  9. Uploadif稍做扩展使用
  10. java基础点总结
  11. 【译】学习JavaScript中提升、作用域、闭包的终极指南
  12. vue-cli快速原型开发
  13. Expm 9_1 有向图中环的判断问题
  14. Nginx浏览目录配置及美化
  15. 选择、操作web元素-3
  16. nodejs乱码处理
  17. day 64 crm项目(1) admin组件的初识别以及应用
  18. shell 变量定义使用
  19. mysqld_safe脚本执行的基本流程
  20. Python的知识点 plt.plot()函数细节

热门文章

  1. GetMenu返回0解决方法
  2. Swift—类的继承-备
  3. Ubuntu 查看和杀死进程[转]
  4. 转:分享13款PHP开发框架
  5. Entity Framework with MySQL 学习笔记一(验证标签)
  6. 8.2.1.15 ORDER BY Optimization ORDER BY 优化
  7. 【转】Java 集合系列16之 HashSet详细介绍(源码解析)和使用示例--不错
  8. Redis应用场景 及其数据对象 string hash list set sortedset
  9. SpringMVC+highstock实现曲线报表
  10. 整型数组处理算法(八)插入(+、-、空格)完成的等式:1 2 3 4 5 6 7 8 9=N[华为面试题]