Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createFromObjects incorrect object position for isometric tiled map #6790

Open
ddanushkin opened this issue Apr 11, 2024 · 1 comment
Open

Comments

@ddanushkin
Copy link

Version

  • Phaser Version: 3.80.1
  • Operating system: Windows 11
  • Browser: Edge

Description

I wanted to create the tilemap objects myself and copied logic from here
But the positions of the objects were not correct

I created a new map and got exactly the same result with createFromObjects

image
image

Example Test Code

const tilemap = this.add.tilemap(Timemaps.test);

const tile_grass = tilemap.addTilesetImage('tile_grass', Images.tile_grass)!;
tilemap.createLayer('Tile Layer 1', tile_grass);

tilemap.createFromObjects('Object Layer 1', {
    gid: 2,
    key: Images.marker
});
Tiled JSON
{
    "compressionlevel": -1,
    "height": 4,
    "infinite": false,
    "layers": [
        {
            "data": [
                1,
                1,
                1,
                1,
                1,
                1,
                1,
                1,
                1,
                1,
                1,
                1,
                1,
                1,
                1,
                1
            ],
            "height": 4,
            "id": 1,
            "name": "Tile Layer 1",
            "opacity": 1,
            "type": "tilelayer",
            "visible": true,
            "width": 4,
            "x": 0,
            "y": 0
        },
        {
            "draworder": "topdown",
            "id": 2,
            "name": "Object Layer 1",
            "objects": [
                {
                    "gid": 2,
                    "height": 40,
                    "id": 1,
                    "name": "",
                    "rotation": 0,
                    "type": "",
                    "visible": true,
                    "width": 40,
                    "x": 178.41935483871,
                    "y": 179.58064516129
                }
            ],
            "opacity": 1,
            "type": "objectgroup",
            "visible": true,
            "x": 0,
            "y": 0
        }
    ],
    "nextlayerid": 3,
    "nextobjectid": 2,
    "orientation": "isometric",
    "renderorder": "right-down",
    "tiledversion": "1.10.2",
    "tileheight": 108,
    "tilesets": [
        {
            "columns": 1,
            "firstgid": 1,
            "image": "tile_grass.png",
            "imageheight": 108,
            "imagewidth": 186,
            "margin": 0,
            "name": "tile_grass",
            "spacing": 0,
            "tilecount": 1,
            "tileheight": 108,
            "tilewidth": 186
        },
        {
            "columns": 0,
            "firstgid": 2,
            "grid": {
                "height": 1,
                "orientation": "orthogonal",
                "width": 1
            },
            "margin": 0,
            "name": "marker",
            "spacing": 0,
            "tilecount": 1,
            "tileheight": 40,
            "tiles": [
                {
                    "id": 0,
                    "image": "marker.png",
                    "imageheight": 40,
                    "imagewidth": 40
                }
            ],
            "tilewidth": 40
        }
    ],
    "tilewidth": 186,
    "type": "map",
    "version": "1.10",
    "width": 4
}
@ddanushkin
Copy link
Author

I managed to solve my problem
I used this code from tiled as reference
I dont know if it breaks while scale/rotate objects in tiled, but if works for me)

const tilemap = this.add.tilemap(Timemaps.test);

const tileWidth = tilemap.tileWidth;
const halfTileWidth = tileWidth * 0.5;
const tileHeight = tilemap.tileHeight;
const originX = tilemap.height * tileWidth * 0.5;

const tile_grass = tilemap.addTilesetImage('tile_grass', Images.tile_grass)!;

const layer = tilemap.createLayer('Tile Layer 1', tile_grass)!;

//! Layer position fix
const tilemapWidth = tilemap.widthInPixels;
const halfTilemapWidth = tilemapWidth * 0.5;
layer.setPosition(halfTilemapWidth - halfTileWidth, 0);

const objects = tilemap.objects[0].objects;
for (let i = 0; i < objects.length; i++) {
     //! Ref: https://github.com/mapeditor/tiled/blob/12ca7077d3d5cd9655929030092681a986dd749a/src/libtiled/isometricrenderer.cpp#L495-L505
    const object = objects[i];

    const tileY = object.y! / tileHeight;
    const tileX = object.x! / tileHeight;

    const x = (tileX - tileY) * tileWidth * 0.5 + originX;
    const y = (tileX + tileY) * tileHeight * 0.5;

    const sprite = this.add.sprite(x, y, (Images as any)[object.name]);
    sprite.setOrigin(0.5, 1);
}

image
image

About "Layer position fix" part, layer getBounds doesn't match its rendered position

layer.setInteractive();
this.input.enableDebug(layer, 0);

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant