I got an issues concerning the vertices of my shape.
The origin is at the top left corner (0,0) when I set the position of the bodyDef at 1, 1 bodyDef.position.set(position.x, position.y)
the body do a translation diagonally by applying a vector (1,1) and I don't get it why ... can you help me ?
public class Player {
private Body body;
public Player(World world, Vec2 position) {
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(position.x, position.y);
bodyDef.type = BodyType.DYNAMIC;
PolygonShape shapeShip = new PolygonShape();
Vec2[] verticesShip = {
new Vec2(0.0f, -10.0f),
new Vec2(10.0f, 10.0f),
new Vec2(-10.0f, 10.0f)
};
shapeShip.set(verticesShip, verticesShip.length);
FixtureDef fixtureDefShip = new FixtureDef();
fixtureDefShip.shape = shapeShip;
fixtureDefShip.density = 0.5f;
fixtureDefShip.friction = 0.3f;
fixtureDefShip.restitution = 0.5f;
body = world.createBody(bodyDef);
body.createFixture(fixtureDefShip);
}
public void draw(Graphics2D graphics, int width, int height) {
PolygonShape polygonShape = (PolygonShape) body.getFixtureList().getShape();
Vec2[] vertices = polygonShape.getVertices();
for(int i = 0; i < polygonShape.getVertexCount(); i++) {
Vec2 vertice = vertices[i];
vertices[i] = body.getWorldPoint(vertice);
System.out.println(body.getWorldCenter());
}
DrawShape.drawPolygon(graphics, polygonShape.getVertices(), polygonShape.getVertexCount(), Color.WHITE);
}
public void forward() {
Vec2 force = new Vec2(0.0f, 10.0f);
Vec2 point = body.getPosition();
body.applyForce(force, point);
}
public void rotateLeft() {
float angle = (float) Math.toDegrees(body.getAngle()) % 360;
body.setTransform(body.getPosition(), (float) Math.toRadians(--angle));
}
public void rotateRight() {
float angle = (float) Math.toDegrees(body.getAngle()) % 360;
body.setTransform(body.getPosition(), (float) Math.toRadians(++angle));
}
public Body getBody() {
return body;
}
}
public void draw(Graphics2D graphics, int width, int height) {
PolygonShape polygonShape = (PolygonShape) body.getFixtureList().getShape();
Vec2[] vertices = polygonShape.getVertices();
Vec2[] verticesTransform = new Vec2[polygonShape.getVertexCount()];
for(int i = 0; i < polygonShape.getVertexCount(); i++) {
verticesTransform[i] = body.getWorldPoint(vertices[i]);
System.out.println(verticesTransform[i]);
}
DrawShape.drawPolygon(graphics, verticesTransform, verticesTransform.length, Color.WHITE);
}