Pergunta

I succeed installing Box2D into my project. But how can I render a body? Assume I'm using something that supports drawing polygons. I just want to find out the current positions of the vertices of the body-polygon, to draw it with the engine.

If you can help me, I will be very thankful.

Foi útil?

Solução

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;
        }
    }
}

Outras dicas

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.

You should use World->SetDebugDraw(&myDebugDraw) and set the appropriate drawing flags to render the various aspects of the physics world (shapes, joints, center of gravity, etc.) Drawing flags are set via myDebugDraw.SetDebugFlags(flags).

myDebugDraw is an instance of b2Draw (b2DebugDraw in versions 2.1.2 and prior) which most "platforms" have a readily available implementation. During your rendering callback, use World->DrawDebugData() and the appropriate content will be drawn via your instance of myDebugData.

Hope this wasn't to confusing given the shortness of the post. All this is covered in the Box2d documentation.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top