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

coordinateForPoint give wrong Point. #6

Open
AUDevs opened this issue Feb 12, 2017 · 2 comments
Open

coordinateForPoint give wrong Point. #6

AUDevs opened this issue Feb 12, 2017 · 2 comments
Assignees
Labels

Comments

@AUDevs
Copy link

AUDevs commented Feb 12, 2017

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

@mfessenden mfessenden self-assigned this Feb 12, 2017
@mfessenden
Copy link
Owner

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.

@AUDevs
Copy link
Author

AUDevs commented Feb 13, 2017

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

@mfessenden mfessenden added the bug label Apr 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants