- Editado
Libgdx + Spine
I got an error when implementing box2d + spine + libgdx.
Image removed due to the lack of support for HTTPS. | Show Anyway
the bodies aren't in the right spot.
helpme.
Below is the code I'm using:
public PlayerEntity(World world,final TextureAtlas textatlas, Vector2 position){
this.world=world;
this.textatlas = textatlas;
AtlasAttachmentLoader atlasLoader = new AtlasAttachmentLoader(textatlas) {
public RegionAttachment newRegionAttachment (Skin skin, String name, String path) {
Box2dAttachment attachment = new Box2dAttachment(name);
TextureAtlas.AtlasRegion region = textatlas.findRegion(attachment.getName());
if (region == null) throw new RuntimeException("Region not found in atlas: " + attachment);
attachment.setRegion(region);
return attachment;
}
};
SkeletonJson json = new SkeletonJson(atlasLoader);
json.setScale(.005f);
SkeletonData data = json.readSkeletonData(Gdx.files.internal("animations/skeleton.json"));
skeleton = new Skeleton(data);
skeleton.setPosition(3, 1.3f);
skeleton.updateWorldTransform();
AnimationStateData stateData = new AnimationStateData(data);
animationState = new AnimationState(stateData);
skeletonRenderer = new SkeletonRenderer();
debugRenderer = new SkeletonRendererDebug();
animationState.setAnimation(0, "running", true);
for (Slot slot : skeleton.getSlots()) {
if (!(slot.getAttachment() instanceof Box2dAttachment)) continue;
Box2dAttachment attachment = (Box2dAttachment)slot.getAttachment();
PolygonShape boxPoly = new PolygonShape();
boxPoly.setAsBox(attachment.getWidth() / 2 * attachment.getScaleX(),
attachment.getHeight() / 2 * attachment.getScaleY(), vector.set(attachment.getX(), attachment.getY()),
attachment.getRotation() * MathUtils.degRad);
BodyDef boxBodyDef = new BodyDef();
boxBodyDef.type = BodyDef.BodyType.StaticBody;
attachment.body = world.createBody(boxBodyDef);
attachment.body.createFixture(boxPoly, 1);
boxPoly.dispose();
}
}
static class Box2dAttachment extends RegionAttachment {
Body body;
public Box2dAttachment (String name) {
super(name);
}
}
@Override
public void draw(Batch batch, float parentAlpha) {
batch.end();
shapeRenderer = debugRenderer.getShapeRenderer();
shapeRenderer.getProjectionMatrix().set(batch.getProjectionMatrix());
shapeRenderer.getTransformMatrix().set(batch.getTransformMatrix());
debugRenderer.setScale(.005f);
debugRenderer.draw(skeleton);
batch.begin();
skeletonRenderer.draw(batch, skeleton);
for (Slot slot : skeleton.getSlots()) {
if (!(slot.getAttachment() instanceof Box2dAttachment)) continue;
Box2dAttachment attachment = (Box2dAttachment)slot.getAttachment();
if (attachment.body == null) continue;
float x = skeleton.getX() + slot.getBone().getWorldX();
float y = skeleton.getY() + slot.getBone().getWorldY();
float rotation = slot.getBone().getWorldRotationX();
attachment.body.setTransform(x, y, rotation * MathUtils.degRad);
}
;
super.draw(batch, parentAlpha);
}
@Override
public void act(float delta) {
animationState.update(delta);
animationState.apply(skeleton);
skeleton.updateWorldTransform();
super.act(delta);
}
Hard to say what is going on. Have you tried debugging it? Try a simpler skeleton to narrow down the problem? Note you won't be able to use nonuniform scale or the shear tool since that kind of transform can't be described by setAsBox
.