MZ Help understanding how exactly Tilemap works

Introduction and context

So a little bit of context, I'm working on a battle system for RPG Maker MZ. For the battle, I create a new scene and I use some data from the map to generate a terrain (since it's a tactics battle). I opted for this way instead of making the battle occur directly on the map for a number of reasons, so changing to a battle directly on the map is not an option. The map images are parallax mapped, one image for the bottom layer and one image for the top layer. Under those 2 images there's a Tilemap for animated tiles (the usual method of painting animated tiles on top of parallax images).


The problem

I'm making a dynamic camera for the battle that can move freely and control the zoom freely as well. The 2 sprites for the bottom and top layer work perfectly with the camera. However, the Tilemap object moves weirdly with the camera and has some strange behaviors. Here's the code I'm currently using for the camera. And a couple of things:
  • this._camera refers to the actual current camera position and zoom
  • this._cameraMove refers to the origin and destination of the camera during a camera movement. So at each movement during the movement, it's easy to find the position by doing destination - origin + oringin * (time / total duration).
  • All of this is inside a Spriteset Object. So if the spriteset moves, the map bottom and top layers move with it. However, the Tileset doesn't seem to just move well if I just let it move with the spriteset, which is why I have a function called "repositionTilemapToCamera". However, I can't figure the math.
  • I'm not sure about the general camera formula, but it seems to be working perfectly well, except for the tilemap if the zoom isn't 1. If the zoom is 1, everything works perfectly, even the Tilemap. If the zoom isn't X1, everything works except the Tilemap.

JavaScript:
Spriteset_Battlefield.prototype.requestCameraReposition = function() {
    this.x = Math.round(Graphics.width / 2 - this._camera.x * this._camera.zoom); // Have to redo the formula
    this.y = Math.round(Graphics.height / 2 - this._camera.y * this._camera.zoom); // Have to redo the formula
    this.scale.x = this._camera.zoom;
    this.scale.y = this._camera.zoom;
    this.repositionTilemapToCamera();
};

Spriteset_Battlefield.prototype.repositionTilemapToCamera = function() {
    this._sprtMapTile.origin.x = ($battleData.map.rect.x * 48) - this.x /// this._camera.zoom;
    this._sprtMapTile.origin.y = ($battleData.map.rect.y * 48) - this.y /// this._camera.zoom;
    this._sprtMapTile.x = -this.x;
    this._sprtMapTile.y = -this.y;
};

// Called once per frame
Spriteset_Battlefield.prototype.updateCamera = function() {
    this.updateCamera_checkIfRequest();
    if (this._cameraMove.active) {
        const cm = this._cameraMove;
        cm.time.current += 1;
        const delta_x = cm.destination.x - cm.origin.x;
        const delta_y = cm.destination.y - cm.origin.y;
        const delta_z = cm.destination.zoom - cm.origin.zoom;
        const sx = Math.round(cm.origin.x + cm.time.current / cm.time.max * delta_x);
        const sy = Math.round(cm.origin.y + cm.time.current / cm.time.max * delta_y);
        const sz = Math.round( (cm.origin.zoom + cm.time.current / cm.time.max * delta_z) * 100 ) / 100;
        this._camera.x = sx;
        this._camera.y = sy;
        this._camera.zoom = sz;
        this.requestCameraReposition();
        if (sx == this._cameraMove.destination.x && sy == this._cameraMove.destination.y && sz == this._cameraMove.destination.zoom) {
            this.stopCameraMovement();
        } else if (cm.time.current >= cm.time.max) {
            this.stopCameraMovement();
        }
    }
};

Here's some footage of what happens to the tilemap when the zoom isn't X1:


Anyone have any idea what is making the Tilemap behave so weirdly? Also, if my camera formula isn't perfect, any other alternatives?

Thanks!​

Original thread: https://forums.rpgmakerweb.com/threads/help-understanding-how-exactly-tilemap-works.136115/