2

我遇到了关于形状顶点的问题。当我将 bodyDef 的位置设置为 1 时,原点位于左上角 (0,0),1 bodyDef.position.set(position.x, position.y)身体通过应用矢量 (1,1) 对角线进行平移,但我不明白为什么.. 。 你能帮助我吗 ?

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

1 回答 1

2
    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);
}
于 2013-12-15T16:54:51.010 回答