有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java多边形旋转不正确

我正在做一个游戏,我需要计算出不同角色的碰撞。问题是,当我尝试旋转角色的多边形时,多边形开始在整个屏幕上移动。当它仅处于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) 个答案

  1. # 1 楼答案

    我刚想出来,我必须做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());
    }