获取多边形形状体的顶点列表

时间:2013-11-20 14:35:41

标签: java libgdx

如何在libgdx中获取特定多边形体的顶点列表?

像这样:

public Array<Vector2> getVerts(Body body){
    Array<Vector2>verts = null;

    // can't find how to look them up properly anywhere

    return verts;
}

谢谢!

2 个答案:

答案 0 :(得分:3)

我没有使用过LibGDX,但是我和Box2D一起工作并查看了API,我建议:

//Assuming only 1 fixture per body and a polygon shape


Array<Vector2>verts = new Array<Vector2>();
Fixture f = body.getFixtureList().get(0);
PolygonShape s = f.shape;
for (int i = 0; i < s.getVertexCount(); i++)
{
    verts.add(s.getVertex(i, /*I couldn't figure out what this param is supposed to be*/));
}

这是在没有IDE的情况下键入的,请注意明显的错误!我也没有在很长一段时间内完成Java。

答案 1 :(得分:1)

基于@James Webster的代码:

Array<Vector2> verts = new Array<Vector2>();
Fixture f = body.getFixtureList().get(0);
PolygonShape s = f.shape;

// this is needed to temporarily keep the vertex, getVertex is a void method
Vector2 tmp = new Vector2();
for (int i = 0; i < s.getVertexCount(); i++) {
    // fill tmp with the vertex
    s.getVertex(i, tmp));
    verts.add(new Vector2(tmp));
}