旋转后,Libgdx中的精灵和矩形位置未对齐 [英] After rotation, sprite and rectangle position misaligned in Libgdx

查看:65
本文介绍了旋转后,Libgdx中的精灵和矩形位置未对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,其中紧密映射的精灵,矩形和多边形在libgdx中以相同角度旋转.旋转后,矩形未与精灵对齐.尽管精灵在绘制时会旋转,但旋转后的坐标和尺寸保持不变.矩形不是这种情况.请参见下面的代码和结果图像.

I have following code, where closely mapped sprite, rectangle and polygon is been rotated at same angle in libgdx. After rotation rectangle is misaligned with the sprite. Although sprite is rotated when its drawn, the coordinates and dimensions stay same after the rotation. This is not the case for rectangle. Please see the code and results image below.

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.这比更高的宽度还要宽.因此,当您旋转此对象时,您会期望它会更高而不会更宽.我们将进行一些快速匹配,以查看我们得到的内容是否正确:

If you look at the values for your rectangle the width is about 370 and the height is about 345. This is wider than it is taller. So when you rotate this you would expect it to be taller than it is wider. We'll do some quick match to see if what we're getting is correct:

我们将从高度上删除宽度,以获取两侧的额外空间

We'll remove the width from the height to get the amount of extra space for both sides

370-346 = 24

370 - 346 = 24

现在,我们将其除以2,因为它平均分布在矩形的每一侧

Now we'll divide that by 2 as it's equally distributed to each side of the rectangle

24/2 = 12

24 / 2 = 12

我们现在将原来的x位置-额外值的1/2

we now take the original x position - 1/2 the extra value

268-12 = 256

268 - 12 = 256

还有y位置

739 + 12 = 751

739 + 12 = 751

这些与您为矩形的x和y位置获取的值匹配,因此我们知道这是正确的.

These match the values you are getting for x and y position for the rectangle so we know this is correct.

现在,如果我们对Sprite执行此操作,您将看到所获得的值不适用于旋转的Sprite.值前后的精灵相同.

Now, if we do this for the Sprite you will see the value you are getting are not for the rotated sprite. The sprites before and after values are the same.

这样做的原因很可能是精灵的x,y,w和h永远不会改变,而是在渲染之前使用矩阵对图像进行了变换,从而使精灵在整个生命周期中都保持原始值.

The reason for this is most likely that the sprite's x,y,w and h never change and instead a matrix is used to transform the image before rendering leaving the sprite with the original values throughout its lifetime.

这篇关于旋转后,Libgdx中的精灵和矩形位置未对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆