1

i don't know what im doing wrong...i think im having brain freeze. I am really struggling with converting my spine objects pixel coordinates to world coordinates. I have recently converted all my code to work with Ashley ecs and i cant seem to get my spine object to display in the correct position. i have a system which handles the rendering and positioning of my spine object but i cant seem to get it displaying in the correct position. I'm hoping someone can point me in the correct direction!

i have included my code for the spine rendering system...hope you can help! i want to place the spine object at the same position as my box 2d object which is using world coordinates. but spine is using pixel coordinates. i have also included an image to show you what is happening. (the grey square near the middle right of the screen is where i want my spine object to be!)

Amarino

in game image

    public class SpineRenderSystem extends IteratingSystem {
    private static final String TAG = com.chaingang.freshstart.systems.SpineRenderSystem.class.getName();

    private PolygonSpriteBatch pBatch;
    SkeletonMeshRenderer skeletonMeshRenderer;
    private boolean process = true;

    BodyComponent bodyComp;
    Spine2DComponent spineComp;

    public SpineRenderSystem(PolygonSpriteBatch pBatch){
        super(Family.all(RenderableComponent.class, Spine2DComponent.class, PositionComponent.class).get());
        this.pBatch = pBatch;
        skeletonMeshRenderer = new SkeletonMeshRenderer();
        skeletonMeshRenderer.setPremultipliedAlpha(true);

    }


    @Override
    protected void processEntity(Entity entity, float deltaTime) {

        bodyComp = Mappers.body.get(entity);
        spineComp = Mappers.spine2D.get(entity);

        float offsetX = 100.00f/Gdx.graphics.getWidth(); //100 equal world width
        float offsetY = 50.00f/Gdx.graphics.getHeight(); //50 equals world height

        pBatch.begin();
            spineComp.skeleton.setX((bodyComp.body.getPosition().x / offsetX) );
            spineComp.skeleton.setY((bodyComp.body.getPosition().y) / offsetY);

            skeletonMeshRenderer.draw(pBatch,spineComp.skeleton);
            //spineComp.get(entity).skeleton.setFlipX(player.dir == -1);
            spineComp.animationState.apply(spineComp.skeleton);
            spineComp.skeleton.updateWorldTransform();
        pBatch.end();


    }
}
Amar_H
  • 47
  • 11

2 Answers2

1

What I do for my spine renders is look at the bounding box size in pixels in spine. This is usually in the order of 100s of pixels. But if you are working with box2d scales, it is recommended that you think of 1 as 1 meter.

With this in mind I will scale a human spine animation with a hip y coordinate of 200 pixels by dividing it by 200, or there about.

Once you have this ratio, then when you build your Spine Skeleton you can do this (sorry, I do all my libgdx stuff in Kotlin now):

val atlasLoader = AtlasAttachmentLoader(atlas)
val skeletonJson = SkeletonJson(atlasLoader)
skeletonJson.scale = 1/200f

Then you might also want to handle an offset for rendering your spine object, as I see you are trying to do, because possibly your root bone is in the center of your spine object (a hip for example). However, you are doing a division operation, which I guess is exploratory as offsets should be an addition or subtraction. Here is how I do it using the spine pixel coordinates (again, sorry for the Kotlin, but I like it):

//In some object or global state we have this stuff
var skeleton: Skeleton
var skeletonRenderer = SkeletonRenderer<PolygonSpriteBatch>()


//Then in the rendering code
val offset = Vector2(0f,-200f)
val position = physicsRoot.position().add(offset)
skeleton.setPosition(position.x, position.y)
skeleton.updateWorldTransform()
skeletonRenderer.draw(batch, skeleton)

That should get your spine stuff working as you expect.

Laurence
  • 1,556
  • 10
  • 13
0

Have you heard of a method called camera.project(world coordinates); it might do what you are looking for. It takes the world coordinates and turns them into screen coordinates. For the opposite you can do camera.unproject(screen coordinates);

Lucas B
  • 338
  • 4
  • 14
  • I think that kind of works but what about when you are resizing the screen...it doesn't seem to work correctly then...do I need to reset some width and height values associated with camera and and then recall the project and unproject functions? – Amar_H Sep 02 '17 at 23:20
  • Do you update your camera? In resize method do camera.viewportwidth =width and camera.viewportheight=height and then call camera.update() – Lucas B Sep 02 '17 at 23:44
  • i have tried that...i think i need to re look at my code which handles the extended viewport and camera...i think i have a few issues there which i need to resolve... – Amar_H Sep 06 '17 at 22:57
  • when i resize the screen the screen, it looks to be working correctly at a particular resolution but when i manually adjust the size of the window it doesnt work correctly...so imn guessing i have a problem with the camera..also when i zoom in and out the spine model doesnt change size as you would expect it to do. The level and any sprites i have showing seem to scale correctly. i got spine model moving at a particular resolution moving correctly by using camera.project – Amar_H Sep 06 '17 at 22:59
  • ok i just figured out it works correctly with whatever resolution i set in the config.width and config.height in the desktop launcher...also when i adjust the zoom on the camera it still doesnt zoom the spine model. – Amar_H Sep 06 '17 at 23:09
  • Did you set the batch projection matrix to the camera? The batch which you are using to draw. – Lucas B Sep 06 '17 at 23:40
  • Yes I do...I will go through my code again just in case I have left some extraneous test code still active – Amar_H Sep 07 '17 at 00:13
  • And are you updating the viewport in the resize method? – Lucas B Sep 07 '17 at 00:26
  • got it working...i had a few extra lines of test code and a few extra cameras i handnt deleted...my soloution was to only use one sprite batch and 2 cameras...one camera for the GUI and one camera for the main gamescreen – Amar_H Sep 08 '17 at 16:43