Search code examples
rotationlibgdx

Trouble with LibGDX resetting origin on an already rotated actor


If I do:

actor.setOrigin(0, 0);
actor.setRotation(45);
actor.setOrigin(actor.getWidth() / 2, actor.getHeight() / 2);

It appears that on the last setOrigin call, the actor gets repositioned to the location it would've been if actor.setRotation(45) would have been called after its latest origin was set.

What do I do to make it so that the latest origin of the actor is only used for future "scale" and "rotation" actions?


Solution

  • Okay so i looked in the source code of libgdx, and i'll tell you the short answer.

    Basically when you set the origin or the rotation, you just change a variable named "originx", "originy" and "rotation". So every call to setOrigin will overwrite the values set in previous calls.

    And every time you draw the actor, it recalculates the bounds using the current variable.

    To be clear, setOrigin looks like this :

    public void setOrigin (float originX, float originY) {
        this.originX = originX;
        this.originY = originY;
    }
    

    So the precedent setOrigin is lost.