I honestly have no idea why, but this code I’m using to create a Polygon Shape doesn’t seem to work. I’m trying to create a regular pentagon given the centre and radius. Circle and Box shapes work.
Here’s the code:
Circle, which works:
Body createCircle(float centreX, float centreY, float radius, BodyType bodyType) {
BodyDef bodyDef = new BodyDef();
bodyDef.type = bodyType;
bodyDef.position.set(centreX, centreY);
Body body = world.createBody(bodyDef);
CircleShape circle = new CircleShape();
circle.setRadius(radius);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = circle;
fixtureDef.density = 0.5f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 1f;
body.createFixture(fixtureDef);
circle.dispose();
return body;
}
Pentagon, doesn’t work:
Body createPentagon(float centreX, float centreY, float radius, BodyType bodyType) {
BodyDef bodyDef = new BodyDef();
bodyDef.type = bodyType;
bodyDef.position.set(centreX, centreY);
Body body = world.createBody(bodyDef);
PolygonShape shape = new PolygonShape();
Vector2[] vertices = new Vector2[5];
for(int i=0; i<=4; i++) {
vertices[i] = new Vector2(centreX + (float) (radius*Math.cos(i*72)), (float) (centreY + radius*Math.sin(i*72)));
}
shape.set(vertices);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 0.5f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 1f;
body.createFixture(fixtureDef);
shape.dispose();
return body;
}
Any ideas? Is it the maths I’m using wrong?