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

Peter Poliwoda picture Peter Poliwoda · Dec 17, 2012 · Viewed 16k times · Source

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.

Answer

aacotroneo picture aacotroneo · Dec 18, 2012

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);