Hi,
we switched from frame by frame animations to Spine. Our artist is very pleased to say at least 😉
The issue I - as the programmer - 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)
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?
I just figured out, that if I call tree1.Draw() after the spriteBatch.End(); it works fine
I've also changed the DrawCall to
((BasicEffect)skeletonRenderer.Effect).Projection = Matrix.CreateOrthographicOffCenter(0, Orbs.graphics.PreferredBackBufferWidth, Orbs.graphics.PreferredBackBufferHeight, 0, 1, 0);
((BasicEffect)skeletonRenderer.Effect).World = Orbs.camera.GetViewMatrix();
Is this a limitation or is there an other way to do it?
I also need to use skeleton.ScaleY = -1; because my tree is flipped on the y-Axis?! (skeleton.FlipY is deprecated)
Any Ideas why?