问题
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