Debug Draw isn't working

HI
My DebugDraw doesn't seem to be working. The code doesn't seem to be accessing any of the functions at all (I know cause I've put cout statements in all of them). I added it to the World using the SetDebugDraw function and I even checked the framework I seem to be doing the same thing !!

I'm using Ubuntu 8.10 and Box2D ver 2.01 (cmake-build).

Here is the code in case anyone is Interested -
Code:
#ifndef DEBUGDRAW_H
#define DEBUGDRAW_H

#include <Box2D/Box2D.h>
#include <SDL/SDL_gfxPrimitives.h>

class DebugDraw : public b2DebugDraw
{
public:
    DebugDraw();
    virtual ~DebugDraw();

    void DrawPolygon (const b2Vec2 *vertices, int32 vertexCount, const b2Color &color);
    void DrawSolidPolygon (const b2Vec2 *vertices, int32 vertexCount, const b2Color &color);
    void DrawCircle (const b2Vec2 &center, float32 radius, const b2Color &color);
    void DrawSolidCircle (const b2Vec2 &center, float32 radius, const b2Vec2 &axis, const b2Color &color);
    void DrawSegment (const b2Vec2 &p1, const b2Vec2 &p2, const b2Color &color);
    void DrawXForm (const b2XForm &xf);
protected:
private:
};

#endif // DEBUGDRAW_H


Code:
#include "DebugDraw.h"
#include <iostream>
DebugDraw::DebugDraw()
{
    //ctor
    std::cout << "Constructor - DebugDraw" << std::endl;
}

DebugDraw::~DebugDraw()
{
    //dtor
}


void DebugDraw::DrawPolygon (const b2Vec2 *vertices, int32 vertexCount, const b2Color &color)
{
    SDL_Surface * screen = SDL_GetVideoSurface();
    for(int i=0; i < vertexCount-1; i++)
    {
        lineRGBA( screen, vertices[i].x, vertices[i+1].x, vertices[i].y, vertices[i+1].y,
                    0, 255, 0, 255 );
    }
    lineRGBA( screen, vertices[vertexCount-1].x, vertices[0].x,
                    vertices[vertexCount-1].y, vertices[0].y,
                    0, 255, 0, 255 );
    std::cout << "Drew Polygon" << std::endl;

}

void DebugDraw::DrawSolidPolygon (const b2Vec2 *vertices, int32 vertexCount, const b2Color &color)
{
    DrawPolygon( vertices, vertexCount, color);
    std::cout << "Drew Solid Polygon" << std::endl;
}

void DebugDraw::DrawSegment (const b2Vec2 &p1, const b2Vec2 &p2, const b2Color &color)
{
    std::cout << "Drawing Segment" << std::endl;
    lineRGBA( SDL_GetVideoSurface(), p1.x, p2.x, p1.y, p2.y, 0, 0, 255, 255);
}
void DebugDraw::DrawCircle (const b2Vec2 &center, float32 radius, const b2Color &color)
{
    circleRGBA( SDL_GetVideoSurface(), center.x, center.y, radius, 0, 255, 255, 255);
    std::cout << "Drawing Solid Circle" << std::endl;
}

void DebugDraw::DrawSolidCircle (const b2Vec2 &center, float32 radius, const b2Vec2 &axis,const b2Color &color)
{
    circleRGBA( SDL_GetVideoSurface(), center.x, center.y, radius, 0, 255, 255, 255);
}
void DebugDraw::DrawXForm (const b2XForm &xf)
{
    std::cout << "Drawing XForm" << std::endl;
}


If someone is feeling really benevolent maybe they could add the code to a blank project on their system and try it out ? (I'm using SDL along with SDL_gfx )

🙂
Hi Vish,

I'm new to Box2D, but I thought I'd give your code a test. I took the HelloWorld example code, initialized an SDL window and set the DebugDraw to your drawing class. In order to get it to work, I first removed the constructor and destructor as they are not needed. Then I set the draw shapes flag. That is probably what was stopping it?

You can do it like this:

Code:
uint32 flags = b2DebugDraw::e_shapeBit;
DebugDraw m_debugDraw;
world.SetDebugDraw(&m_debugDraw);
m_debugDraw.SetFlags(flags);

I'm not sure how related this is, but I also tried a different method to draw the shapes. I removed the inheritance from the class and slightly modified the methods for my engines rendering system. I then used some code I found on this forum to get the vertices from all the shapes attached to a body. Going through the list, I draw rectangles from the points using your DrawPolygon method. Now the problem is that the shapes drawn are wildly moving around and totally out of control. Outputting the vertex positions has them increasing at a very fast rate. If I instead use the shapes position vector and add a bit of padding to increase the size (I still don't know how to properly scale yet), it looks more normal.

So I'm not sure why the vertices have wacky coordinates but the shapes position seems fine.
Forgot to mention that the order of the parameters is not correct for your SDL Gfx methods. You are giving it (x1, x2, y1, y2) when it should be (x1, y1, x2, y2)

Cheers,
Thanks a lot Mike, you're awesome !!

It's finally working (Not that it matters anymore, and it's not working made me understand Box2D better !!)

Still WOO HOO !!

Original thread: https://box2d.org/forum/viewtopic.php?t=1944