Skip to content Skip to sidebar Skip to footer

After Rotation, Sprite And Rectangle Position Misaligned In 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

Solution 1:

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

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

24 / 2 = 12

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

268 - 12 = 256

And for the y position too

739 + 12 = 751

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

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.

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.

Post a Comment for "After Rotation, Sprite And Rectangle Position Misaligned In Libgdx"