Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Wrong isometric tile position when the world coordinates become negative  (Read 918 times)

0 Members and 1 Guest are viewing this topic.

Tim

  • Newbie
  • *
  • Posts: 2
    • View Profile
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

« Last Edit: September 19, 2022, 02:17:18 pm by Tim »

Tim

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Wrong isometric tile position when the world coordinates become negative
« Reply #1 on: September 20, 2022, 11:31:38 am »
I fixed it by replacing % and / to implementations of floor_div and floor_mod and used them instead

inline int floor_mod(int x, int y) {
  x %= y;

  if (x < 0) {
      x += y;
  }

  return x;
}

inline int floor_div (int num, int den)
{
  if (0 < (num^den))
    return num/den;
  else
    {
      ldiv_t res = ldiv(num,den);
      return (res.rem)? res.quot-1
                      : res.quot;
    }
}

// ...

sf::Vector2i cell = sf::Vector2i{floor_div(mouse.x, chunk->get_tile_size().x), floor_div(mouse.y,chunk->get_tile_size().y)};

sf::Vector2i offset = sf::Vector2i{floor_mod(mouse.x, chunk->get_tile_size().x), floor_mod(mouse.y, chunk->get_tile_size().y)};
 


 

anything