title: Determining draw order for isometric games and irregular shapes (Java)
body: <p>I'm having some unique trouble determining the order in which objects should be rendered, particularly in the case of when a player is behind a wall. I'll try and explain thoroughly, so prepare for a lot of information.</p>
  
  <p>I render things back to front (as recommended here: <a href="https://stackoverflow.com/questions/892811/drawing-isometric-game-worlds">Drawing Isometric game worlds</a>) and that is, for the most part, simple enough.
  I determine what is in the back using a simple comparator:</p>
  
  <pre><code>private Comparator&lt;Entity&gt; spriteSorter = new Comparator&lt;Entity&gt;(){
      public int compare(Entity e1, Entity e2){
          if(e1.getPhysics().getY() &gt; e2.getPhysics().getY()) return 1;
          if(e1.getPhysics().getY() &lt; e2.getPhysics().getY())return -1;
          return 0;
      }
  };
  </code></pre>
  
  <p>This uses the hitbox of an entity to determine its render order, and it works flawlessly for entities with a rectangular hitbox (as almost all my characters do). The actual draw call looks like this:</p>
  
  <pre><code>ArrayList&lt;Entity&gt; spriteList = new ArrayList&lt;&gt;();
      spriteList.add(player);
  
      for(int i = 0; i &lt; walls.size(); i++){
          spriteList.add(walls.get(i));
      }
  
      Collections.sort(spriteList, spriteSorter);
          for(int i = 0; i &lt; spriteList.size(); i++){
          spriteList.get(i).render(g);
      }
  </code></pre>
  
  <p>Here's where things get tricky though, all my characters use an instance of Rectangle, and their ordering works with no problem at all, but my walls (due to the nature of an isometric game) cannot use a rectangle, and thus use a Polygon that fits their size.
  See picture: <img src="https://i.stack.imgur.com/PXqlM.png" alt="This is what the hitboxes for a player and a wall look like."></p>
  
  <p>I have to use a Polygon because if I use a rectangle, I get a stupid unpassable space near the wall like so:
  <img src="https://i.stack.imgur.com/5kYce.png" alt="Stupid."></p>
  
  <p>Unfortunately, due to the nature of polygons, there is no immediately apparent test to see if the player is in front or behind the wall. Line 3 seems to be a likely candidate as a test, but there are two triangles on either side of the wall that would pass the <code>player.getPhysics.getY() &gt; wall.getPhysics.getY()</code> test and mark him as in front, when he could be both in front and behind the wall. </p>
  
  <p><img src="https://i.stack.imgur.com/OwAHz.png" alt="Grr"></p>
  
  <p>So, how can I devise a test to tell me when he is behind the diagonal lines? The nature of Shapes and Polygons in Java seems to be limiting me a lot, but I'm sure there's a way to do it. I've played with points a lot, and getCenterY() has gotten fairly close, but it's still not perfect. The problem cases that arise with all of the methods I've tried look like this:</p>
  
  <p><img src="https://i.stack.imgur.com/IkCoI.png" alt="Actually behind, rendered in front">Actually behind, rendered in front</p>
  
  <p>or this:</p>
  
  <p><img src="https://i.stack.imgur.com/cq4gR.png" alt="Actually in front, rendered behind">Actually in front, rendered behind.</p>
  
  <p>Any one got any ideas? </p>