如何在box2d中获取身体的宽度和高度?

时间:2014-03-28 07:12:31

标签: libgdx box2d

我正在Libgdx引擎中开展游戏。我必须从box2d中的身体获得宽度和高度。是他们在box2d ??

中获取身体宽度和高度的任何方法

8 个答案:

答案 0 :(得分:2)

嗯我不太确定,但这似乎相当复杂,因为box2d机身可以由几个灯具组成,每个灯具都有不同的形状。

您只是在寻找边界框的宽度和高度吗?

然后,您可能必须遍历主体的夹具列表并评估其中的形状,查看形状的位置并计算边界框。不幸的是,这看起来并不舒服。

答案 1 :(得分:0)

((Sprite)body.getUserData()).getWidth();

((Sprite)body.getUserData()).getHeight();

使用它..如果我猜错了它应该回答你的问题

答案 2 :(得分:0)

我之前遇到过这个问题,这是我的解决方案,你必须遍历所有灯具然后开始获取每个灯具的b2AABB矩形并将它们组合在一起,然后获得组合AABB的宽度和高度

    b2AABB aabb;
    b2Transform t;
    t.SetIdentity();
    b2Fixture* fixture = nearBody->GetFixtureList();
    while (fixture != NULL) {
        const b2Shape *shape = fixture->GetShape();
        const int childCount = shape->GetChildCount();
        for (int child = 0; child < childCount; ++child) {
            b2AABB shapeAABB;
            shape->ComputeAABB(&shapeAABB, t, child);
            shapeAABB.lowerBound = shapeAABB.lowerBound;
            shapeAABB.upperBound = shapeAABB.upperBound;
            aabb.Combine(shapeAABB);
        }
        fixture = fixture->GetNext();
    }

    CCPoint lowerVertex = aabb.lowerBound;
    CCPoint heigherVertex = aabb.upperBound;
    bodyWidth = heigherVertex.x - lowerVertex.x;
    bodyHeight = heigherVertex.y - lowerVertex.y;

答案 3 :(得分:0)

检查b2World.cpp,有一些代码可能对你有帮助:

void b2World::DrawDebugData() {
    ...
    for (b2Body* b = m_bodyList; b; b = b->GetNext()) {
        const b2Transform& xf = b->GetTransform();
        for (b2Fixture* f = b->GetFixtureList(); f; f = f->GetNext()) {
            ...
            DrawShape(f, xf, b2Color(0.5f, 0.5f, 0.3f));
        }
    }
    ...
}

void b2World::DrawShape(b2Fixture* fixture, const b2Transform& xf, const b2Color& color) {
    ...
    case b2Shape::e_polygon: {
            b2PolygonShape* poly = (b2PolygonShape*)fixture->GetShape();
            int32 vertexCount = poly->m_count;
            b2Vec2 vertices[b2_maxPolygonVertices];
            for (int32 i = 0; i < vertexCount; ++i) {
                //they are polygen's vertices, you can use these to get the height / width
                vertices[i] = b2Mul(xf, poly->m_vertices[i]);
            }
            m_debugDraw->DrawSolidPolygon(vertices, vertexCount, color);
        }
    ...
}

答案 4 :(得分:0)

这是我在关注this libgdx教程时提出的。

float w2 = 0f
float h2 = 0f
b.body.fixtureList.each { Fixture fixture ->
    if(fixture) {
        if(fixture.shape.type == Shape.Type.Circle) {
            w2 += fixture.shape.radius * 2
            h2 += fixture.shape.radius * 2
        } else if(fixture.shape.type == Shape.Type.Polygon) {
            PolygonShape shape = fixture.shape as PolygonShape
            Vector2 vector = new Vector2()

            def vectors = []
            shape.vertexCount.times {
                shape.getVertex(it, vector)
                vectors.add(new Vector2(vector.x, vector.y))
            }

            vectors.sort {it.x}
            w2 = (vectors.first().x).abs() + (vectors.last().x).abs()

            vectors.sort {it.y}
            h2 = (vectors.first().y).abs() + (vectors.last().y).abs()
        }
    }
}

答案 5 :(得分:0)

我也将此答案发布在gamedev.stackexchange.com的同一线程上。我知道这是一个非常老的线程,但我只是用自己的方式在这里获取了身体的大小:{{3 }}
如果链接对您来说太怪异,则为以下代码:

dict

答案 6 :(得分:0)

如果仅将polygonShape用作框,为什么不创建包装器类。并将相关的信息(例如宽度,高度和位置)存储在包装类变量中。

答案 7 :(得分:0)

(如果您要在运行时更改主体尺寸(添加/删除夹具),则在将主体尺寸更改为以下方法时可能需要进行一些修改,但它应该可以工作。)

注意:我使用的是 Java+jbox2d

我能想到的最简单的方法是在创建主体时自己存储对象的高度和宽度

float objectHeight;
float objectWidth;

当您创建身体(特别是形状)时。 只需将所有维度加在一起

objectWidth = fixture1Shape.width + ...;
objectHeight = fixture1Shape.height + ...;

然后它只是在对象中创建自定义方法

float getWidth(){
   return objectWidth;
}

float getHeight(){
   return objectHeight;
}

现在您可以获得对象主体的确切宽度和高度。 使用

o1.getHeight();
o1.getWidth();

[考虑到 o1 是您找到对象的位置,例如 Object o1 = fixture.getUserData();Object o1=body.getUserData()]

您可能需要创建所需类的新对象并将其转换为对象,然后再调用方法

//consider your object class is Player
Player p1 = (Player) o1;
p1.getHeight();
p1.getWidth();

PS:如果您需要 Box2D 世界大小,只需使用您的缩放因子进行缩放。 或者最好在创建形状时将其作为缩放版本存储在 objectHeight,objectWidth 中

相关问题