Search code examples
javalibgdxtexturesbox2dspritebatch

Rotating textureRegion based on box2D body angle (without sprite)


I am trying to rotate a textureRegion, based on box2d body angle, using the spriteBatcher draw method:

SpriteBatch.draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height,float scaleX, float scaleY, float rotation)

However the resulting rotation looks like this

enter image description here I can't figure out what I am doing wrong, have tried a lot of different combinations of originX, originY and it is always broken. Tried to setting the origins to center of body, left bottom corner, and no luck.

Looked at the documentation, analzyed the SpriteBatch.class but its unclear to me what's the reason.

How to rotate the texture properly?

EDIT: I cannot use Sprite class, because the textureRegion that is being drawn is an animation frame and afaik you can't make animation frames out of Sprite class


Solution

  • Set polygonshape of box2d body like this

    float width=x,height=y;
    
    PolygonShape polygonShape=new PolygonShape();
    polygonShape.setAsBox(width/2, height/2,new Vector2(width/2,height/2),0);
    

    setAsBox method of PolygonShape is overloaded.