Search code examples
animationlibgdxtexture-atlas

Libgdx Animation using images from TextureAtlas gets incorrectly rotated


In my TextureAtlas the Sprite's for my Animation are rotated 90 degrees.

When I draw my Animation it's still rotaed by 90 degrees. How can I fix that?

My code looks like that:

    TextureAtlas spritesheet = new TextureAtlas(Gdx.files.internal("images/spritesheet/spritesheet.atlas"));
    Array<AtlasRegion> CLOUD_ANIMATION_REGIONS = spritesheet.findRegions("cloud_animation");
    Animation animation = new Animation(0.1f,ImageProvider.CLOUD_ANIMATION_REGIONS);

In the render method:

    batch.draw(animation.getKeyFrame(elapsedTime, true), x, y);

The animation works perfectly fien but it's rotated by 90 degree like in the spritesheet.

I realize that if I have a Sprite I can call Sprite.draw(batch) and it will fix the rotation but I don't seem to be able to use that mechanism for Animation's?

EDIT:

Like Alexander said, this will do the trick:

batch.draw(textureRegion, x, y, 0, 0,textureRegion.getRegionWidth(), textureRegion.getRegionHeight(), 1, 1, 90);

Solution

  • Ok, here is untested code:

    TextureRegion textureRegion = animation.getKeyFrame(elapsedTime, true);
    if (textureRegion instanceof TextureAtlas.AtlasRegion && ((TextureAtlas.AtlasRegion) textureRegion).rotate)
    {
        batch.draw(textureRegion, x, y, 0, 0, textureRegion.getRegionWidth(), textureRegion.getRegionHeight(), 1, 1, 90, true);
    }
    else
    {
        batch.draw(textureRegion, x, y);
    }
    

    What I'm doing here: I check if atlas packer marked the region as rotated and then draw it rotated 90 angle clockwise to compensate original 90 angle counter-clockwise rotation. See AtlasRegion's javadoc and special version of draw method that can rotate TextureRegion.

    EDITED: fix arguments based on Markus comment