views:

51

answers:

2

Hi,

I'm getting crazy... I succeed installing box2d into my project. But how can I draw a body? Of course I need an engine to draw with, but that doesn't matter now (SDL). I just want to reach that I can get all the coordinates of the points that represent the body-polygon, to draw it with the engine. (Not relevant for circles)

If you can help me, I will be very thankful.
I'm already searching for houres...
alt text

+1  A: 

The Box2D manual refers to a HelloWorld project that is bundled in the download. The same documentation also goes through it step by step. Quoting the manual:

The program creates a large ground box and a small dynamic box. This code does not contain any graphics. All you will see is text output in the console of the box's position over time.

If you haven't got a rectangle to work, this should help you get started.

Peter Jaric
+1  A: 

I found it!!!

void Box2DUtils::DrawBody(SDL_Surface *buffer, b2Body *body, int fr, int fg, int fb, int falpha, int lr, int lg, int lb, int lalpha, bool aa)
{
    const b2Transform& xf = body->GetTransform();
    for (b2Fixture* f = body->GetFixtureList(); f; f = f->GetNext())
    {
        switch (f->GetType())
        {
        case b2Shape::e_circle:
        {
            b2CircleShape* circle = (b2CircleShape*) f->GetShape();

            b2Vec2 center = b2Mul(xf, circle->m_p);
            float32 radius = circle->m_radius;
            b2Vec2 axis = xf.R.col1;

            //m_debugDraw->DrawSolidCircle(center, radius, axis, color);
            if (falpha > 0)
            {
                filledCircleRGBA(buffer, center.x, center.y, (int) radius, fr, fg, fb, falpha);
            }
            if (lalpha > 0)
            {
                if (aa)
                {
                    aacircleRGBA(buffer, center.x, center.y, (int) radius, lr, lg, lb, lalpha);
                } else
                {
                    aacircleRGBA(buffer, center.x, center.y, (int) radius, lr, lg, lb, lalpha);
                }
            } else if (aa)
            {
                aacircleRGBA(buffer, center.x, center.y, (int) radius, fr, fg, fb, falpha);
            }

        }
            break;

        case b2Shape::e_polygon:
        {
            b2PolygonShape* poly = (b2PolygonShape*) f->GetShape();
            int32 vertexCount = poly->m_vertexCount;
            b2Assert(vertexCount <= b2_maxPolygonVertices);
            b2Vec2 vertices[b2_maxPolygonVertices];
            Sint16 xv[b2_maxPolygonVertices];
            Sint16 yv[b2_maxPolygonVertices];
            for (int32 i = 0; i < vertexCount; ++i)
            {
                vertices[i] = b2Mul(xf, poly->m_vertices[i]);
                xv[i] = (int) vertices[i].x;
                yv[i] = (int) vertices[i].y;
            }
            if (falpha > 0)
            {
                filledPolygonRGBA(buffer, xv, yv, (Sint16) vertexCount, fr, fg, fb, falpha);
            }
            if (lalpha > 0)
            {
                if (aa)
                {
                    aapolygonRGBA(buffer, xv, yv, (Sint16) vertexCount, lr, lg, lb, lalpha);
                } else
                {
                    polygonRGBA(buffer, xv, yv, (Sint16) vertexCount, lr, lg, lb, lalpha);
                }
            } else if (aa)
            {
                aapolygonRGBA(buffer, xv, yv, (Sint16) vertexCount, fr, fg, fb, falpha);
            }
            //m_debugDraw->DrawSolidPolygon(vertices, vertexCount, color);
        }
            break;
        }
    }
}
Martijn Courteaux