Hey, I am making an isometric map class, but I've faced an issue when trying to get tiles on the map when the world values presented are negative. This happens often because the values easily become negative if the player explores towards the left and up direction of the screen. I am fairly certain the issue is because of precision errors but I came here to double check and also I am unsure how I could fix it if it has to do with precision
int get_tile_index(sf::RenderWindow* window, Chunk* chunk, int chunk_idx /* unused */)
{
sf::Vector2f mouse_float = sf::Vector2f(window->mapPixelToCoords(sf::Mouse::getPosition(*window)));
sf::Vector2i mouse = {int(floor(mouse_float.x)), int(floor(mouse_float.y))};
// tilesize is 40 by 20
sf::Vector2i cell = sf::Vector2i{mouse.x/chunk->get_tile_size().x, mouse.y/chunk->get_tile_size().y};
// origin is the position of the chunk's first tile
sf::Vector2i selected =
{
(cell.y - chunk->get_origin().y) + (cell.x - chunk->get_origin().x),
(cell.y - chunk->get_origin().y) - (cell.x - chunk->get_origin().x)
};
// some other code that will further verify the position that i will not be putting for simplicity
int idx = selected.y * chunk->get_chunk_size().x + selected.x;
// guard array boundry, chunk size is 16x16
if (selected.x < chunk->get_chunk_size().x && selected.y < chunk->get_chunk_size().y && selected.x >= 0 && selected.y >= 0)
{
return idx; // number from 0 to 15
}
return -1;
}
Everything except the `mouse_float` variable is an integer, or sf::Vector2i so I am confused how this could happen. When either mouse_float x or y become negative, the wrong tile will be selected, for example if I am selecting the tile 3 - 2 it becomes 3 - 3 or 3 - 1. This is not the case when both of them are positive.
This is what happens when I use the get_tile_index function to replace the tiles
Negative:
Normal:
Thanks
Edit: more clarification