SKTiled icon indicating copy to clipboard operation
SKTiled copied to clipboard

coordinateForPoint give wrong Point.

Open AUDevs opened this issue 7 years ago • 2 comments

let coord = playerLayer.pointForCoordinate(13,1) let point = playerLayer.coordinateForPoint(coord)

point != 13,1... (result is 13,2).

Why? => return CGPoint(x: floor(pixelX / tileWidth), y: floor(pixelY / tileHeight)) if the position is from 13,1 for example (216.0, -24.0) then the outcome from pixelY / tileHeight is 2.

Edit: Map is Orthogonal Edit2: Fixed it with +tileHeight or -tileHeight the pixelY

AUDevs avatar Feb 12 '17 21:02 AUDevs

Looks like I need to rework the TiledLayerObject.coordinateForPoint method.

The reason for this is because of the difference in coordinate systems between SpriteKit and Tiled. Internally, the conversion between screen points & tile coordinate is happening in Tiled's coordinate space, and converted by the coordinateForPoint & pointForCoordinate methods.

If the point (in SpriteKit coordinate space) is (216.0, -24.0), the floor operation incorrectly returns 2 because floor(-24.0 / 16.0) returns -2 and floor(24.0 / 16.0) returns 1.

Modifying this code will give you the correct result:

    open func coordinateForPoint(_ point: CGPoint) -> CGPoint {
        let coordinate = screenToTileCoords(point.invertedY)
        return floor(point: coordinate)
    }

I'll run a few tests to make sure everything checks out, and push up a fix.

mfessenden avatar Feb 12 '17 22:02 mfessenden

for now it works. thanks for the awesome framework! Edit: invertedY is not good. if pixelY is 24 it would be -24 and we have the same problem again. if (pixelY < 0) {pixelY += 16} fixes my Problem

AUDevs avatar Feb 13 '17 17:02 AUDevs