Get vertice list of a polygon shape body

筅森魡賤 提交于 2019-12-21 23:52:49

问题


How do you get a list of vertices for a specific polygon body in libgdx?

Like this :

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

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

    return verts;
}

Thanks!


回答1:


Based on @James Webster's code:

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



回答2:


I haven't worked with LibGDX, but I have worked with Box2D and looking at the API, I'd suggest:

//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*/));
}

This was typed without an IDE, watch out for blatant errors! I haven't done Java in a long time either.



来源:https://stackoverflow.com/questions/20098779/get-vertice-list-of-a-polygon-shape-body

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!