I wrote a little subclass of Sprite to make it easier to draw my Sprites at the correct position using Box2D and LibGDX. You can find it on Bitbucket.
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.
Code example:
Body body = world.createBody(bodyDef);
Fixture fixture = body.createFixture(fixtureDef);
Box2DSprite box2DSprite = new Box2DSprite(someSpriteOrTextureYouHave);
body.setUserData(box2DSprite); // will draw on whole body
fixture.setUserData(box2DSprite); // will only draw on the Fixture
To render, simply do:
batch.begin();
// 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);
batch.end();
Here is a little example Screen:
example
NOTE: To use this, you also need my Box2DUtils class which is dependent on GeometryUtils.
[b]This is now part of [url=http://dermetfan.bplaced.net/libgdx-utils.php]libgdx-utils[/url][/b].
-
Box2DSprite (needs Box2DUtils)
[list][li]AnimatedBox2DSprite (needs AnimatedSprite)[/li]
[/list]