BeginnersQ: DebugDraw not working after overriding and specifying flags

Hey everyone, as the title says, I am having a tough time getting the debug draw stuff to work. The functions themselves are not even firing, as I have printfs all over the place and none of them are being triggered. Here's some code:

Code: Select all

class GameScreen : public Screen {
public:
  GameScreen(Game* const game, sf::RenderWindow* const window);
  void showScreen() override;

private:
  PhysicsDebugDraw physicsDebugDraw_;

  // Physics
  b2World world_;
  b2BodyDef groundBodyDef_;
  b2Body* groundBody_;
  b2PolygonShape groundBox_;

  b2BodyDef bodyDef_;
  b2Body* body_;
  b2PolygonShape dynamicBox_;
  b2FixtureDef fixtureDef_;

  int32 velocityIterations = 8;
  int32 positionIterations = 3;
};

// Constructor
GameScreen::GameScreen(Game* const game, sf::RenderWindow* const window)
  : Screen(game)
   , physicsDebugDraw_(window)
  , world_(b2Vec2(0.0f, -100.0f)) {}

// When the screen is shown
void
GameScreen::showScreen() {
  // Do some physics
   world_.SetDebugDraw(&physicsDebugDraw_);
   physicsDebugDraw_.SetFlags(PhysicsDebugDraw::e_shapeBit);

  const sf::Vector2u* peter = GameSetup::getInstance().getSize();
  groundBodyDef_.position.Set(peter->x * 0.0f, peter->y * 0.75f);
  groundBody_ = world_.CreateBody(&groundBodyDef_);
  groundBox_.SetAsBox(peter->x, 10.0f);
  groundBody_->CreateFixture(&groundBox_, 0.0f);

  bodyDef_.type = b2_dynamicBody;
  bodyDef_.position.Set(peter->x * 0.5f, 0.0f);
  body_ = world_.CreateBody(&bodyDef_);
  dynamicBox_.SetAsBox(1.0f, 1.0f);
  fixtureDef_.shape = &dynamicBox_;
  fixtureDef_.density = 1.0f;
  fixtureDef_.friction = 0.3f;
  body_->CreateFixture(&fixtureDef_);
  }
 
  // Update the game every frame
void
GameScreen::update(const sf::Time& dt) {
  world_.Step(dt.asSeconds(), velocityIterations, positionIterations);
  if (body_ != nullptr) {
    b2Vec2 position = body_->GetPosition();
    float32 angle = body_->GetAngle();
    printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
  }
}


// My header file for PhysicsDebugDraw
class PhysicsDebugDraw : public b2Draw {
  public:
      PhysicsDebugDraw(sf::RenderWindow* const window)
         : window_(window) {}

      // Convert Box2D's OpenGL style color definition[0-1] to SFML's color definition[0-255]
      // with optional alpha byte[Default - opaque]
      static sf::Color GLColorToSFML(const b2Color &color, sf::Uint8 alpha = 255) {
         return sf::Color(static_cast<sf::Uint8>(color.r * 255),
            static_cast<sf::Uint8>(color.g * 255),
            static_cast<sf::Uint8>(color.b * 255), alpha);
      }

      // Convert Box2D's vector to SFML vector
      // [Default - scales the vector up by SCALE constants amount]
      static sf::Vector2f B2VecToSFVec(const b2Vec2 &vector, bool scaleToPixels = true) {
         return sf::Vector2f(vector.x * (scaleToPixels ? sfdd::SCALE : 1.f),
            vector.y * (scaleToPixels ? sfdd::SCALE : 1.f));
      }

    void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) override;
    void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) override;
    void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color) override;
    void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color) override;
    void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color) override;
    void DrawTransform(const b2Transform& xf) override;
      void DrawPoint(const b2Vec2& p, float32 size, const b2Color& color);

  private:
    sf::RenderWindow* const window_;

};

// Sample of one of the functions
void PhysicsDebugDraw::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) {
  printf("DRAWING POLYGON\n");
   sf::ConvexShape polygon(vertexCount);
   sf::Vector2f center;
   for(int i = 0; i < vertexCount; i++) {
      //polygon.setPoint(i, SFMLDraw::B2VecToSFVec(vertices[i]));
      sf::Vector2f transformedVec = PhysicsDebugDraw::B2VecToSFVec(vertices[i]);
    // flooring the coords to fix distorted lines on flat surfaces
      polygon.setPoint(i, sf::Vector2f(std::floor(transformedVec.x), std::floor(transformedVec.y)));
   }                                                                           // they still show up though.. but less frequently
   polygon.setOutlineThickness(-1.f);
   polygon.setFillColor(sf::Color::Transparent);
   polygon.setOutlineColor(PhysicsDebugDraw::GLColorToSFML(color));

   window_->draw(polygon);
}




Please notice that at the top of DrawPolygon, I printf straight away. I do this in all of the functions I have overridden in PhysicsDebugDraw.h and none of them are printing anything to the screen. I've only just started the tutorials and while it prints the location of the object just fine, I'm not getting any images OR prints. If I could just get the printfs to work I can do the rest, but if the functions aren't even firing I'm at a bit of a loss. Thank you.

EDIT: I put the code into a pastebin in case it makes it easier for people to read:
https://pastebin.com/qDQNL6g3

Thank you for the help!
For anyone seeing this in the future, your 'World' class has a function to draw the debug stuff. Try:

Code: Select all

world_->DrawDebugData()

or something similar in your render loop, it does NOT render your stuff just from the step call, you have to explicitly call it!

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