This project is read-only.

How to enable Box2d DebugDraw

Topics: Actions and Control Logic, Physics, Sample Code and Demos
Jan 20, 2014 at 8:51 PM
Hi,
I would like to enable the Box2d debug draw. I have enabled it in the AngryNinjas demo but it is not working. First the font file cannot be found and after I have solved this I get the exception "BeginCustomDraw must be called before drawing anything.".

I checked with Google and it seems the class GLESDebugDraw is usually used with cocos2d, but this seems not to be available in Cocos2d-XNA.

Below the code from AngryNinjas:
void EnableDebugMode()
        {
            CCBox2dDraw debugDraw = new CCBox2dDraw("fonts/arial-12");
            world.SetDebugDraw(debugDraw);
            debugDraw.AppendFlags(b2DrawFlags.e_shapeBit);

        }

        public override void Draw()
        {
            //
            // IMPORTANT:
            // This is only for debug purposes
            // It is recommend to disable it
            //
            base.Draw();

            CCDrawManager.

            CCDrawingPrimitives.Begin();
            world.DrawDebugData();
            CCDrawingPrimitives.End();
            
        }
Thanks for your help

Olaf
Jan 21, 2014 at 12:35 AM
Hello

The steps to successfully using Cocos2D-XNA and Box2D:
  1. Ignore any cocos2d-x references to box2d on google as they don't convey in any understandable way for a novice user
  2. Look at the "Tests" code:
https://github.com/Cocos2DXNA/cocos2d-xna/blob/master/tests/tests/classes/tests/Box2DTestBet(Box2D)/Test.cs

In this class you will find the following ctor:
    public Test()
    {
        m_destructionListener = new DestructionListener();
        m_debugDraw = new CCBox2dDraw("fonts/arial-12");

        b2Vec2 gravity = new b2Vec2();
        gravity.Set(0.0f, -10.0f);
        m_world = new b2World(gravity);
        m_bomb = null;
        m_textLine = 30;
and many other useful demonstrations of how to setup the b2World object. Start here!
  1. The simplest world example is the falling boxes example:
https://github.com/Cocos2DXNA/cocos2d-xna/blob/master/tests/tests/classes/tests/Box2DTest/Box2DTest.cs

This example does not have a debug draw in it, sorry, but it demonstrates the very simple mechanics of a falling body in Box2D.
  1. Box2D is a physics engine that runs in parallel to the cocos2d-xna world. It has its own world that you must translate back to the visual cocos2d world. There is no magical mending of these worlds. Only you can figure out how to mix the two in your game and map the results of the physics simulation back to the visual world. We provide a simple physics sprite example in the Box2DTest, but that is the simplest example.
Jan 21, 2014 at 7:24 PM
Hi,
thanks for your help. I think so far I understand the AngryNinja sample. I have many years of experience with .NET and also played around with some other frameworks like Corona. I just don't really get the debug view running in a useful way. And I think the debug view is quite important specially at the beginning.
I will look at the tests this evening.

Thanks for the help

Olaf