Search code examples
javalibgdxtexturesgame-physics

Rotate texture with Circlshape and Body


I'm trying to rotate a ball texture with the rotation of the body, but I can't figure out what am I doin wrong.

Here is my code for the draw method

    @Override
public void draw(Batch batch, float parentAlpha) {
    super.draw(batch, parentAlpha);
    sprite.setSize((Gdx.graphics.getWidth()/10), (Gdx.graphics.getWidth()/10));
    sprite.setPosition(body.getPosition().x - sprite.getWidth()/2, body.getPosition().y -sprite.getHeight()/2);
    sprite.setRotation(body.getAngle()* MathUtils.radiansToDegrees);
    sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
    sprite.draw(batch);
}

the body rotates, but the texture don't.


Solution

  • you try Sprite.rotate(YourRotation);

    but keep in mind that body.getAngle() returns the rotation that has the body with respect to its initial position, I think you'll have to adjust for use with rotate.

    for example, take body.getAngle() between frame and frame and the difference the previous frame is that you apply to sprite.rotate so when is 0 this does not move, use one method or another perhaps depends on whether you will be moving a lot of time or just once or I think. Hope it will help and I have explained well.

    Edit

    now that better read your question, sprite.setRotation(body.getAngle()* MathUtils.radiansToDegrees); would have to rotate somewhat, when you say no rotation, you mean that no rotation anything, or that rotation bad, could check you are receiving the data that you expect