Arash
Arash

Reputation: 12415

Actor origin in scene 2d

I don't seem to understand the actor origin in scene2d of Libgdx correctly.

My understanding is as follows:

If I setX of an actor to 0, its left edge is going to be rendered at corrdination 0. Now if I set its origin's X to 5, the fifth pixel of the actor is going to be at corrdination 0, in other words, the left edge of the actor is going to be at -5.

Some how my experience shows otherwise. If I set origin to 5, the left edge somehow is rendered on coordination 5.

Can someone please help me understand this issue.

Upvotes: 1

Views: 2192

Answers (1)

Robert P
Robert P

Reputation: 9783

the origin is used only for scaling and rotation.
That means, that if the origin is P(0,0) (the default), the Actor will be rotated and scaled arround his lower left corner.
If you instead set it's origin to the center (P(width/2, height/2)), he will rotate and scale arround his center.
The position instead is always relative to the lower, left corner and is not affected by the origin.
If you want to set the Actors position relative to his origin, you need to use the origin as offset:

actor.setX(newPosX - actor.getOriginX());
actor.setY(newPosY - actor.setOriginY());

Also remember, that setting an x-origin of 5, like in your example, does not need to set the Actors origin to it's 5th pixel, but to it's 5th unit.
If you use a Viewport, you don't need to calculate things in pixels, which is really important, as you can support different resolutions and aspect ratios.

Upvotes: 4

Related Questions