As my prototype works perfectly fine I want to advance to more fun areas of the project and add a more complex player-character body.
So for Box2D I figured I'd need multiple fixtures, like one for the legs, one for the torso and one for the head.
Each of these fixtures will get a different sprite/texture that should be swappable dynamically if the player puts on some armour or changes cloths etc.
Until now the player-character consisted of a Box2D Body with just one single fixture. I added the sprite like this:
body.setUserData(this.sprite); I rendered it like this:
// Draw-method if(body.getUserData() != null && body.getUserData() instanceof Sprite) { Sprite sprite = (Sprite) body.getUserData(); sprite.setPosition(body.getPosition().x - sprite.getWidth() / 2, body.getPosition().y - sprite.getHeight() / 2); sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees); sprite.draw(spriteBatch); } Now that I want to have multiple sprites for different "regions" of the body I figured it's better to add them to the fixture itself. For now I just wanted to reproduce what I already had, so just the one sprite I have to the one fixture I have right now instead of the body.
I do this like that:
Fixture fixture = body.createFixture(fixtureDef); My problem arises when trying to render this.
for(Fixture fixture : body.getFixtureList()) { if(fixture.getUserData() != null && fixture.getUserData() instanceof Sprite) { Sprite sprite = (Sprite) fixture.getUserData(); //sprite.setPosition(fixture.getShape().getPosition() ... // Not gonna work, because PolygonShape has no position! :( sprite.draw(spriteBatch); } } As you can read in the comment, the fixture for now has a rectangular shape which is made like:
shape = new PolygonShape(); shape.setAsBox(widthInMeters / 2, heightInMeters / 2); The problem is, that a PolygonShape has not position :/ Now when I render this, the sprite is not being drawn at the fixtures position and currently I really have no idea how to fix that.
I'd highly appreciate any help you can give me! Thanks in advance and have a wonderful day! :)