【问题标题】:Align sprites and bounded rectangle after rotation in Libgdx在 Libgdx 中旋转后对齐精灵和有界矩形
【发布时间】:2017-10-10 08:09:58
【问题描述】:

我想一起旋转一组精灵、多边形和矩形。图 a) 显示了 2 个精灵,边界多边形和矩形。该组围绕大矩形的中心旋转 90 度(两个矩形的并集)。

以下代码创建图 b)。它不会围绕联合矩形的中心旋转精灵和多边形。

public void rotate(int rangle, float rotateX, float rotateY){
    this.sprite.rotate(rangle);
    this.polygon.rotate(rangle);

    Polygon tempPoly = new Polygon(new float[] {
            this.rectangle.x, this.rectangle.y,
            this.rectangle.x, this.rectangle.y + this.rectangle.height,
            this.rectangle.x + this.rectangle.width, this.rectangle.y + this.rectangle.height,
            this.rectangle.x + this.rectangle.width, this.rectangle.y
    });

    tempPoly.setOrigin(rotateX, rotateY);
    tempPoly.rotate(rangle);
    this.rectangle = null;
    this.rectangle = tempPoly.getBoundingRectangle();


}

以下代码创建图 c)。当我尝试将精灵和多边形移动到矩形位置时,存在未对齐的问题。

public void rotate(int rangle, float rotateX, float rotateY){

    this.sprite.rotate(rangle);
    this.polygon.rotate(rangle);

    Polygon tempPoly = new Polygon(new float[] {
            this.rectangle.x, this.rectangle.y,
            this.rectangle.x, this.rectangle.y + this.rectangle.height,
            this.rectangle.x + this.rectangle.width, this.rectangle.y + this.rectangle.height,
            this.rectangle.x + this.rectangle.width, this.rectangle.y
    });

    tempPoly.setOrigin(rotateX, rotateY);
    tempPoly.rotate(rangle);
    this.rectangle = null;
    this.rectangle = tempPoly.getBoundingRectangle();

    // This tries to move them to rectangle
    this.sprite.setPosition(this.rectangle.getX(), this.rectangle.getY());
    this.polygon.setPosition(this.rectangle.getX(), this.rectangle.getY());

}

问题是如何在旋转后将精灵与矩形对齐(即类似于图a,角度=0)。 提示:问题的出现是因为精灵在绘制时会旋转,因此精灵和矩形之间存在未对齐的情况。 (link)

【问题讨论】:

    标签: android opengl-es libgdx sprite


    【解决方案1】:

    我必须从多边形获取旋转位置并使用矩形计算偏移量。然后将该偏移量添加到精灵和多边形。

    public void rotate(int rangle, float rotateX, float rotateY){Rectangle pastRect = polygon.getBoundingRectangle();
    
    this.sprite.rotate(rangle);
    this.polygon.rotate(rangle);
    
    Polygon tempPoly = new Polygon(new float[] {
            this.rectangle.x, this.rectangle.y,
            this.rectangle.x, this.rectangle.y + this.rectangle.height,
            this.rectangle.x + this.rectangle.width, this.rectangle.y + this.rectangle.height,
            this.rectangle.x + this.rectangle.width, this.rectangle.y
    });
    
    tempPoly.setOrigin(rotateX, rotateY);
    tempPoly.rotate(rangle);
    Rectangle tempRect = new Polygon(polygon.getTransformedVertices()).getBoundingRectangle();
    this.rectangle = tempPoly.getBoundingRectangle();
    
    float x = sprite.getX() + (rectangle.x - tempRect.x);
    float y = sprite.getY() + (rectangle.y - tempRect.y);
    this.sprite.setPosition(x,y);
    this.polygon.setPosition(x,y);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-02
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多