Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate an image actor in libgdx by the middle point?

Tags:

If I understand correctly, LibGDX is rotating an image with the use of the addActions method:

this.addAction( parallel(rotateBy(360, 0.5f), moveTo(320, 100, 0.5f)));

The thing is, it is being rotated by the point=(0,0) of the image.

Here's my question:

Is there a way to rotate an image by the center point of the object? Something like pinning it in the middle and then turning it like a wheel in a car? Both rotateBy and rotateTo methods turn it by the (0,0) point of the image itself.

like image 479
Peter Poliwoda Avatar asked Dec 17 '12 19:12

Peter Poliwoda


2 Answers

You have to properly set the "origin" of you actor. You can read from the Actor API that the origin is relative to the position and is used for scale and rotation.

So, calculate your middle point, and set origin to middle point.

Actor a;
....
a.setOrigin(a.getWidth()/2, a.getHeight()/2);
like image 99
aacotroneo Avatar answered Sep 29 '22 13:09

aacotroneo


The easiest and shortest way I found for setting the origin is to call:

Actor a;

...

a.setOrigin(Align.center);
like image 45
Amit Kotlovski Avatar answered Sep 29 '22 15:09

Amit Kotlovski