Coordinates are driving me nuts. In various places I need to convert tile coordinates to it's screen position and then back to it's original tile coordinates.
The two methods below are in a CCTMXTiledMap sub class.
I'm aware that tile 0,0 is top-left and screen is bottom-left... But the Y coordinate is all wrong here... Any ideas?
// position to tile coords
- (CGPoint) positionToPoint:(CGPoint)position {
return ccp((int)(position.x/tileSize_.width),(int)(mapSize_.height-(position.y/tileSize_.height)));
}
// tile coords to position
- (CGPoint) pointToPosition:(CGPoint)point {
int down = mapSize_.height-1;
float y = (down * tileSize_.height - (point.y * tileSize_.height));
float x = (point.x * tileSize_.width);
CGPoint ret = ccp((int)x, (int)(y));
return ret;
}
Please forgive my stupid math... I skipped math class way too much in school!