0

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);
Markus
  • 1,452
  • 2
  • 21
  • 47

2 Answers2

1

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

Alexander Mironov
  • 3,095
  • 26
  • 28
  • I assume the correct usage is: batch.draw(textureRegion, x, y, 0, 0,textureRegion.getRegionWidth(), textureRegion.getRegionHeight(), 0, 0, 90); , but still the image doesn't rotate, no matter what rotation I enter. – Markus Jan 22 '15 at 19:36
  • Do you use built-in [TexturePacker](https://github.com/libgdx/libgdx/wiki/Texture-packer)? – Alexander Mironov Jan 22 '15 at 19:45
  • 1
    Can you bring your .atlas file? May be export doesn't flag region as rotated. – Alexander Mironov Jan 22 '15 at 19:57
  • Oh thank you but I could fix it with your help. The final problem was that I had used 0 for scale x and scale y which made it invisible. Using 1 for the scalings did the job. Thanks again! – Markus Jan 22 '15 at 20:11
  • Oh, I see. Not at all. – Alexander Mironov Jan 22 '15 at 20:17
0

Somehow you should be using AtlasSprite I think. That carries out the unrotate in its constructor. You dont want to be rotating every frame - thats some overhead. Also the AtlasSprite should take care of trimmed regions in the atlas : something thats very important to maximise a single atlas texture. Alas it doesnt seem very easy to use as it seems one needs a seperate sprite for each frame which seems massive overhead.

RichieHH
  • 2,116
  • 4
  • 28
  • 30
  • If I remember I finally went with avoiding rotated textures in the atlas at all. I didn't know about AtlasSprite. I'll give it a look. Thanks for your comment. – Markus Sep 22 '15 at 06:00