cocos2d-x box2d使用调试绘图

cocos2d-x box2d使用调试绘图

复制TestCpp的GLES-Render.h和GLES-Render.cpp过来。

添加一个成员变量:

GLESDebugDraw *m_debugDraw;

初始化物理引擎的时候:

void HNGameLayer::initPhysics()
{
    m_debugDraw = new GLESDebugDraw(RATIO);
    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);
    m_b2World = new b2World(b2Vec2(0, -10));
    m_b2World->SetAllowSleeping(true);          // 允许物体进入休眠状态
    m_b2World->SetContinuousPhysics(true);      // 使用连续物理碰撞检测
    m_b2World->SetDebugDraw(m_debugDraw);
    
    ....其他代码
}

重写draw函数

void HNGameLayer::draw()
{
    CCLayer::draw();
    if (!m_bDebug) {
        return;
    }
    ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
    
    kmGLPushMatrix();
    
    m_b2World->DrawDebugData();
    
    kmGLPopMatrix();
    
    CHECK_GL_ERROR_DEBUG();
}

防止被背景阻挡了。因为draw函数话出来的东西的z-order应该是0,把背景的z-order设成负数就可以了

原文地址:https://www.cnblogs.com/skyhacker/p/3667037.html