多边形无法正确旋转

时间:2016-02-18 13:00:29

标签: java libgdx rotation polygon

我正在制作一个游戏,我需要针对不同的角色进行碰撞。问题是,当我尝试旋转角色的多边形时,多边形开始在整个屏幕上移动。当它仅在0度旋转时,它工作正常,但其他任何东西都会离开角色的位置。基本上,有没有比使用polygon.setRotation()更好的旋转方式,或者我只是做错了?这是代码:

public Polygon getPoly() {
    Polygon poly = new Polygon(new float[] {
            position.x - (width / 2), position.y + (width / 2),
            position.x + (width / 2), position.y + (width / 2),
            position.x + (width / 2), position.y - (width / 2),
            position.x - (width / 2), position.y - (width / 2)
    });

    poly.setRotation(rotation);
    poly.translate(width / 2, width / 2);

    return new Polygon(poly.getTransformedVertices());
}

1 个答案:

答案 0 :(得分:2)

我刚想通了,我必须做poly.setOrigin(position.x, position.y)来设置相对于角色的旋转。如果有人需要,完整的代码就在这里:

public Polygon getPoly() {
    Polygon poly = new Polygon(new float[] {
            position.x - (width / 2), position.y + (width / 2),
            position.x + (width / 2), position.y + (width / 2),
            position.x + (width / 2), position.y - (width / 2),
            position.x - (width / 2), position.y - (width / 2)
    });

    poly.setOrigin(position.x, position.y);
    poly.setRotation(getRotation());
    poly.translate(width / 2, width / 2);

    return new Polygon(poly.getTransformedVertices());
}