How to Enable Box2D debugDraw in Lua?

How to Enable Box2D debugDraw in Lua?
0.0 0


Hi guys,

I’m tring to open box2d debugDraw in Lua. As I know in Cpp, we need to override CCLayer:draw() function. But in Lua, I can’t get the override work done. Did you guys have idea about it?


You will have to implement the DebugDraw Layer in a C*+ class and generate a binding with toLua*+
However, tolua++ doesn’t seem to support multiple inheritance so:

Make a class DebugDraw that inherits from b2Draw. Make another class DebugDrawLayer that inherits from CCNode. In DebugDrawLayer, you create an instance of your DebugDraw class and add it to your box2D world. DebugDrawLayer::draw() then calls the draw method of your DebugDraw class. Then you only have to generate a binding for your DebugDrawLayer and use it in your Lua code!


Here is the basic stuff in the layer

//## DebugDrawLayer.h

#include "cocos2d.h"
#include "Box2D/Box2D.h"
#include "GLES-Render.h" 
#define PTM_RATIO 100

class DebugDrawLayer : public cocos2d::CCLayer {
    b2World * world;
    GLESDebugDraw * debugDraw;
    virtual bool init ();
    virtual void update (float deltaTime);

    void setupWorld();
    void setupDebugDraw();
    virtual void draw();

    CREATE_FUNC (DebugDrawLayer);

//## DebugDrawLayer.cpp

#include "DebugDrawLayer.h"
using namespace cocos2d;

bool DebugDrawLayer::init () {
    if (!CCLayer::init()) return false;


    return true;

void DebugDrawLayer::setupWorld()
    b2Vec2 gravity = b2Vec2(0.0f, -10.0f);
    world = new b2World(gravity);

void DebugDrawLayer::setupDebugDraw()
    debugDraw = new GLESDebugDraw(PTM_RATIO *CCDirector::sharedDirector()->getContentScaleFactor() ); // deals with pixels not points

void DebugDrawLayer::draw()
    // This is only for debug purposes
    // It is recommend to disable it

    ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );




//##  DebugDrawLayer.pkg

class DebugDrawLayer : public CCLayer {
    b2World * world;
    static DebugDrawLayer * create();

Run DebugDrawLayer.pkg tolua++ and use the DebugDrawLayer in lua like any other cocos2-d class

Hope it helps.


Thank you!~ Stanko KrstićMartin Z