Hello,
I am currently trying to convert world coordinates to isometric tilemap coordinates.
After reading many topics I found, that none was a solution to my problem, so I created my own transformation.
From my understanding, the coordinate system from isometric tilemaps is like following:
-
x
-axis goes from top corner to right corner -
y
-axis goes from top corner to left corner.
The tilemap is located as a child at initial position in the TileMapLayer
. This TileMapLayer gets moved around to “scroll” the tilemap and has following helper method:
Code
Vec2 TileMapLayer::getTilePositionFrom(const Vec2 &worldPos) {
auto nodePos = _tileMap->convertToNodeSpace(worldPos);
auto boxSize = _tileMap->getBoundingBox().size;
auto width = boxSize.width;
auto height = boxSize.height;
// move to origin (translation)
nodePos.x -= width / 2;
nodePos.y -= height;
// align x-axis (rotation)
auto angle = atan(height / width);
auto length = nodePos.length();
nodePos.x += cos(angle) * length;
nodePos.y += sin(angle) * length;
// invert y-axis
nodePos.y *= -1;
// align y-axis (shearing/skewing)
auto oppositeAngle = M_PI - 2 * angle; // not everything is in radians
auto sideLength = sqrt(width*width/4 + height*height/4);
auto shear = tan(oppositeAngle) * sideLength;
nodePos.x += shear * nodePos.y;
nodePos.x = (int) nodePos.x;
nodePos.y = (int) nodePos.y;
// clamps to height / width of tilemap
return clampToTileMapBounds(result);
}
After multiple draw-ups I can’t figure out my error.
At first I tried with the Mat4
transformation matrix, but it didn’t give the expected result.
Maybe the conversion to node space is wrong of some sort?
I tried many other conversions already.
Thank you for your help!
Best regards,
Julius