MZ About transforming pictures with multiple origins

I'm currently developing a plugin that allows me to manipulate pictures with different transformations (translation, mirroring, rotation, scaling, ...), according to origins based on the following 9 coordinate points:

Code:
X---X---X
|   |   |
X---X---X
|   |   |
X---X---X

All the transformations are working independently as expected. However, I have a problem with the origin management. For example, if I position a picture on X and Y coordinates based on the top left origin, and then try a rotation based on the bottom left origin, the origin point moves to the original X/Y coordinates and my image ends up rotated in the wrong place.

Spoiler: Illustration of the problem
Transformation.gif


Initial.jpg


Final.jpg

After a long analysis of the code (and a lot of headaches to understand how it works), I discovered that the Sprite_Picture object had only one possible anchor, via this function (original code from rmmz_sprites.js):

Code:
Sprite_Picture.prototype.updateOrigin = function() {
    const picture = this.picture();
    if (picture.origin() === 0) {
        this.anchor.x = 0;
        this.anchor.y = 0;
    } else {
        this.anchor.x = 0.5;
        this.anchor.y = 0.5;
    }
};

This means that each transformation can only be linked to one origin at a time. And as the picture sprite is updated with each game frame, if the origin changes, all the dimensions (X/Y position, X/Y scale, angle) are readjusted to this origin, causing the unwanted edge effect.

Code:
Sprite_Picture.prototype.update = function() {
    Sprite_Clickable.prototype.update.call(this);
    this.updateBitmap();
    if (this.visible) {
        this.updateOrigin();
        this.updatePosition();
        this.updateScale();
        this.updateTone();
        this.updateOther();
    }
};

Please note that, apart from a function for resetting all transformations made on a picture, each transformation controlled by my plugin is independent of the others (contrary to the standard Move Picture function in RMMZ).

So in your opinion, is it possible to manage several origins per transformation (origin for positioning, origin for rotation, origin for scaling, etc.) with the Sprite_Picture object? Or is this a limitation of the PIXI.Sprite object (subsequently inherited by all RMMZ's Sprite_* objects)? And what solution could be envisaged to get around this problem (if any)?

Thank you in advance for any help or suggestions.

PS: For your information, the code currently under development for my plugin, if you want to test some ideas: raw code (gitlab).
Maybe just add an offset to the sprite's coordinates when the origin changes?

Also, in case it's relevant:
Spoiler: anchor vs pivot
  • anchor is the normalised texture origin.
    • When (0, 0), the texture appears centred in its bounding box.
    • When (0.5, 0.5), the texture appears centred on its pivot.

  • pivot is the origin for all transforms, including translation, scaling, rotation, and skew.
    • It is measured in px, based on the display object's bounds.
    • Default is (0, 0), i.e. top-left.
    • A display object's position within its parent is the location of its transform origin (i.e. pivot) relative to that of its parent.
Maybe just add an offset to the sprite's coordinates when the origin changes?
Thanks for the suggestion, I'll look into it...

Also, in case it's relevant:
This is the case! However, it seems that the pivot is only used to display the bitmap in the sprite frame, whereas the anchor is used for positioning during each transformation... which is contrary to the doc, isn't it (or maybe I'm wrong)?

Spoiler: PIVOT
Code:
Sprite.prototype._onBitmapLoad = function(bitmapLoaded) {
    if (bitmapLoaded === this._bitmap) {
        if (this._refreshFrame && this._bitmap) {
            this._refreshFrame = false;
            this._frame.width = this._bitmap.width;
            this._frame.height = this._bitmap.height;
        }
    }
    this._refresh();
};

Sprite.prototype._refresh = function() {
    const texture = this.texture;
    const frameX = Math.floor(this._frame.x);
    const frameY = Math.floor(this._frame.y);
    const frameW = Math.floor(this._frame.width);
    const frameH = Math.floor(this._frame.height);
    const baseTexture = this._bitmap ? this._bitmap.baseTexture : null;
    const baseTextureW = baseTexture ? baseTexture.width : 0;
    const baseTextureH = baseTexture ? baseTexture.height : 0;
    const realX = frameX.clamp(0, baseTextureW);
    const realY = frameY.clamp(0, baseTextureH);
    const realW = (frameW - realX + frameX).clamp(0, baseTextureW - realX);
    const realH = (frameH - realY + frameY).clamp(0, baseTextureH - realY);
    const frame = new Rectangle(realX, realY, realW, realH);
    if (texture) {
        this.pivot.x = frameX - realX;
        this.pivot.y = frameY - realY;
        if (baseTexture) {
            texture.baseTexture = baseTexture;
            try {
                texture.frame = frame;
            } catch (e) {
                texture.frame = new Rectangle();
            }
        }
        texture._updateID++;
    }
};

Spoiler: ANCHOR
Code:
Sprite_Picture.prototype.update = function() {
    Sprite_Clickable.prototype.update.call(this);
    this.updateBitmap();
    if (this.visible) {
        this.updateOrigin();
        this.updatePosition();
        this.updateScale();
        this.updateTone();
        this.updateOther();
    }
};

Sprite_Picture.prototype.updateBitmap = function() {
    const picture = this.picture();
    if (picture) {
        const pictureName = picture.name();
        if (this._pictureName !== pictureName) {
            this._pictureName = pictureName;
            this.loadBitmap();
        }
        this.visible = true;
    } else {
        this._pictureName = "";
        this.bitmap = null;
        this.visible = false;
    }
};

Sprite_Picture.prototype.updateOrigin = function() {
    const picture = this.picture();
    if (picture.origin() === 0) {
        this.anchor.x = 0;
        this.anchor.y = 0;
    } else {
        this.anchor.x = 0.5;
        this.anchor.y = 0.5;
    }
};

Sprite_Picture.prototype.updatePosition = function() {
    const picture = this.picture();
    this.x = Math.round(picture.x());
    this.y = Math.round(picture.y());
};

Sprite_Picture.prototype.updateScale = function() {
    const picture = this.picture();
    this.scale.x = picture.scaleX() / 100;
    this.scale.y = picture.scaleY() / 100;
};

Note that the problem is also present with a translation from an X/Y position to a new X'/Y' position if the origin changes... so not sure that the notion of pivot is relevant here.

Spoiler: Move unwanted effect
Move.gif


Move.jpg
I see no contradiction: you can change the pivot (which translates the sprite's coordinate system) and/or the anchor (which translates the texture display location in that coordinate system).

In the core scripts, I believe pivot is set to 0 unless the sprite's texture is smaller than its frame (or the frame is negative size); anchor is widely used to place the texture relative to that pivot.

You probably don't need to worry about the pivot, just thought I'd mention that it's a thing. 😴
Hi there!

Don't know if I understand you correctly, but here is my thoughs:

I guess what you could try to do is emulate the origin/anchor changes with the x,y,width, and height properties of a picture. I think this way, the transition would be smooth, instead of the picture jump to another position like in your example. If you change the anchor of a picture, it will visually changes it's position, even without changing the X and Y properties values.

I mean, it is more work to do, but I guess it is more simple than dealing with the origin change plus a rotation or any other changes.

An example of what I mean is, let's say you have a Picture Origin Upper Left → X = 20 / Y = 20. And you wanna rotate it so it looks like it has another anchor value, like centered(0.5, 0.5). Instead of changing the anchor points, you could "emulate" that, by just doing:

JavaScript:
sprite.x = sprite.x - width/2
sprite.y = sprite.y - height/2

This would change the X and Y position values, and it could be bad on some occasions that you want just to retrieve the X and Y values.
So maybe creating a new property like originOffsetX or even a new method:

JavaScript:
gamePicture.originOffsetX = -this.width/2
sprite.x = gamePicture.x + gamePicture.originOffsetX

Then, if perhaps you want to do something with the X and Y positions, you could get their original values on the gamePicture.x
On the image below, the red box is the picture with Upper Left origin. The Yellow box would be that same picture, with the calculations above. But still, having it's anchor points as Upper Left.
1714250582707.png


On the code, the Picture Sprite would still has the same Origin Upper Left, but it will look like it has it centered.
you can change the pivot (which translates the sprite's coordinate system) and/or the anchor (which translates the texture display location in that coordinate system).
With such simple and precise words, it's immediately clearer for me. Really, thanks to you.

Hi there!

Don't know if I understand you correctly, but here is my thoughs:

I guess what you could try to do is emulate the origin/anchor changes with the x,y,width, and height properties of a picture. I think this way, the transition would be smooth, instead of the picture jump to another position like in your example. If you change the anchor of a picture, it will visually changes it's position, even without changing the X and Y properties values.

I mean, it is more work to do, but I guess it is more simple than dealing with the origin change plus a rotation or any other changes.

An example of what I mean is, let's say you have a Picture Origin Upper Left → X = 20 / Y = 20. And you wanna rotate it so it looks like it has another anchor value, like centered(0.5, 0.5). Instead of changing the anchor points, you could "emulate" that, by just doing:

JavaScript:
sprite.x = sprite.x - width/2
sprite.y = sprite.y - height/2

This would change the X and Y position values, and it could be bad on some occasions that you want just to retrieve the X and Y values.
So maybe creating a new property like originOffsetX or even a new method:

JavaScript:
gamePicture.originOffsetX = -this.width/2
sprite.x = gamePicture.x + gamePicture.originOffsetX

Then, if perhaps you want to do something with the X and Y positions, you could get their original values on the gamePicture.x
On the image below, the red box is the picture with Upper Left origin. The Yellow box would be that same picture, with the calculations above. But still, having it's anchor points as Upper Left.
View attachment 303307

On the code, the Picture Sprite would still has the same Origin Upper Left, but it will look like it has it centered.
Oh yeah! I appreciate your help in offering me another really relevant point of view. Your proposal offers me a whole new approach to the problem, thank you. So yes, it's going to be a bit more work than I imagined, but it should be worth it.

Now all that's left to do is develop and test it.

:MV3:
Yoohoo! I think I'm on the right path to success! With just the few adjustments below that were inspired by @Eliaquim's suggestions, I've managed to get some smooth moves.

Code:
ODW.APS.Sprite_Picture_updateOrigin = Sprite_Picture.prototype.updateOrigin;
Sprite_Picture.prototype.updateOrigin = function() {
    const picture = this.picture();
    if (picture && picture instanceof APS_Game_Picture) {
        picture.updateOrigin(this.width, this.height);
        this.anchor.x = picture.origin().x;
        this.anchor.y = picture.origin().y;
    } else {
        ODW.APS.Sprite_Picture_updateOrigin.call(this);
    }
};

APS_Game_Picture.prototype.updateOrigin = function(width, height) {
    if (this._targetOrigin.x !== this._origin.x) {
        this._x += (this._targetOrigin.x - this._origin.x) * (Math.abs(this._scaleX) / 100) * width;
    }
    if (this._targetOrigin.y !== this._origin.y) {
        this._y += (this._targetOrigin.y - this._origin.y) * (Math.abs(this._scaleY) / 100) * height;
    }
    this._origin = this._targetOrigin;
};

Example below with each movement change using a different origin point (the final move is a "reset" command to restore the initial picture parameters).

Spoiler
Test.gif

Now I still have to deal with rotations... but I'll have to go back to studying trigonometry, and that's going to take some time. In the meantime, I've already got enough to create a game like PONG. 😀

Thanks again for you help, @Eliaquim and @caethyril !

Original thread: https://forums.rpgmakerweb.com/threads/about-transforming-pictures-with-multiple-origins.168115/