java jbox2D PolygonShape设置位置

时间:2013-12-02 10:53:34

标签: java position shape jbox2d

我开发了jbox2d(java),我在创建多边形时遇到了问题。

我无法选择多边形的位置,有一个字段“m_centroid”我定义为我的窗口的中心。多边形保留在左角。

public class Player {
    private Body body;

    public Player(World world) {
        BodyDef bodyDef = new BodyDef();
        bodyDef.angle = (float) Math.toRadians(0.0f);
        bodyDef.position.set(800.0f / 2, 600.0f / 2);
        bodyDef.type = BodyType.DYNAMIC;

        /*Vec2[] vertices = {
            new Vec2(bodyDef.position.x, bodyDef.position.y - 10.0f),
            new Vec2(bodyDef.position.x + 10.0f, bodyDef.position.y + 10.0f),
            new Vec2(bodyDef.position.x - 10.0f, bodyDef.position.y + 10.0f)
        };*/

        Vec2[] vertices = {
                new Vec2(0.0f, - 10.0f),
                new Vec2(+ 10.0f, + 10.0f),
                new Vec2(- 10.0f, + 10.0f)
        };

        PolygonShape shape = new PolygonShape();
        shape.set(vertices, vertices.length);
        shape.m_centroid.set(bodyDef.position);

        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = shape;
        fixtureDef.density = 0.5f;
        fixtureDef.friction = 0.3f;
        fixtureDef.restitution = 0.5f;

        body = world.createBody(bodyDef);
        body.createFixture(fixtureDef);
    }
}

1 个答案:

答案 0 :(得分:0)

质心应该是局部坐标中多边形的中心,用于扭矩/质量计算。你不应该修改它。相反,您需要在Body上使用变换。尝试类似body.getTransform().setPos(___)的内容。