Skip to content

Fix StaggeredWorldToTileXY returning the wrong tile for most points - #7404

Open
vtxe02023274 wants to merge 1 commit into
phaserjs:masterfrom
vtxe02023274:fix/staggered-world-to-tile
Open

vtxe02023274 wants to merge 1 commit into
phaserjs:masterfrom
vtxe02023274:fix/staggered-world-to-tile

Conversation

@vtxe02023274

Copy link
Copy Markdown

This PR

  • Fixes a bug

Describe the changes below:

Fixes #7144

getTileAtWorldXY on a staggered map returned the wrong tile for most points. StaggeredWorldToTileXY had two problems:

  • It picked the row from the bounding box, but the boxes of neighboring rows overlap by half a tile, so in the top half of a box the point is often in the row above.
  • It added half a tile for odd rows instead of taking it off, so odd rows were one tile off to the right.

With snapToFloor it now finds the diamond the point is in. In half tile units the diamond centers are where u + v and u - v are both even, so it rounds to that grid and converts back to tile x and y. The non snapped result is left as it was, a fractional tile position does not have one clear meaning on a staggered grid.

The tests for the snapped path checked the old results, for example (32, 32) is the center of tile (0, 1) but was expected to be (1, 2). They now check the center and the four corners of every tile in a few tile sizes, the corners of the bounding box, the odd row shift, and the layer offset, scale and scroll cases with tile centers.

Checked in Chrome as well: a 6x8 staggered Tiled JSON map with a different color for every tile, 3034 random points where the rendered pixel is a tile color, comparing that color to layer.getTileAtWorldXY. Before this change 2295 of them were wrong, after it none are.

npm run lint fails on master with ESLint 10 (.eslintrc.json needs an import attribute), so I ran eslint@8 with .eslintrc.json on the changed file, no errors.

It used the tile bounding boxes, which overlap between rows, and shifted odd rows the wrong way. It now finds the diamond the point is in. Fixes phaserjs#7144
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

Successfully merging this pull request may close these issues.

getTileAtWorldXY does not appear to be accurate for staggered isometric maps

1 participant