title: 2D isometric - SFML - Right formulas, wrong coordinate range
body: <p>I don't work with tiles but cubes drawn with sf::Vertex. Each cubes have 6 sides with 4 points each.
  <a href="https://i.stack.imgur.com/ewBnx.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ewBnx.png" alt="enter image description here"></a></p>
  
  <p>So i just have to <code>cubes[numCube].sides()[numSide]....</code> to select a side.</p>
  
  <p>I create cubes layer.cpp :</p>
  
  <pre><code>for(int J = 0; J &lt; mapSize; J++)
      {
          for(int I = 0; I &lt; mapSize; I++)
          {
              x = (J - I) * (cubeSize/2);
              y = (J + I) * (cubeSize/4);
  
              c = new cube(cubeSize, x, y, z, I, J);
              cs.push_back(*c);
          }
      }
  </code></pre>
  
  <p>In cube.cpp i create sides, then, in sides.cpp, i calcul each points' coordinates like this :</p>
  
  <pre><code>switch(typeSide)
  {
      case 0://DOWN_SIDE
          light = 1;
  
          tmp_x = x + (size/2);
          tmp_y = y + (size/2);
          p0 = new point(tmp_x, tmp_y, tmp_z);
  
          tmp_x = x + size;
          tmp_y = y + (3 * (size/4));
          p1 = new point(tmp_x, tmp_y, tmp_z);
  
          tmp_x = x + (size/2);
          tmp_y = y + size;
          p2 = new point(tmp_x, tmp_y, tmp_z);
  
          tmp_x = x;
          tmp_y = y + (3 * (size/4));
          p3 = new point(tmp_x, tmp_y, tmp_z);
          break;
  
      case 1://BACK_LEFT_SIDE
  
  //ETC. ....
  </code></pre>
  
  <p>Point.cpp :</p>
  
  <pre><code>/*
   * point.cpp
   *
   *  Created on: 21 nov. 2015
   *      Author: user
   */
  
  #include "point.h"
  
  point::point(float tx, float ty, float tz)
  {
      coords* dummyVar = new coords(tx, ty, tz);
      coordinates = dummyVar;
  }
  
  std::vector&lt;float&gt; point::position()//Use : myPoint.getPosition[0] //get the x
  {
      std::vector&lt;float&gt; dummyVar;
  
      dummyVar.push_back(coordinates-&gt;getX());
      dummyVar.push_back(coordinates-&gt;getY() - coordinates-&gt;getZ());
  
      return dummyVar;
  }
  
  void point::move(float tx, float ty, float tz)
  {
      coordinates-&gt;setX(tx);
      coordinates-&gt;setY(ty);
      coordinates-&gt;setZ(tz);
  }
  </code></pre>
  
  <p>My problem come from the function i use to detect click :</p>
  
  <pre><code>if (event.type == sf::Event::MouseMoved)
  {
              currentSelectedCube = maps[currentMapID].getCubeIDAt(event.mouseMove.x, event.mouseMove.y, offsetLeft, offsetTop, enableOffset);
  }
  </code></pre>
  
  <p>The function(don't bother with the comments) :</p>
  
  <p><strong>I try to get a cube's entry in my cube vector without 'for loop'.
  Why ? to use less CPU when i click.</strong></p>
  
  <pre><code>int map::getCubeIDAt(float x, float y, int offsetLeft, int offsetTop, bool enableOffset)//WIP ! //USED FOR CLICK DETECTION ON CUBES
      {
      //----------------------------------------------------------------//
          int unsigned entry = -1;
  
          int I = 0;
          int J = 0;
      //----------------------------------------------------------------//
  
          if(currentLayerId() &gt; -1)//If there is any layers
          {
              //IF CHECK IN MAP BOUDING BOX + ROTATION TO GOT DIAMOND SHAPE AREA(LAYER + OFFSETS)----------------------------------
              //{
  
                  if(!enableOffset)//With offsets disabled
                  {
                      I = (y * 2 - x) / cubeSize;
                      J = (y * 2 + x) / cubeSize;
                  }
                  else //With offsets enabled
                  {
                      I = (((y-offsetTop)+(currentLayerId()*(cubeSize/2))) * 2 - (x-offsetLeft)) / cubeSize;
                      J = (((y-offsetTop)+(currentLayerId()*(cubeSize/2)))  * 2 + (x-offsetLeft)) / cubeSize;
                  }
  
                  entry = I + J * size;
  
                  if (entry &lt; 0 || entry &gt;= layers()[currentLayerId()].cubes().size())
                  {
                      entry = -1;
                  }
                  else//DEBUG - DISPLAYING VALUES FOR TEST
                  {
                      std::cout &lt;&lt; "Entry n°" &lt;&lt; entry &lt;&lt; " - ";
                      std::cout &lt;&lt; "[" &lt;&lt; I &lt;&lt; "; " &lt;&lt; J &lt;&lt; "]" &lt;&lt; std::endl;
                  }
              //}
              //END IF CHECK IN MAP BOUDING BOX + ROTATION TO GOT DIAMOND SHAPE AREA(LAYER + OFFSETS)----------------------------------
          }
  
          return entry;
      }
  </code></pre>
  
  <p>The I-J and entryNumber are OK. i mean, for example, for the cube 0, i have I = 0; J = 0; etc ... This is working.</p>
  
  <p>I don't understand why the coordinate range is like the red part(not accurate at 100%, i'm not a paint genius ha ha) in this picture :</p>
  
  <p><a href="https://i.stack.imgur.com/nIb4Z.png" rel="noreferrer"><img src="https://i.stack.imgur.com/nIb4Z.png" alt="enter image description here"></a></p>
  
  <p>But i should get that(2nd picture - the red part is where i click) :</p>
  
  <p><em>But after few checks, the I-J and the entry i got are corresponding. This is so weird.</em></p>
  
  <p><a href="https://i.stack.imgur.com/fsdI0.png" rel="noreferrer"><img src="https://i.stack.imgur.com/fsdI0.png" alt="enter image description here"></a></p>
  
  <p><strong>EDIT2:</strong>
  <em>Offsets and layer number implemented.</em>
  <strong>Problem left: wrong coordinates range.</strong></p>
  
  <p>Just in case, this is the 'function' handling events :</p>
  
  <pre><code>void GRAPHICS_HANDLER::listenEvents()
  {
      while (window-&gt;pollEvent(event))
      {
          if (event.type == sf::Event::Closed)
          {
              window-&gt;close();
          }
  
          if(event.type == sf::Event::KeyPressed)
          {
              //DISPLAY/UNDISPLAY GRID -- DEBUG FUNCTION
              if(event.key.code == sf::Keyboard::Escape)
              {
                  if(grid)
                      grid = false;
                  else
                      grid = true;
              }
  
  //-----------------------------------------------------------------------------------DEBUG---------------------------------------------------------------//
              if(event.key.code == sf::Keyboard::B)//ACTIVE BRUSHMODE -- NEED TO BLOCK IT WHEN ACCESS VIOLATION OF CUBES ARRAY(CRASH)
              {
                  if(!brushMode)
                  {
                      brushMode = true;
                      std::cout &lt;&lt; "Brush mode enabled" &lt;&lt; std::endl;
                  }
                  else
                  {
                      brushMode = false;
                      std::cout &lt;&lt; "Brush mode disabled" &lt;&lt; std::endl;
                  }
              }
  
              if(event.key.code == sf::Keyboard::L)//ADD_LAYER
              {
                  addLayer(getCurrentMapID());
              }
  
              if(event.key.code == sf::Keyboard::M)//DELETE_LAYER
              {
                  deleteLayer(currentMapID, maps[currentMapID].currentLayerId());
              }
  
              if(event.key.code == sf::Keyboard::S)//ADD_LAYER
              {
                  std::cout &lt;&lt; "Select a texture: ";
                  std::cin &gt;&gt; currentSelectedTexture; std::cout &lt;&lt; std::endl;
              }
  
              if(event.key.code == sf::Keyboard::Left)//Move in Layer
              {
                  if(maps[currentMapID].currentLayerId() &gt; 0)
                  {
                      maps[currentMapID].setCurrentLayerID(maps[currentMapID].currentLayerId()-1);
                  }
              }
  
              if(event.key.code == sf::Keyboard::Right)//Move in Layer
              {
                  if(maps[currentMapID].currentLayerId() &lt; maps[currentMapID].layers().size()-1)
                  {
                      maps[currentMapID].setCurrentLayerID(maps[currentMapID].currentLayerId()+1);
                  }
              }
  //-----------------------------------------------------------------------------------DEBUG---------------------------------------------------------------//
          }
  
          if (event.type == sf::Event::MouseMoved)
          {
  //--------------------------------------------------------------------------CURSOR-----------------------------------------------------------------------//
              currentSelectedCube = maps[currentMapID].getCubeIDAt(event.mouseMove.x, event.mouseMove.y, offsetLeft, offsetTop, enableOffset);
  //--------------------------------------------------------------------------CURSOR-----------------------------------------------------------------------//
          }
  
          if (event.type == sf::Event::MouseButtonPressed)
          {
  //--------------------------------------------------------------------------CURSOR-----------------------------------------------------------------------//
              currentSelectedCube = maps[currentMapID].getCubeIDAt(event.mouseButton.x, event.mouseButton.y, offsetLeft, offsetTop, enableOffset);
  //--------------------------------------------------------------------------CURSOR-----------------------------------------------------------------------//
              if (event.mouseButton.button == sf::Mouse::Left)
              {
  //--------------------------------------------------------------------------CUBE CLICK DETECTION--------------------------------------------------//
                  if(maps.size() &gt; 0 &amp;&amp; maps[currentMapID].layers().size() &gt; 0 &amp;&amp; currentSelectedCube &gt; -1)
                  {
                      cubeClicked = true;
                  }
              }
  
              if (event.mouseButton.button == sf::Mouse::Right)
              {
                  if(maps.size() &gt; 0 &amp;&amp; maps[currentMapID].layers().size() &gt; 0 &amp;&amp; currentSelectedCube &gt; -1)
                  {
                      maps[currentMapID].layers()[maps[currentMapID].currentLayerId()].cubes()[currentSelectedCube].setTexture(1);
                  }
              }
  //--------------------------------------------------------------------------CUBE CLICK DETECTION--------------------------------------------------//
          }
      }
  }
  </code></pre>
  
  <p><strong>EDIT3:</strong> I updated my code to allow me to draw only the down side of the cube, so i can do this(the grass) :
  <a href="https://i.stack.imgur.com/HZaBq.png" rel="noreferrer"><img src="https://i.stack.imgur.com/HZaBq.png" alt="enter image description here"></a></p>
  
  <p>The coordinate range(the red isometric square shown before in the screenshots) change a little when i put flat square(green).
  I don't know why, i prefer to precise it, just in case.</p>