Box2D:如何手动渲染实体 [英] Box2D: How to manually render a body

查看:128
本文介绍了Box2D:如何手动渲染实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功将Box2D安装到我的项目中.但是我该如何渲染一个身体?假设我使用的东西支持绘制多边形.我只想找出身体多边形顶点的当前位置,并用引擎绘制它.

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.

推荐答案

我找到了!!!

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

这篇关于Box2D:如何手动渲染实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆