phaserjs / phaser-ce

Phaser CE is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
http://phaser.io
MIT License
1.35k stars 491 forks source link

TilemapLayer: fix getRayCastTile implementation #705

Closed noocsharp closed 2 years ago

noocsharp commented 2 years ago

This PR (choose one or more, ✏️ delete others)

Previously, getRayCastTile relied on getTiles, which retrieves a list of tiles in some rectangle. Not only was this inefficient, but it would cause getRayCastTile to sometimes return an empty array when there were tiles in the ray. Specifically, this bug could be triggered by calling getRayCastTile on a horizontal or vertical line. Since this is converted to a zero width or height, getTiles would return an empty array because th = 0 or tw = 0 in the for statements:

for (var wy = ty; wy < ty + th; wy++)

and

for (var wx = tx; wx < tx + tw; wx++)

meaning these loops would not even start. This behavior makes sense for the getTiles function, but does not for getRayCastTiles. Instead, we get an array of coordinates on the line to check, and simply check if a tile is present at these coordinates.

noocsharp commented 2 years ago

Just found a problem with this, please don't merge until it's fixed.

noocsharp commented 2 years ago

I had the x and y coordinates swapped, should be good to go now.

samme commented 2 years ago

I'll add the collides and interestingFace checks.

samme commented 2 years ago

I think the original function returns empty tiles (-1) as well.

noocsharp commented 2 years ago

Oh, should I send a pull request to fix this?

samme commented 2 years ago

No, I'm working on it, but thanks.