3
votes

Hi I am creating a game for android using the Libgdx framework. I am trying to place a sprite in the middle of the X axis near the top of my screen. I can do this just fine but since the sprite needs to be rotated 90 degrees(to save room on spritesheet) it causes some sort of offset and I don't understand why. I believe it is because of the origin rotation but I have tried setting the origin to the center of the sprite but it still doesn't work as planned. Please can you help me understand why the rotation is causing an offset and how it can be fixed thank you.

    regions [1] = new TextureRegion(menu, 395, 0, 115, 320);
    logo=new Sprite(regions[1]);// the sprite
    logo.setPosition(W/2-logo.getWidth()/2,H*0.95f);  //draw right in the middle of X and 95% up Y
    logo.rotate(90);                                  //rotate the sprite 90 degrees
    logo.setOrigin(logo.getWidth()/2,logo.getHeight()/2);  //rotate around the centre of the sprite

    logo.draw(spriteBatch); //in render method draw the sprite
1
If I change logo.setPosition(W/2-logo.getWidth()/2,H*0.95f); to logo.setPosition(W/2-logo.getWidth()/2,0); it positions the sprite nearly half way up the y axis rather than at the bottom for some reason.user2002077

1 Answers

2
votes

You set the origin of the sprite after you rotate it:

logo.rotate(90);                                  //rotate the sprite 90 degrees
logo.setOrigin(logo.getWidth()/2,logo.getHeight()/2);  //rotate around the centre of the sprite

you should do it the other way round. First set origin, then rotate:

logo.setOrigin(logo.getWidth()/2,logo.getHeight()/2); 
logo.rotate(90);