How to convert Touch coords to Isometric Tilemap tile coords

Not sure why this hasn’t been added yet to the default TMXLayer class. I should prob submit a PR for review.

We use this for ‘regular’ (default diamond-ish, non-staggered) isometric. Others have used similar.

auto world_pos = this->convertToNodeSpace(touch->getLocation());
auto tile_pos = layer->tileFromPosition(world_pos);
layer->setTileGID(59, tile_pos);
// custom TMXLayer subclass method (Vec2 can replace iVec2) 
iVec2 XX::tileFromPosition(const Vec2& pWorld)
{
        // iso diamond
	float wx = pWorld.x;
	float wy = pWorld.y;
	wx *= CC_CONTENT_SCALE_FACTOR();
	wy *= CC_CONTENT_SCALE_FACTOR();

	float tw = getMapTileSize().width;
	float th = getMapTileSize().height;
	float mw = getLayerSize().width;
	float mh = getLayerSize().height;

	float isox = floorf(mh - wy/th + wx/tw - mw/2);
	float isoy = floorf(mh - wy/th - wx/tw + mw/2 - 1/2);

	return iVec2(isox,isoy);
}

Related Discussion Topics

2 Likes