b2Draw子类成员函数未调用

时间:2015-05-14 07:52:10

标签: c++ box2d physics

我正在尝试为另一个关于Box2d的问题创建一个最小的测试用例,但是我无法调用我的自定义抽屉的成员函数:

#include <iostream>

#include "Box2D/Box2D.h"

class DebugDrawer : public b2Draw
{
public:
    DebugDrawer() {
        AppendFlags(b2Draw::e_shapeBit);
        AppendFlags(b2Draw::e_jointBit);
        AppendFlags(b2Draw::e_aabbBit);
        AppendFlags(b2Draw::e_pairBit);
        AppendFlags(b2Draw::e_centerOfMassBit);
    }

    void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color&)
    {
        for (int vertex = 0; vertex < vertexCount; ++vertex) {
            b2Vec2 vec = vertices[vertex];
            std::cout << "DrawPolygon" << "vertex" << vertex << "x" << vec.x << "y" << vec.y;
        }
    }

    void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color&)
    {
        for (int vertex = 0; vertex < vertexCount; ++vertex) {
            b2Vec2 vec = vertices[vertex];
            std::cout << "DrawSolidPolygon" << "vertex" << vertex << "x" << vec.x << "y" << vec.y;
        }
    }

    void DrawCircle(const b2Vec2& center, float32 radius, const b2Color&)
    {
        std::cout << "DrawCircle" << "x" << center.x << "y" << center.y << "radius" << radius;
    }

    void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2&, const b2Color&) {
        std::cout << "DrawSolidCircle" << "x" << center.x << "y" << center.y << "radius" << radius;
    }

    void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color&) {
        std::cout << "DrawSegment" << "p1.x" << p1.x << "p1.y" << p1.y << "p2.x" << p2.x << "p2.y" << p2.y;
    }

    void DrawTransform(const b2Transform& xf) {
        std::cout << "DrawTransform" << "x" << xf.p.x << "y" << xf.p.y << "angle" << xf.q.GetAngle();
    }
};

int main(int argc, char** argv)
{
    b2World world(b2Vec2(0.0f, 0.0f));
    DebugDrawer debugDrawer;
    world.SetDebugDraw(&debugDrawer);

    // body
    b2BodyDef bd;
    bd.type = b2_dynamicBody;
    bd.position = b2Vec2(0, 0);
    b2Body *body = world.CreateBody(&bd);
    // shape
    b2CircleShape shape;
    shape.m_radius = 1.0f;
    // fixture
    b2FixtureDef fd;
    fd.shape = &shape;
    fd.density = 1.0f;
    body->CreateFixture(&fd);

    world.Step(1.0f / 120.0f, 8, 3);

    return 0;
}

我曾多次试过Step(),强迫身体保持清醒等等。我错过了什么?

1 个答案:

答案 0 :(得分:1)

我认为您明确需要致电world.DrawDebugData()

另外,您知道可以拨打SetFlags(a | b | c);而不是AppendFlags(a); AppendFlags(b); AppendFlags(c);吗?