Clone wiki

libgdx-utils /

A subclass of Sprite that draws itself on Box2D bodies and fixtures.

Just create a Box2DSprite and put it in the user data of a body or fixture and it will draw positioned, sized and rotated according to what you put it in. If you use the user data already, you can set an accessor Function that will be used to get the Box2DSprite from the user data.

Body body = world.createBody(bodyDef);
Fixture fixture = body.createFixture(fixtureDef);
Box2DSprite box2DSprite = new Box2DSprite(Assets.testImage);
body.setUserData(box2DSprite); // will draw on whole body
fixture.setUserData(box2DSprite); // will only draw on the Fixture

To render, simply do this:

// renders all the Box2DSprites in the given Box2D World
Box2DSprite.draw(batch, world);
// renders just this Box2DSprite, but you have to give in the Body or Fixture to render on (this way, it doesn't have to be in the user data)
box2DSprite.draw(batch, body);
box2DSprite.draw(batch, fixture);

There are many other options, for example you can use your own origin, offset and enable or disable the width or height adjustment.

Here is a little example Screen.