How to use animation offset in libgdx from texturepacker

While trying some new things after working all days, I encountered something annoying .. when I used texturepacker gui from codeandweb, the packer packs my image into one and use offset to maximize its compression.

This kind of packing is actually good, but it’s a little annoying because we must set the offset in libgdx to actually make the animation position line-up.

Packed by texturepacker
Packed by texturepacker

I browsed through google to find how to actually get the offset and apply it to the animation function in libgdx, but luck not in my side for a whole day … ah, as reminder I used this code to apply rotation from the left bug, because if you used the sprite as it is, you will get the same as the sprite above for animation.

The code above will make your animation looks just like this ..

 

Animation before using offset
Animation before using offset

After a whole day searching with no actual result, I find some method to get the offset and apply it to the animation..

The code is really simple, when you see the offset in the .atlas / .pack file it was actually means that the size + (offset * 2) + 2 = original sprite size before packed.

for example this line

means that, the image is rotated, the xy is the border, the size is the compressed packed size ( height + width ), the orig is the original size ( height + width ), and offset is the difference between size and orig …

It was calculate just like that, so to actually position so the animation were aligned perfectly you need to give additional code like below

After adding offset to animation
After adding offset to animation

I hope this helped newbie like me who use libgdx animation that use offset and texturepacker … if you have something to correct, feel free to use the comment box, cheers 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *