1
\$\begingroup\$

we switched from frame by frame animations to Spine (this animation software).

The issue I am facing now, that it am unable to fit it in our coordinate system. (simple 2D game) I guess there is an issue with the projection or some other matrix-related stuff that I am not really good in. (It works perfectly in an empty project, Example provided here: https://github.com/EsotericSoftware/spine-runtimes/tree/3.7/spine-monogame It's probably a one-liner, but I cant figure it out. Target in this case I want to draw the tree at world position 100,100 - so the top left corner.

This is my sample class for spine objects:

public class SpineObject
    {
        private SkeletonRenderer skeletonRenderer;
        private Skeleton skeleton;
        private AnimationState state;
        private SkeletonBounds bounds = new SkeletonBounds();
        private string assetsFolder = "Content/Animations/";

        public SpineObject()
        {
            skeletonRenderer = new SkeletonRenderer(Orbs.graphics.GraphicsDevice);
            skeletonRenderer.PremultipliedAlpha = false;

            String name = "Objects/tree1/tree1";
            String atlasName = "Objects/tree1/tree1";

            Atlas atlas = new Atlas(assetsFolder + atlasName + ".atlas", new XnaTextureLoader(Orbs.graphics.GraphicsDevice));

            SkeletonJson json = new SkeletonJson(atlas);
            json.Scale = 1;
            SkeletonData skeletonData = json.ReadSkeletonData(assetsFolder + name + ".json");
            skeleton = new Skeleton(skeletonData);
            AnimationStateData stateData = new AnimationStateData(skeleton.Data);
            state = new AnimationState(stateData);
            state.SetAnimation(0, "animation", true);

            skeleton.X = 100;
            skeleton.Y = 100;
        }

        public void Update(GameTime gameTime)
        {
            state.Update(gameTime.ElapsedGameTime.Milliseconds / 1000f);
            skeleton.UpdateWorldTransform();
            state.Apply(skeleton);
        }

        public void Draw(Matrix viewMatrix)
        {
            ((BasicEffect)skeletonRenderer.Effect).Projection = Matrix.CreateOrthographicOffCenter(0, Orbs.graphics.GraphicsDevice.Viewport.Width, 0, Orbs.graphics.GraphicsDevice.Viewport.Height, 1, 0);
           ((BasicEffect)skeletonRenderer.Effect).View = Orbs.camera.GetViewMatrix();

            skeletonRenderer.Begin();
            skeletonRenderer.Draw(skeleton);
            skeletonRenderer.End();
        }
    }

The Draw-Call looks like this:

[...]
Animations.SpineObject tree1 = new Animations.SpineObject();
Camera2D camera = new Camera2D(GraphicsDevice.Viewport);
Matrix viewMatrix  = camera.GetViewMatrix();
[...]

protected override void Draw(GameTime gameTime)
{
    spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, null, null, null, viewMatrix);
    //Draw Map Layers and Stuff
    tree1.Draw(viewMatrix);
    spriteBatch.End();
}

I guess the tree is beeing drawn somwhere far off, so how do I get the trees X/Y value to be on par with the worlds coordinates?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Just in case someone runs into the same issue, I've found a simple workaround: I moved the Draw()-call of the tree out of the current spriteBatch-call. Means after spriteBatch.End(). It works perfectly now, also drawing it on the correct world position. I did not get an answer yet from Spine if it is some kind of restriction that it needs to be drawn outside of the current spriteBatch or if I did something wrong.

SpineObject Draw call:

    public void Draw()
    {
        ((BasicEffect)skeletonRenderer.Effect).Projection = Matrix.CreateOrthographicOffCenter(0, Orbs.graphics.PreferredBackBufferWidth, Orbs.graphics.PreferredBackBufferHeight, 0, 1, 0);
        ((BasicEffect)skeletonRenderer.Effect).World = Orbs.camera.GetViewMatrix();

        skeletonRenderer.Begin();
        skeletonRenderer.Draw(skeleton);
        skeletonRenderer.End();
    }
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .