【问题标题】:Polygon doesn't rotate correctly多边形无法正确旋转
【发布时间】:2016-02-18 13:00:28
【问题描述】:

我正在制作一款游戏,我需要为不同的角色解决碰撞问题。问题是,当我尝试旋转角色的多边形时,多边形开始在整个屏幕上移动。当它仅在 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());
}

【问题讨论】:

    标签: java libgdx rotation polygon


    【解决方案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());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      相关资源
      最近更新 更多