Index: orxonox/trunk/src/lib/graphics/spatial_separation/quadtree.cc
===================================================================
--- orxonox/trunk/src/lib/graphics/spatial_separation/quadtree.cc	(revision 4919)
+++ orxonox/trunk/src/lib/graphics/spatial_separation/quadtree.cc	(revision 4920)
@@ -57,5 +57,11 @@
      this->rootNode->buildHashTable(this->nodes, index);
    }
+   this->sortHashTable(this->nodes);
    this->revertHashTable(this->nodes);
+
+   for(int i = 0; i < (int)pow(4, treeDepth); ++i)
+   {
+     printf("node %i, %f, %f \n", i, this->nodes[i]->getDimension()->getCenter()->x, this->nodes[i]->getDimension()->getCenter()->z);
+   }
 }
 
@@ -103,4 +109,34 @@
 
 
+void Quadtree::sortHashTable(QuadtreeNode** nodes)
+{
+  int                  len         = (int)pow(2, this->treeDepth);          //!< the length of a quadtree side
+  float a,b;
+  QuadtreeNode* tmpNode;
+
+  for(int i = 0; i < len; ++i)
+  {
+    for(int j = 0; j < len; ++j)
+    {
+      for(int k = j + 1; k < len; ++k)
+      {
+        a = this->nodes[i * len + j]->getDimension()->getCenter()->z;
+        b = this->nodes[i * len + k]->getDimension()->getCenter()->z;
+
+        if( b > a)
+        {
+          tmpNode = this->nodes[i * len + j];
+          this->nodes[i * len + j] = this->nodes[i * len + k];
+          this->nodes[i * len + k] = tmpNode;
+        }
+      }
+
+    }
+
+  }
+}
+
+
+
 QuadtreeNode* Quadtree::getQuadtreeFromPosition(const Vector& position)
 {
@@ -108,11 +144,28 @@
   Rectangle* r = this->rootNode->getDimension();
   float len = this->nodes[0]->getDimension()->getAxis() * 2.0f;
+
+  Vector edge;
   float xOff = r->getCenter()->x - r->getAxis();
   float yOff = r->getCenter()->z - r->getAxis();
+  edge.x = xOff, edge.z = yOff;
 
-  int i = (int)(( position.x - xOff) / len) + 1;
-  int j = (int)(( position.z - yOff) / len) + 1;
+  /* shift into model space */
+  v = position - edge;
+  /* map */
+  int i = (int)(v.x / len);
+  int j = (int)(v.z / len);
 
-  printf("the array coordinates are: %i, %i\n", i, j);
+  int max = (int)pow(2, this->treeDepth);
+
+  if( i < max && j < max)
+  {
+    printf("-----------\nthe array coordinates are: %i, %i\n", i, j);
+    printf("position: %f,%f, center %f, %f\n", position.x, position.z, this->nodes[i + j * max]->getDimension()->getCenter()->x, this->nodes[i + j * max]->getDimension()->getCenter()->z);
+    this->nodes[i + j * max]->drawTree(0,0);
+    this->nodes[i + j * max]->includesPoint(position);
+  }
+  else
+    printf("object has left terrain\n");
+
 }
 
@@ -126,5 +179,5 @@
   for(int i = 0; i < (int)pow(4, this->treeDepth); ++i)
   {
-    this->nodes[i]->drawTree(0, 0);
+    //this->nodes[i]->drawTree(0, 0);
   }
 
Index: orxonox/trunk/src/lib/graphics/spatial_separation/quadtree.h
===================================================================
--- orxonox/trunk/src/lib/graphics/spatial_separation/quadtree.h	(revision 4919)
+++ orxonox/trunk/src/lib/graphics/spatial_separation/quadtree.h	(revision 4920)
@@ -31,4 +31,5 @@
   private:
     void revertHashTable(QuadtreeNode** nodes);
+    void sortHashTable(QuadtreeNode** nodes);
 
  private:
Index: orxonox/trunk/src/lib/graphics/spatial_separation/quadtree_node.cc
===================================================================
--- orxonox/trunk/src/lib/graphics/spatial_separation/quadtree_node.cc	(revision 4919)
+++ orxonox/trunk/src/lib/graphics/spatial_separation/quadtree_node.cc	(revision 4920)
@@ -357,4 +357,15 @@
 
 
+bool QuadtreeNode::includesPoint(const Vector& v)
+{
+  Vector center = *this->pDimension->getCenter();
+  float ax = this->pDimension->getAxis();
+  if( v.x > center.x - ax && v.x < center.x + ax &&
+      v.z > center.z - ax && v.z < center.z + ax )
+    printf("THIS POINT IS INSIDE :) \n");
+  else
+    printf("POINT IS NOT INSIEDE\n");
+}
+
 /**
  *  draws the debug quadtree boxes around the model
Index: orxonox/trunk/src/lib/graphics/spatial_separation/quadtree_node.h
===================================================================
--- orxonox/trunk/src/lib/graphics/spatial_separation/quadtree_node.h	(revision 4919)
+++ orxonox/trunk/src/lib/graphics/spatial_separation/quadtree_node.h	(revision 4920)
@@ -31,4 +31,6 @@
     float getHeight(const Vector& position);
     Rectangle* getDimension() { return this->pDimension; }
+
+    bool includesPoint(const Vector& v);
 
     void drawTree(int depth, int drawMode) const;
