Hi ! Today I've played around with box2d and i have manage to have a pretty cool result but not the fanciest i think. I read in box2d forum thath fixture could change their vertex posistion and this is allow also in Jbox2d. So I basically change fixture as skeleton moves and also when it is fillpped all the fixture is automatically flipped 🙂 . Here is the code ( i'm using bounding boxes but it should be the same ) :
//Creation of the player body
for(Slot s : skeleton.getSlots()){
if(s.getAttachment() instanceof BoundingBoxAttachment){
BoundingBoxAttachment attach = (BoundingBoxAttachment) s.getAttachment() ;
PolygonShape poly = new PolygonShape();
float[] vertices = Arrays.copyOf(attach.getVertices(), attach.getVertices().length);
attach.computeWorldVertices(0, 0, s.getBone(), vertices);
for(int i=0;i<vertices.length;i++){
vertices[i] = vertices[i]*GameScreen.WORLD_TO_BOX;
}
poly.set(vertices);
Fixture fixt = box.createFixture(poly, 0);
fixt.setUserData(attach.getName());
poly.dispose();
}
}
and in my AnimationUpdate
if(bodyMap.has(e)){
Body b = bodyMap.get(e).getBody();
float x = aComp.getSkeleton().getX();
float y = aComp.getSkeleton().getY();
for(Fixture f : b.getFixtureList()){
Object safe = f.getUserData();
if(safe instanceof String){
Slot findSlot = aComp.getSkeleton().findSlot(f.getUserData() == null ? "" : (String)f.getUserData());
if(findSlot != null){
BoundingBoxAttachment attach = (BoundingBoxAttachment) findSlot.getAttachment();
float[] vertices = new float[attach.getVertices().length];
attach.computeWorldVertices(x,y, findSlot.getBone(), vertices);
for(int i=0;i<vertices.length;i++){
vertices[i] = (vertices[i]*GameScreen.WORLD_TO_BOX) - b.getPosition().x;
i++;
vertices[i] = (vertices[i]*GameScreen.WORLD_TO_BOX) - b.getPosition().y;
}
((PolygonShape)f.getShape()).set(vertices);
}
}
}
}
I know this solution hasn't the highest performance ( even true in my game i didn't see any fps drop ) but it works 😉 . There are also a couple of problems i.e. some glitches due to heavy forces on the player body or fast speed of the animation that cause tunnelling .. but I have not found a solution yet . please tell me if your able to solve them .
Bye 🙂