In libgdx, can i add a sprite as children of another an set it with the relative coordinates of the father and not the stage?

Like in HTML when you add controls they are relative to its parent, but here i want it with sprites, i add the children in a Group class but the coordinates are absolute (relative to the stage)


I've never worked with libgdx and only briefly checked its documentation. I'm not aware if it has support for what you're asking out of the box, but even if it doesn't have, you can still emulate it yourself with a bit of work. I've written about a related topic before here. It's written for XNA but the concepts are universal, so hopefully it will give you a starting point if you really need to implement it yourself.

Basically you'd want to start by encapsulating the sprite class in a new class of your own, by adding a list to hold the children sprites, and optionally a link back to the parent sprite.

After that, I'm not going to go into much detail because I've already described the procedure in the other question, but basically it consists of creating a world matrix from each sprite's "relative" position/rotation/scale. To transform from "relative" to "absolute" you need to multiply that sprite's "relative" matrix by the parent's "absolute" matrix. This is done all the way to the top of the hierarchy, i.e. at the top level there's no difference between "relative" or "absolute".

Finally, after you have the "absolute" matrix calculated, you decompose it to get back the correct position/rotation/scale to set your sprite to.

You can probably also do it without matrices too but I've never figured the calculations out.

| improve this answer | |
  • 1
    \$\begingroup\$ Thanks for the comment, but i resolved it :P I use the Group class, which contains all the necessary to manipulate children :) thanks for the answer \$\endgroup\$ – Rudy_TM Feb 7 '12 at 15:56
  • \$\begingroup\$ @Rudy_TM You're right, I didn't see the Group class when I browsed the documentation, but it does seem to be a perfect fit. \$\endgroup\$ – David Gouveia Feb 8 '12 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.