Search code examples
javalibgdximage-rotation

How to rotate a Sprite around the circle in LibGDX?


I want to rota a sprite around a circle, but I couldn't do that. If I could not tell my problem you can understand from this picture:

square rotating around circle


Solution

  • Sprite has a method setOrigin(), https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Sprite.html#setOrigin-float-float-

    Set Sprite's origin at the center of your circle and rotate it. Code will look similar to this:

    float circleCenterX = ...;
    float circleCenterY = ...;
    float angle = ...;
    Sprite sprite = ...;
    
    sprite.setOrigin(circleCenterX, circleCenterY);
    sprite.rotate(angle); // or sprite.setRotation(angle);