Search code examples
rotationlibgdxspritebatch

LibGDX - Rotate SpriteBatch on itself


enter image description here

I would like to rotate this SpriteBatch on itself upon clicking on a button

@Override
public void render() {

    SpriteBatch batch = new SpriteBatch();

    batch.begin();
    batch.draw(gemTexture, 10, 10, 100, 100);
    batch.end();

    if (Gdx.input.isTouched()) {
        rotateRight();
    }

}

private void rotateRight() {
// How do I rotate it to look like 
}

enter image description here


Solution

  • You're drawing a Texture using a SpriteBatch. A Texture doesn't support rotation. I suggest that the Sprite class might be better suited for what you are trying to do. Here's a rough outline of what you might do... see the Sprite javadoc for more detail.

    private void createGemSprite() {
        gemSprite = new Sprite(gemTexture);
        gemSprite.setPosition(10, 10);
    }
    
    @Override
    public void render() {
    
        SpriteBatch batch = new SpriteBatch();
    
        batch.begin();
        gemSprite.draw(batch);
        batch.end();
    
        if (Gdx.input.isTouched()) {
            rotateRight();
        }
    }
    
    private void rotateRight() {
        gemSprite.setRotation(gemSprite.getRotation() - 90);
    }