Skip to content Skip to sidebar Skip to footer

Libgx Rotate A Texture Region

I wonder know how to Rotate a textureRegion in Libgdx. If there is a isue with Pixmap, you can also give it to me. I don't want to use sprites because it's not appropiate for the t

Solution 1:

It is actually pretty simple to rotate a textureRegion with multiples of 90°.

A textureRegion is a portion of a bigger Texture, defined with two couples of UV coordinates. In order to rotate your textureRegion, you juste have to rotates these UV coordinates.

For instance, here is you original textureRegion:Original region

(we're assuming that your region is a square, but it would work with a rectangle as well) To rotate 90 clockwise, you need just have to swap coordinates:

u1 = u1
v1 = v2
u2 = u2
v2 = v1

Which will lead to:

Rotated region

You can generalize this solution with transformation matrix (rotation, translation, scaling), but for 90° multiple rotation, you just need to swap coordinates.

EDIT: This page contains all you need you know to change UV coordinates of a textureRegion

Solution 2:

for rotating at an angle say theta (symbol @) at origin

let (x,y) be old values and

(xn,yn) be new values then

clockwise

xn=x*cos(@)+ y*sin(@)

yn=y*sin(@)-x*cos(@)

counter clockwise

xn=x*cos(@)+ y*sin(@)

yn=y*sin(@)-x*cos(@)

for along a point refer to this the diagram helps How to rotate a vertex around a certain point?

for example a rectangle just calculate the 2 vertex position(x1,y1) and (x2,y2)

then draw as follows width=x2-x1; height=y2-y1;

g.drawRectangle(x,y,width,heigh);

been a while since I revised my C.G. notes but i guess above will do the trick

if using libgdx here is simple solution: Spritebatch.draw(region, 0, 0, 128, 128, 256, 256, 1, 1, 90); last parameter is rotation angle

Post a Comment for "Libgx Rotate A Texture Region"