Index: orxonox/trunk/src/lib/graphics/spatial_separation/quadtree.cc
===================================================================
--- orxonox/trunk/src/lib/graphics/spatial_separation/quadtree.cc	(revision 4921)
+++ orxonox/trunk/src/lib/graphics/spatial_separation/quadtree.cc	(revision 4922)
@@ -64,4 +64,15 @@
      printf("node %i, %f, %f \n", i, this->nodes[i]->getDimension()->getCenter()->x, this->nodes[i]->getDimension()->getCenter()->z);
    }
+
+   this->quadLength = this->nodes[0]->getDimension()->getAxis() * 2.0f;
+   Rectangle* r = this->rootNode->getDimension();
+
+   Vector* offset = new Vector();
+   float xOff = r->getCenter()->x - r->getAxis();
+   float yOff = r->getCenter()->z - r->getAxis();
+   this->offset->x = xOff;
+   this->offset->z = yOff;
+
+   this->maxIndex = (int)pow(2, this->treeDepth);
 }
 
@@ -76,8 +87,11 @@
   delete [] this->nodes;
   delete this->rootNode;
+  delete offset;
 }
 
 
 /**
+  \brief this function rotates the array and mirrors it in the middle
+  \param nodes: the nodes to translate
 
   since the original matrix is counted from the right upper edge to the right lower one, we have to reorganize the elements
@@ -108,16 +122,21 @@
 }
 
+/**
+  \brief sorts the hash table using their positions
+  \param nodes the nodes to use
 
+ */
 void Quadtree::sortHashTable(QuadtreeNode** nodes)
 {
   int                  len         = (int)pow(2, this->treeDepth);          //!< the length of a quadtree side
-  float a,b;
-  QuadtreeNode* tmpNode;
+  float                a;                                                   //!< temp place for float a
+  float                b;                                                   //!< temp place for float b
+  QuadtreeNode*        tmpNode;                                             //!< tmp place for a QuadtreeNode
 
-  for(int i = 0; i < len; ++i)
+  for( int i = 0; i < len; ++i)
   {
-    for(int j = 0; j < len; ++j)
+    for( int j = 0; j < len; ++j)
     {
-      for(int k = j + 1; k < len; ++k)
+      for( int k = j + 1; k < len; ++k)
       {
         a = this->nodes[i * len + j]->getDimension()->getCenter()->z;
@@ -131,41 +150,30 @@
         }
       }
+    }
+  }
 
-    }
-
-  }
 }
 
 
+/**
+  \brief maps a position to a quadtree
+  \param position the position so look for
+  \return the quadtree
 
+  this function will return the quadtree that contains the position
+ */
 QuadtreeNode* Quadtree::getQuadtreeFromPosition(const Vector& position)
 {
-  Vector v = position;
-  Rectangle* r = this->rootNode->getDimension();
-  float len = this->nodes[0]->getDimension()->getAxis() * 2.0f;
+  /* shift into model space */
+  Vector v = position - *this->offset;
+  /* map */
+  int i = (int)(v.x / quadLength);
+  int j = (int)(v.z / quadLength);
 
-  Vector edge;
-  float xOff = r->getCenter()->x - r->getAxis();
-  float yOff = r->getCenter()->z - r->getAxis();
-  edge.x = xOff, edge.z = yOff;
-
-  /* shift into model space */
-  v = position - edge;
-  /* map */
-  int i = (int)(v.x / len);
-  int j = (int)(v.z / len);
-
-  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]->draw();
-    this->nodes[i + j * max]->includesPoint(position);
-  }
+  /* check if object is still inside the terrain */
+  if( i < this->maxIndex && j < this->maxIndex)
+    this->nodes[i + j * this->maxIndex]->includesPoint(position);
   else
-    printf("object has left terrain\n");
+    PRINTF(0)("Object has left terrain\n");
 }
 
@@ -174,12 +182,10 @@
  *  draws the debug quadtree boxes around the model
  */
-void Quadtree::drawTree(int depth, int drawMode) const
+void Quadtree::drawTree() const
 {
-  //this->rootNode->drawTree(depth, drawMode);
-  for(int i = 0; i < (int)pow(4, this->treeDepth); ++i)
-  {
-    this->nodes[i]->draw();
-  }
-
-
+  this->rootNode->drawTree();
+//   for(int i = 0; i < (int)pow(4, this->treeDepth); ++i)
+//   {
+//     this->nodes[i]->draw();
+//   }
 }
Index: orxonox/trunk/src/lib/graphics/spatial_separation/quadtree.h
===================================================================
--- orxonox/trunk/src/lib/graphics/spatial_separation/quadtree.h	(revision 4921)
+++ orxonox/trunk/src/lib/graphics/spatial_separation/quadtree.h	(revision 4922)
@@ -26,5 +26,5 @@
   QuadtreeNode* getQuadtreeFromPosition(const Vector& position);
 
-  void drawTree(int depth, int drawMode) const;
+  void drawTree() const;
   inline Material* getMaterial(int indexNode) const { return this->materials[indexNode % 4]; }
 
@@ -38,4 +38,8 @@
    int                             treeDepth;             //!< depth of the tree
 
+   float                           quadLength;            //!< length of the leaf quadtree nodes
+   Vector*                         offset;                //!< vector to the left lower corner of the root quadtree node
+   int                             maxIndex;              //!< maximal index for the nodes array
+
    Material**                      materials;             //!< materials for debug drawing purposes
 
Index: orxonox/trunk/src/lib/graphics/spatial_separation/quadtree_node.cc
===================================================================
--- orxonox/trunk/src/lib/graphics/spatial_separation/quadtree_node.cc	(revision 4921)
+++ orxonox/trunk/src/lib/graphics/spatial_separation/quadtree_node.cc	(revision 4922)
@@ -135,4 +135,7 @@
 
 
+/**
+
+ */
 void QuadtreeNode::buildHashTable(QuadtreeNode** nodeList, int* index)
 {
@@ -362,16 +365,19 @@
   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");
-  this->bDraw = true;
-}
+  {
+    this->bDraw = true;
+    return true;
+  }
+  return false;
+}
+
 
 /**
  *  draws the debug quadtree boxes around the model
  */
-void QuadtreeNode::drawTree(int depth, int drawMode) const
+void QuadtreeNode::drawTree() const
 {
   if( this->treeDepth == this->maxDepth)
@@ -381,12 +387,10 @@
     float h = 50.0f;
 
-    //printf("draw @ %f, %f to %f, %f", t1.x + ax, t1.z + ax, t1.x - ax, t1.z - ax);
-
     this->quadtree->getMaterial(this->indexNode)->select();
     glBegin(GL_QUADS);
-    glVertex3f(t1.x + ax, h - depth * 10.0f, t1.z + ax);
-    glVertex3f(t1.x - ax, h - depth * 10.0f, t1.z + ax);
-    glVertex3f(t1.x - ax, h - depth * 10.0f, t1.z - ax);
-    glVertex3f(t1.x + ax, h - depth * 10.0f, t1.z - ax);
+    glVertex3f(t1.x + ax, h - this->treeDepth * 10.0f, t1.z + ax);
+    glVertex3f(t1.x - ax, h - this->treeDepth * 10.0f, t1.z + ax);
+    glVertex3f(t1.x - ax, h - this->treeDepth * 10.0f, t1.z - ax);
+    glVertex3f(t1.x + ax, h - this->treeDepth * 10.0f, t1.z - ax);
     glEnd();
   }
@@ -394,12 +398,13 @@
 
   if( this->nodeA != NULL)
-    this->nodeA->drawTree(depth - 1, drawMode);
+    this->nodeA->drawTree();
   if( this->nodeB != NULL)
-    this->nodeB->drawTree(depth - 1, drawMode);
+    this->nodeB->drawTree();
   if( this->nodeC != NULL)
-    this->nodeC->drawTree(depth - 1, drawMode);
+    this->nodeC->drawTree();
   if( this->nodeD != NULL)
-    this->nodeD->drawTree(depth - 1, drawMode);
-}
+    this->nodeD->drawTree();
+}
+
 
 void QuadtreeNode::draw() const
@@ -411,16 +416,12 @@
   float h = 70.0f;
 
-    //printf("draw @ %f, %f to %f, %f", t1.x + ax, t1.z + ax, t1.x - ax, t1.z - ax);
-
   glBegin(GL_QUADS);
   this->quadtree->getMaterial(this->indexNode)->select();
-//   for( int i = 0; i < 50; ++i)
-//   {
-  int i = 0;
-    glVertex3f(t1.x + ax, h + 10.0f * i, t1.z + ax);
-    glVertex3f(t1.x - ax, h + 10.0f * i, t1.z + ax);
-    glVertex3f(t1.x - ax, h + 10.0f * i, t1.z - ax);
-    glVertex3f(t1.x + ax, h + 10.0f * i, t1.z - ax);
-//   }
+
+  glVertex3f(t1.x + ax, h, t1.z + ax);
+  glVertex3f(t1.x - ax, h, t1.z + ax);
+  glVertex3f(t1.x - ax, h, t1.z - ax);
+  glVertex3f(t1.x + ax, h, t1.z - ax);
+
   glEnd();
 
Index: orxonox/trunk/src/lib/graphics/spatial_separation/quadtree_node.h
===================================================================
--- orxonox/trunk/src/lib/graphics/spatial_separation/quadtree_node.h	(revision 4921)
+++ orxonox/trunk/src/lib/graphics/spatial_separation/quadtree_node.h	(revision 4922)
@@ -34,5 +34,5 @@
     bool includesPoint(const Vector& v);
 
-    void drawTree(int depth, int drawMode) const;
+    void drawTree() const;
     void draw() const;
 
Index: orxonox/trunk/src/lib/graphics/spatial_separation/spatial_separation.cc
===================================================================
--- orxonox/trunk/src/lib/graphics/spatial_separation/spatial_separation.cc	(revision 4921)
+++ orxonox/trunk/src/lib/graphics/spatial_separation/spatial_separation.cc	(revision 4922)
@@ -99,13 +99,8 @@
 Quadtree* SpatialSeparation::createQuadtree(AbstractModel* model)
 {
-  this->quadtree = new Quadtree(model->getModelInfo(), 5);
+  this->quadtree = new Quadtree(model->getModelInfo(), 4);
 
   return this->quadtree;
 }
-
-
-
-
-
 
 
@@ -116,5 +111,5 @@
     return;
 
-  this->quadtree->drawTree(4, 0);
+  this->quadtree->drawTree();
 }
 
