title: Sorting objects to the front or back depending on their position
body: <p>I am trying to sort my renderables/actors correctly and noticed that I have some troubles with walls since they get sorted by their centerpoint. So I am sorting all my actors before I draw them depending on their distance to the camera with an insertion sort. After that, I am trying to determine if the wall should be drawn behind or in front of the gamefield. To explain this, the game takes place inside of a cube which is out of 6 planes. Since I can rotate the camera around that cube I need a sorting which would put the planes in front/back depending on that. So here is a picture so you know what we are talking about:</p>
  
  <p><img src="https://i.stack.imgur.com/mcnMe.jpg" alt="enter image description here"></p>
  
  <p>You can clearly see the rendermisstake whats happening at the front of those kind of snake.</p>
  
  <p>Okay here is my current sorting:</p>
  
  <pre><code>//list of Actors the abstract class which Wall and cube and so on extend
  void Group::insertionSort(vector&lt;Actor *&gt; &amp;actors)
  {
      int j;
  
      for (int i = 1; i &lt; actors.size(); i++)
      {
          Actor *val = actors[i];
          j = i - 1;
  
          while (j &gt;= 0 &amp;&amp; distanceToCamera(*actors[j]) &lt; distanceToCamera(*val))
          {
              actors[j + 1] = actors[j];
              j = j - 1;
          }
          actors[j + 1] = val;
      }
  }
  
  float Group::distanceToCamera(Actor &amp;a)
  {
      float result = 0;
      XMVECTOR posActor = XMLoadFloat3(&amp;a.getPosition()); //here i get the centerpoint of the object
      XMVECTOR posCamera = XMLoadFloat3(&amp;m_camera-&gt;getPosition());
      XMVECTOR length = XMVector3Length(posCamera - posActor);
      XMStoreFloat(&amp;result, length);
      return result;
  }
  </code></pre>
  
  <p>To determine if it's a <code>Wall</code> I used kind like this <code>dynamic_cast&lt;Wall*&gt;(val)</code> but I don't get them in front/back of the vector depending on that. To remember the objects return their centerpoint. <strong>Can anyone lead me to the right way?</strong> </p>