[SOLVED] Anchor/Pivot points

Hello!
I’ m writing a small game demo, using Monogame.Extended libraries.
I wonder, if pivot data is exported from jsonMap, I was looking for export entries in source code, but couldn’t find. :hushed:
For example: I have TextureAtlas & Map created via TexturePacker/SpriteSheetPacker, something like this:

{frames: [
{
    "filename": "Frame1.png",
    "frame": {"x":823,"y":612,"w":76,"h":97},
    "rotated": false,
    "trimmed": true,
    "spriteSourceSize": {"x:":3,"y":0,"w":76,"h":97},
    "sourceSize": {"w":80,"h":97},
    "pivot": {"x":0.523697,"y":0.345168}
},
{
    "filename": "Frame2.png",
    "frame": {"x":823,"y":711,"w":76,"h":96},
    "rotated": false,
    "trimmed": true,
    "spriteSourceSize": {"x:":3,"y":1,"w":76,"h":96},
    "sourceSize": {"w":80,"h":97},
    "pivot": {"x":0.603516,"y":0.411546}
}

//C#
tAtlas = Content.Load<TextureAtlas>(@"textureMap");
SpriteSheetAnimationFactory spaFactory = new SpriteSheetAnimationFactory(tAtlas);
spaFactory.Add("Idle", new SpriteSheetAnimationData(array(0-10), 0.2f));
spaFactory.Add("Attack1", new SpriteSheetAnimationData(array(11-20), 0.04f, false));
spaFactory.Add("Attack2", new SpriteSheetAnimationData(array(21-30), 0.04f, false));
animSprite= new AnimatedSprite(spaFactory, "Idle");

But actually it doesn’t work (anchoring) :disappointed_relieved:. Seems like all data is imported except pivot data, which is essential to me, because I’m using SpriteSheet with different-sized frames.
Am I missing something or do I need to create separate wrapper or processor which will read this data??

To illustrate, what I mean: SpriteOrigin
Yellow border is frame size & red square is origin.
The best way it works, when I place origin in center-bottom position, not perfectly though. :neutral_face:

Will appreciate any advice :pensive:

Sorry to say, it looks like we don’t currently support this.

It appears that we do read the pivot point from the file but by the time we get to the writer we’ve dropped it.

Basically, you’ve got 3 options:

  1. You could grab the source code and try to implement this yourself and submit a pull request.
  2. You can raise the issue on github and at some point we’ll get around to fixing it.
  3. You’re welcome to take the bits of code you need and put it directly into your own project.

I realize that this is not the answer you were hoping to hear. We’re actively working on the next major version (2.0) of MonoGame.Extended. It’ll take some time but we’re always listening to feedback like this to make the library better.

If you don’t do anything else, I’d really appreciate it if you do raise the issue on github. Even if it’s just a copy and paste of the question here. It’ll help you stay in the loop as we discuss and progress the issue and it’ll help us if you’re involved in the discussion.

Thank you for your reply :slight_smile:

Right now, I’m trying to work around it:

public enum Anchoring { BottomLeft, BottomCenter, BottomRight, MiddleLeft, MiddleCenter, MiddleRight, TopLeft, TopCenter, TopRight }
private void changeAnchor(Anchoring _anchor)
            {
                if (frameAnchor != _anchor)
                    frameAnchor = _anchor;
                else return;
                Vector2 _origin = Origin;
                switch (_anchor)
                {
                    case Anchoring.TopLeft: OriginNormalized = new Vector2(0, 0); break;
                    case Anchoring.TopCenter: OriginNormalized = new Vector2(0.5f, 0); break;
                    case Anchoring.TopRight: OriginNormalized = new Vector2(1, 0); break;
                    case Anchoring.BottomLeft: OriginNormalized = new Vector2(0, 1); break;
                    case Anchoring.BottomCenter: OriginNormalized = new Vector2(0.5f, 1); break;
                    case Anchoring.BottomRight: OriginNormalized = Vector2.One; break;
                    case Anchoring.MiddleLeft: OriginNormalized = new Vector2(0, 0.5f); break;
                    case Anchoring.MiddleCenter: OriginNormalized = new Vector2(0.5f, 0.5f); break;
                    case Anchoring.MiddleRight: OriginNormalized = new Vector2(1, 0.5f); break;
                }
                _origin -= Origin;
                Position -= _origin;
            }

It is a temporary solution:
setting custom pivot (9 options) for each animation, when animation is changed.

Position = Position - (originOld - originNew) is not a precise calculation, because it doesn’t take into account difference between frames’ sizes.

Probably, when I reach Camera implementation stage, I will need to find another way :hushed:

Hey I want to share a little bit:

Using source code, I’ve managed to get it work, though it is working only with untrimmed sprites yet. I will try to calculate proper points for trimmed ones and then will submit a pull request :slight_smile:

FixedVersion

EDIT: Figured out, that pivot calculation with trimmed sprites is build-in Texture Packer, so I will keep it simple & and just submit pull request.