# Isometric formula and question on sizes (C++)

## Recommended Posts

Hello,

So I've been messing around with isometric tiles. I've come across a problem when I'm using isometric tiles that are different sizes. These tiles are 156x86 and I've looked at a few sites and they never display correctly. While others work when they are the same size, like my 40x40 isometric tile.

Are there formulas that only work if the dimensions are the same and vice-versa? If so, how can I switch between them?

Right now this works for me with my 156x86 tile, but wouldn't if I used the 40x40 tiles: I believe for the same dimension tiles, I had to times the Y-axis by an additional 0.5 to get it to work.

sf::Vector2f screenToIso(sf::Vector2i coord, sf::Vector2i tileSize)
{
return sf::Vector2f((coord.x - coord.y) * tileSize.x * 0.5,
(coord.x + coord.y) * tileSize.y * 0.5);
}

All the formulas for finding out which tile I am clicking have been conflicting as well. One tile is reading as 4 tiles (for each axis). So the top (0,0) coordinate also reads as (1,0), (0,1), and (1,1). Dividing by 4 gets me the correct tile with the different dimension tiles. The only reason I tried 4 was that various sites sometimes were dividing by 4. So I was simply playing around. (I put a comment 4 of where I do that division).

sf::Vector2i isoToScreen(sf::Vector2i coord, sf::Vector2i tileSize) {
return sf::Vector2i((coord.x / (tileSize.x / 2)) + (coord.y / (tileSize.y / 2 /*4*/)) / 2,
(coord.y / (tileSize.y / 2)) - (coord.x / (tileSize.x / 2)) / 2 /*4*/);
}

For those that know SFML, this is how I am getting this. I realize I could possibly be doing something wrong here.

sf::Vector2f mousePos = window.mapPixelToCoords(sf::Mouse::getPosition(window),view);

if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
// pointing out a float makes it read it as 5 tiles, adding (2,2)
sf::Vector2i pos{isoToScreen((sf::Vector2i)mousePos,m_tile_size)};

std::cout << pos.x  << ", " << pos.y  << "\n";
}

When I go to display them in a loop:

for(...){for(...){
sf::Vector2f pos = { screenToIso({(float)x,(float)y},m_tile_size) };
pos.x -= m_tile_size.x / 2;
pos.y -= m_tile_size.y / 2;
sprite.setPosition(pos);
window.draw(sprite);

So is there a more global formula to use that won't cause conflicts between the same or different sizes? Any help would be greatly appreciated, I've gone to many sites and they seem to have differences.

PS: This isn't about using a bigger tile than the floor tiles. It's just about understanding why one tile works and the other doesn't.

## Create an account

Register a new account

• 18
• 28
• 11
• 21
• 16
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!