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

WorldToTileX not working for Hex Tilesets (TileY and TileXY works) #5608

Closed
stonerich opened this issue Mar 16, 2021 · 5 comments
Closed

WorldToTileX not working for Hex Tilesets (TileY and TileXY works) #5608

stonerich opened this issue Mar 16, 2021 · 5 comments

Comments

@stonerich
Copy link

Version

  • Phaser Version: 3.53.1
  • Operating system: MacOS MigSur
  • Browser:

Description

Tilemaplayer.worldToTileX gives undefined in the code below. Expected behavior is to get the X pos

However, both ..ToTileXY and ... ToTileY works

It seems that the GetWorldToTileXFunction (Phaser.Tilemaps.Components.GetWorldToTileXFunction) is not updated in the same way as GetWorldToTileYFunction is.

var GetWorldToTileXFunction = function (orientation)
{
    if (orientation === CONST.ORTHOGONAL)
    {
        return WorldToTileX;
    }
    else
    {
        return NOOP;
    }
};
var GetWorldToTileYFunction = function (orientation)
{
    if (orientation === CONST.ORTHOGONAL)
    {
        return WorldToTileY;
    }
    else if (orientation === CONST.HEXAGONAL)
    {
        return HexagonalWorldToTileY;
    }
    else if (orientation === CONST.STAGGERED)
    {
        return StaggeredWorldToTileY;
    }
    else
    {
        return NOOP;
    }
};

Example Test Code

this.map = this.make.tilemap({key: 'hexmap'});
this.tiles = this.map.addTilesetImage('Hexagons')
this.layer = this.map.createLayer('Map', this.tiles, 20, 30);

this.layer.setInteractive();
this.layer.on('pointerup', (pointer) => {
    console.log("Pointer Up " , pointer, pointer.downX);        
    let tileX = this.layer.worldToTileX(pointer.downX, true);
    let tileXY = this.layer.worldToTileXY(pointer.downX, pointer.downY, true);
    let tileY = this.layer.worldToTileY(pointer.downY, true);
    console.log("Tile", tileXY, tileX, tileY);

});

Additional Information

@stonerich stonerich changed the title WolrdToTileX not working for Hex Tilesets (TileY and TileXY works) WorldToTileX not working for Hex Tilesets (TileY and TileXY works) Mar 16, 2021
@druerridge
Copy link

I'm also experiencing this, but while the XY function works better than the X function, but still doesn't work for me (seems to have square detection area?). I wonder why it doesn't work for me? Were you using arcade or matter physics? Would that matter? Here's my explanation/repro on the forums.

https://phaser.discourse.group/t/gettileatworldxy-selects-wrong-tile-on-hex-maps-from-tiled/11643/2

@innerexception
Copy link

image

@photonstorm
Copy link
Collaborator

photonstorm commented Nov 23, 2022

I'll patch this so it doesn't error, but it's correct that there is no TileToWorldX (or Y) functions for hex or iso maps because it's impossible to get the correct tile from a single coordinate. You have to provide x and y.

@photonstorm
Copy link
Collaborator

Damn, this took a complete rewrite of the function (it was totally wrong!), and it now supports any size hexagon (with varying widths / heights) and layer scales without error. Phew, was quite a bit of learning and optimizing. Video showing it:

hex-clicks.mp4

Demo here: https://labs.phaser.io/view.html?src=src/bugs/5608%20hex%20tile.js&v=dev

I've no doubt that the opposite function, HexagonalTileToWorldXY, is completely wrong, but at least this one is working properly now.

@gsim
Copy link

gsim commented Jan 21, 2023

Wow it took me a long time to find this. Great work thanks.

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

No branches or pull requests

6 participants