我有以下代码,其中紧密映射的 Sprite 、矩形和多边形在 libgdx 中以相同的角度旋转。旋转后矩形与 Sprite 未对齐。虽然 Sprite 在绘制时会旋转,但旋转后坐标和尺寸保持不变。矩形不是这种情况。请查看下面的代码和结果图片。
public void rotate(int angle){
System.out.println("Before-recta x , y " + this.rectangle.getX() + " " + this.rectangle.getY() + " " + this.rectangle.getHeight() + " " + this.rectangle.getWidth());
System.out.println("Before-sprite x , y " + sprite.getX() + " " + sprite.getY()+ " " + this.sprite.getHeight() + " " + this.sprite.getWidth());
this.sprite.rotate(angle);
this.rectangle = null;
this.polygon.rotate(angle);
this.rectangle = this.polygon.getBoundingRectangle();
System.out.println("Afer-sprite x , y " + sprite.getX() + " " + sprite.getY() + " " + this.sprite.getHeight() + " " + this.sprite.getWidth());
System.out.println("Afer-recta x , y " + this.rectangle.getX() + " " + this.rectangle.getY() + " " + this.rectangle.getHeight() + " " + this.rectangle.getWidth());
}
最佳答案
如果您查看矩形的值,宽度约为 370,高度约为 345。宽度大于高度。因此,当您旋转它时,您会期望它的高度大于宽度。我们将进行一些快速匹配,看看我们得到的是否正确:
我们将从高度中删除宽度以获得两侧的额外空间量
370 - 346 = 24
现在我们将它除以 2,因为它平均分布在矩形的每一边
24 / 2 = 12
我们现在采用原始 x 位置 - 额外值的 1/2
268 - 12 = 256
还有y位置
739 + 12 = 751
这些与您获得的矩形 x 和 y 位置的值相匹配,因此我们知道这是正确的。
现在,如果我们对 Sprite 执行此操作,您将看到您获得的值不是针对旋转的 sprite。值之前和之后的 Sprite 是相同的。
原因很可能是 Sprite 的 x、y、w 和 h 永远不会改变,而是使用矩阵在渲染之前转换图像,使 Sprite 在其整个生命周期中都具有原始值。
关于android - 旋转后, Sprite 和矩形位置在 Libgdx 中未对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46601466/