Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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)

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
1  
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 –  Rudy_TM Feb 7 '12 at 15:56
    
@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. –  David Gouveia Feb 8 '12 at 10:17
    
@Rudy_TM can you add an answer and accept it for future readers (like me)? –  ashes999 Apr 22 '12 at 10:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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