如何使用Coco2d-x 3.0 beta 2启用Box2d调试绘图

时间:2014-03-09 12:51:48

标签: debugging box2d cocos2d-x cocos2d-x-3.0

如何使用Cocos2d-x 3.0启用调试绘图?有些代码使用cocos2d-x 2.0来执行此操作,但它们甚至不能在Cocos2d-x上编译。不幸的是我不熟悉cocos2d-x和Box2d,我不知道如何移植它们。如果您要分享绘制形状的方法,那将是很好的。

编辑:

我发现了这个:

http://blog.csdn.net/tian2kong/article/details/20386213

我已经覆盖了我的应用程序主draw的{​​{1}}方法,如下所示:

Layer

也做到了这一点:

void HelloWorld::draw()
{
    GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION);
    kmGLPushMatrix();
    m_world->drawDebugData();
    kmGLPopMatrix();
}

它有效!但是当我添加一个精灵时,这些形状会让我精神恍惚。如何把它们带到前面?

4 个答案:

答案 0 :(得分:2)

打开聚光灯并搜索GLES-Render.cpp。该文件将位于cocos2dx文件夹中的路径中。打开封闭文件夹并将文件GLES-Render.h和GLES-Render.cpp拖到项目中。 然后在init方法中添加以下代码

b2Draw *m_debugDraw = new  GLESDebugDraw(PTM_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);
H_world->SetDebugDraw(m_debugDraw); 

然后添加draw方法。

void HelloWorld::draw()
{
kmGLPushMatrix();
H_world->DrawDebugData();
kmGLPopMatrix();
}

不要忘记包含GLES-Render.h

答案 1 :(得分:1)

你不能把身体带到前面。如果你想在你的精灵后面看到你的身体,你可以将它们的不透明度设置为精灵 - > setOpacity(100)。

我使用以下代码绘制

void GameLayer:: setPhysics() {
b2Vec2 gravity = b2Vec2(0.0f,0.0f);// Initializing World
world = new b2World(gravity);

m_debugDraw = new GLESDebugDraw( PTM_RATIO );
world->SetDebugDraw(m_debugDraw);

world->SetAllowSleeping(false);

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);

contactListener = new MyContactListener(this);
world->SetContactListener(contactListener);
}

和你的相同....你能不能再解释一下你的问题

答案 2 :(得分:1)

对于调试绘图,您需要两个文件

File Download Link

之后

打包该文件

并在.h文件中创建一个Vaiable

  

GLESDebugDraw * debugDraw;

和.ccp文件

b2Vec2 gravity;
gravity.Set(0.0f, -9.8f);
this->world = new b2World(gravity);

this->debugDraw = new GLESDebugDraw(PTM_RATIO);
this->world->SetDebugDraw(debugDraw);


uint32  flags  =  0 ;
flags +=  b2Draw::e_shapeBit ;
flags += b2Draw::e_jointBit;
// flags + = b2Draw :: e_aabbBit;
// flags + = b2Draw :: e_pairBit;
flags += b2Draw::e_centerOfMassBit;
this->debugDraw->SetFlags (flags);

in。 h文件

  

void draw(Renderer * renderer,const Mat4& transform,uint32_t flags);

in .cpp

void HelloWorld::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
    Layer::draw(renderer, transform, flags);
    Director* director = Director::getInstance();
    GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION );
    director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
    world->DrawDebugData();
    director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}

现在享受调试绘制

答案 3 :(得分:0)

您可以在当前图层上方添加另一个图层。

您可以在上层添加您在此处发布的代码,然后一切都应该正常工作。

相关问题