Index: code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.cc
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.cc	(revision 9490)
+++ code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.cc	(revision 9507)
@@ -29,5 +29,4 @@
  * -staticCheckPoint= static Point (see def over = constructor)
  */
-
 
 /*TODO:
@@ -48,5 +47,5 @@
 
     const int ADJUSTDISTANCE = 500;
-    const int MINDISTANCE=5;
+    const int MINDISTANCE = 5;
     /*
      * Idea: Find static Point (checkpoints the spaceship has to reach)
@@ -55,6 +54,6 @@
         ArtificialController(creator)
     {
-        RegisterObject(SpaceRaceController);
-        std::vector<RaceCheckPoint*> checkpoints;
+        RegisterObject(SpaceRaceController)
+;        std::vector<RaceCheckPoint*> checkpoints;
         for (ObjectList<SpaceRaceManager>::iterator it = ObjectList<SpaceRaceManager>::begin(); it!= ObjectList<SpaceRaceManager>::end(); ++it)
         {
@@ -65,7 +64,16 @@
         OrxAssert(!checkpoints.empty(), "No Checkpoints in Level");
         checkpoints_=checkpoints;
-        for( std::vector<RaceCheckPoint*>::iterator it = checkpoints.begin(); it!=checkpoints.end(); ++it){
-            for (std::set<int>::iterator numb = ((*it)->getNextCheckpoints()).begin(); numb!=((*it)->getNextCheckpoints()).end(); ++numb)
-                placeVirtualCheckpoints((*it), findCheckpoint((*numb)));
+        for( std::vector<RaceCheckPoint*>::iterator it = checkpoints.begin(); it!=checkpoints.end(); ++it)
+        {
+            std::set<int> nextCheckPoints = ((*it)->getNextCheckpoints());
+            if(!nextCheckPoints.empty()) {
+                orxout() << "yay" << endl;
+                for (std::set<int>::iterator numb = nextCheckPoints.begin(); numb!=nextCheckPoints.end(); numb++) {
+                    RaceCheckPoint* point2 = findCheckpoint((*numb));
+
+                    if(point2 != NULL)
+                        placeVirtualCheckpoints((*it), point2);
+                }
+            }
         }
         staticRacePoints_ = findStaticCheckpoints(checkpoints);
@@ -81,7 +89,6 @@
          }*/
 
-        virtualCheckPointIndex=-1;
-    }
-
+        virtualCheckPointIndex=-2;
+    }
 
     //------------------------------
@@ -130,6 +137,8 @@
     int SpaceRaceController::rekSimulationCheckpointsReached(RaceCheckPoint* currentCheckpoint, std::map<RaceCheckPoint*, int>* zaehler)
     {
+
         if (currentCheckpoint->isLast())
         {// last point reached
+            orxout() << "last one" << endl;
             (*zaehler)[currentCheckpoint] += 1;
             return 1; // 1 Way form the last point to this one
@@ -140,4 +149,12 @@
             for (std::set<int>::iterator it = currentCheckpoint->getNextCheckpoints().begin(); it!= currentCheckpoint->getNextCheckpoints().end(); ++it)
             {
+                if(currentCheckpoint==findCheckpoint(*it)){
+                    orxout() << currentCheckpoint->getCheckpointIndex()<<endl;
+                    continue;
+                }
+                for (std::set<int>::iterator a = currentCheckpoint->getNextCheckpoints().begin(); a!= currentCheckpoint->getNextCheckpoints().end(); ++a){
+                    orxout() << "Nextcheckpoints: "<<(*a) << endl;
+                }
+                orxout() << "currentCheck; " << currentCheckpoint->getCheckpointIndex() << "findCheck; " << findCheckpoint(*it)->getCheckpointIndex() << endl;
                 numberOfWays += rekSimulationCheckpointsReached(findCheckpoint(*it), zaehler);
             }
@@ -146,6 +163,4 @@
         }
     }
-
-
 
     //-------------------------------------
@@ -167,5 +182,6 @@
     RaceCheckPoint* SpaceRaceController::nextPointFind(RaceCheckPoint* raceCheckpoint)
     {
-        int distances[] = {   -1, -1, -1};
+        int distances[] =
+        {   -1, -1, -1};
         int temp_i = 0;
         for (std::set<int>::iterator it =raceCheckpoint->getNextCheckpoints().begin(); it!= raceCheckpoint->getNextCheckpoints().end(); ++it)
@@ -246,14 +262,17 @@
 
     RaceCheckPoint* SpaceRaceController::findCheckpoint(int index) const
-       {
-           for (size_t i = 0; i < this->checkpoints_.size(); ++i)
-           if (this->checkpoints_[i]->getCheckpointIndex() == index)
-           return this->checkpoints_[i];
-           return NULL;
-       }
-
-    RaceCheckPoint* SpaceRaceController::addVirtualCheckPoint( RaceCheckPoint* previousCheckpoint, int indexFollowingCheckPoint , Vector3 virtualCheckPointPosition ){
-
-        RaceCheckPoint* newTempRaceCheckPoint = new RaceCheckPoint(this);
+    {
+        for (size_t i = 0; i < this->checkpoints_.size(); ++i)
+        if (this->checkpoints_[i]->getCheckpointIndex() == index)
+        return this->checkpoints_[i];
+        return NULL;
+    }
+
+    RaceCheckPoint* SpaceRaceController::addVirtualCheckPoint( RaceCheckPoint* previousCheckpoint, int indexFollowingCheckPoint , Vector3 virtualCheckPointPosition )
+    {
+        RaceCheckPoint* newTempRaceCheckPoint;
+        for (ObjectList<SpaceRaceManager>::iterator it = ObjectList<SpaceRaceManager>::begin(); it!= ObjectList<SpaceRaceManager>::end(); ++it){
+            newTempRaceCheckPoint = new RaceCheckPoint((*it));
+        }
         newTempRaceCheckPoint->setPosition(virtualCheckPointPosition);
         newTempRaceCheckPoint->setCheckpointIndex(virtualCheckPointIndex);
@@ -264,9 +283,11 @@
         checkpoints_.insert(checkpoints_.end(), newTempRaceCheckPoint);
         int positionInNextCheckPoint;
-        for (int i = 0; i <3 ; i++){
+        for (int i = 0; i <3; i++)
+        {
             if(previousCheckpoint->getNextCheckpointsAsVector3()[i]==indexFollowingCheckPoint)
-                positionInNextCheckPoint=i;
-        }
-        switch(positionInNextCheckPoint){
+            positionInNextCheckPoint=i;
+        }
+        switch(positionInNextCheckPoint)
+        {
             case 0: temp.x=virtualCheckPointIndex; break;
             case 1: temp.y=virtualCheckPointIndex; break;
@@ -275,12 +296,12 @@
         previousCheckpoint->setNextCheckpointsAsVector3(temp);
         virtualCheckPointIndex--;
+        OrxAssert(virtualCheckPointIndex < -1, "TO much virtual cp");
         return newTempRaceCheckPoint;
     }
 
-
-
     SpaceRaceController::~SpaceRaceController()
     {
-        for (int i =-1; i>virtualCheckPointIndex ; i--){
+        for (int i =-1; i>virtualCheckPointIndex; i--)
+        {
             delete findCheckpoint(i);
         }
@@ -292,6 +313,8 @@
         {   orxout()<<this->getControllableEntity()<< " in tick"<<endl; return;}
         //FOR virtual Checkpoints
-        if(nextRaceCheckpoint_->getCheckpointIndex() < 0){
-            if( distanceSpaceshipToCheckPoint(nextRaceCheckpoint_) < 30){
+        if(nextRaceCheckpoint_->getCheckpointIndex() < 0)
+        {
+            if( distanceSpaceshipToCheckPoint(nextRaceCheckpoint_) < 30)
+            {
                 currentRaceCheckpoint_=nextRaceCheckpoint_;
                 nextRaceCheckpoint_ = nextPointFind(nextRaceCheckpoint_);
@@ -313,5 +336,7 @@
         }
         //TODO: korrigieren!
-        else if((lastPositionSpaceship-this->getControllableEntity()->getPosition()).length()/dt< MINDISTANCE  ){
+
+        else if((lastPositionSpaceship-this->getControllableEntity()->getPosition()).length()/dt< MINDISTANCE )
+        {
             this->moveToPosition(Vector3(rnd()*100,rnd()*100,rnd()*100));
             this->spin();
@@ -325,15 +350,79 @@
 
     // True if a coordinate of 'pointToPoint' is smaller then the corresponding coordinate of 'groesse'
-    bool SpaceRaceController::vergleicheQuader(Vector3 pointToPoint, Vector3 groesse){
+    bool SpaceRaceController::vergleicheQuader(Vector3 pointToPoint, Vector3 groesse)
+    {
         if(abs(pointToPoint.x)<groesse.x)
-            return true;
+        return true;
         if(abs(pointToPoint.y)<groesse.y)
-                    return true;
+        return true;
         if(abs(pointToPoint.z)<groesse.z)
-                    return true;
-
-    }
-
-    void SpaceRaceController::placeVirtualCheckpoints(RaceCheckPoint* racepoint1, RaceCheckPoint* racepoint2){
+        return true;
+
+    }
+
+    bool SpaceRaceController::directLinePossible(RaceCheckPoint* racepoint1, RaceCheckPoint* racepoint2,std::vector<StaticEntity*> allObjects){
+
+        Vector3 cP1ToCP2=(racepoint2->getPosition()-racepoint1->getPosition()) / (racepoint2->getPosition()-racepoint1->getPosition()).length(); //unit Vector
+        Vector3 centerCP1=racepoint1->getPosition();
+        btVector3 positionObject;
+        btScalar radiusObject;
+
+        for (std::vector<StaticEntity*>::iterator it = allObjects.begin(); it!=allObjects.end(); ++it){
+            for (int everyShape=0; (*it)->getAttachedCollisionShape(everyShape)!=0; everyShape++){
+                btCollisionShape* currentShape = (*it)->getAttachedCollisionShape(everyShape)->getCollisionShape();
+                if(currentShape == NULL)
+                    continue;
+
+                currentShape->getBoundingSphere(positionObject,radiusObject);
+                Vector3 positionObjectNonBT(positionObject.x(), positionObject.y(), positionObject.z());
+                if((powf((cP1ToCP2.dotProduct(centerCP1-positionObjectNonBT)),2)-(centerCP1-positionObjectNonBT).dotProduct(centerCP1-positionObjectNonBT)+powf(radiusObject, 2))>0){
+                    return false;
+                }
+
+            }
+        }
+        return true;
+
+    }
+
+    void SpaceRaceController::computeVirtualCheckpoint(RaceCheckPoint* racepoint1, RaceCheckPoint* racepoint2,std::vector<StaticEntity*> allObjects){
+                Vector3 cP1ToCP2=(racepoint2->getPosition()-racepoint1->getPosition()) / (racepoint2->getPosition()-racepoint1->getPosition()).length(); //unit Vector
+                Vector3 centerCP1=racepoint1->getPosition();
+                btVector3 positionObject;
+                btScalar radiusObject;
+
+                for (std::vector<StaticEntity*>::iterator it = allObjects.begin(); it!=allObjects.end(); ++it){
+                    for (int everyShape=0; (*it)->getAttachedCollisionShape(everyShape)!=0; everyShape++){
+                        btCollisionShape* currentShape = (*it)->getAttachedCollisionShape(everyShape)->getCollisionShape();
+                        if(currentShape == NULL)
+                            continue;
+
+                        currentShape->getBoundingSphere(positionObject,radiusObject);
+                        Vector3 positionObjectNonBT(positionObject.x(), positionObject.y(), positionObject.z());
+                        if((powf((cP1ToCP2.dotProduct(centerCP1-positionObjectNonBT)),2)-(centerCP1-positionObjectNonBT).dotProduct(centerCP1-positionObjectNonBT)+powf(radiusObject, 2))>0){
+                            Vector3 zufall;
+                            Vector3 objectmiddle=positionObjectNonBT;
+                            do{
+                                      zufall=Vector3(rnd(),rnd(),rnd());//random
+                            }while((zufall.crossProduct(objectmiddle-racepoint1->getPosition())).length()==0);
+
+
+                            Vector3 normalvec=zufall.crossProduct(objectmiddle-racepoint1->getPosition());
+                            // a'/b'=a/b => a' =b'*a/b
+                            float laengeNormalvec=(objectmiddle-racepoint1->getPosition()).length()/sqrt((objectmiddle-racepoint1->getPosition()).squaredLength()-radiusObject*radiusObject)*radiusObject;
+                            RaceCheckPoint* newVirtualCheckpoint=addVirtualCheckPoint(racepoint1,racepoint2->getCheckpointIndex(), objectmiddle+normalvec/normalvec.length()*laengeNormalvec);
+                            //placeVirtualCheckpoints(newVirtualCheckpoint, racepoint2);
+                            return;
+                        }
+
+                    }
+                }
+
+    }
+
+
+
+    void SpaceRaceController::placeVirtualCheckpoints(RaceCheckPoint* racepoint1, RaceCheckPoint* racepoint2)
+    {
         Vector3 point1 = racepoint1->getPosition();
         Vector3 point2 = racepoint2->getPosition();
@@ -341,68 +430,104 @@
 
         for (ObjectList<StaticEntity>::iterator it = ObjectList<StaticEntity>::begin(); it!= ObjectList<StaticEntity>::end(); ++it)
-                        {
-
-                            if (dynamic_cast<RaceCheckPoint*>(*it)!=NULL){continue;} // does not work jet
-
-                            problematicObjects.insert(problematicObjects.end(), *it);
-                            //it->getScale3D();// vector fuer halbe wuerfellaenge
-                        }
-        Vector3 richtungen [6];
-        richtungen[0]= Vector3(1,0,0);
-        richtungen[1]= Vector3(-1,0,0);
-        richtungen[2]= Vector3(0,1,0);
-        richtungen[3]= Vector3(0,-1,0);
-        richtungen[4]= Vector3(0,0,1);
-        richtungen[5]= Vector3(0,0,-1);
-
-        for (int i = 0; i< 6; i++){
-            const int STEPS=100;
-            const float PHI=1.1;
-            bool collision=false;
-
-            for (int j =0; j<STEPS; j++){
-                Vector3 tempPosition=(point1 - (point2-point1+richtungen[i]*PHI)*(float)j/STEPS);
-                for (std::vector<StaticEntity*>::iterator it = problematicObjects.begin(); it!=problematicObjects.end(); ++it){
-                    btVector3 positionObject;
-                    btScalar radiusObject;
-                    //TODO: Probably it points on a wrong object
-                    for (int everyShape=0; (*it)->getAttachedCollisionShape(everyShape)!=0; everyShape++){
-                        (*it)->getAttachedCollisionShape(everyShape)->getCollisionShape()->getBoundingSphere(positionObject,radiusObject);
-                        Vector3 positionObjectNonBT(positionObject.x(), positionObject.y(), positionObject.z());
-                        if (((tempPosition - positionObjectNonBT).length()<radiusObject) && (vergleicheQuader((tempPosition-positionObjectNonBT),(*it)->getScale3D()))){
-                            collision=true; break;
-                        }
-                    }
-                    if(collision) break;
-                }
-                if(collision)break;
-            }
-            if(collision) continue;
-            // no collision => possible Way
-            for (float j =0; j<STEPS; j++){
-                Vector3 possiblePosition=(point1 - (point2-point1+richtungen[i]*PHI)*j/STEPS);
-                collision=false;
-                for(int ij=0; ij<STEPS; j++){
-                    Vector3 tempPosition=(possiblePosition - (point2-possiblePosition)*(float)ij/STEPS);
-                            for (std::vector<StaticEntity*>::iterator it = problematicObjects.begin(); it!=problematicObjects.end(); ++it){
-                                btVector3 positionObject;
-                                btScalar radiusObject;
-                                for (int everyShape=0; (*it)->getAttachedCollisionShape(everyShape)!=0; everyShape++){
-                                    (*it)->getAttachedCollisionShape(everyShape)->getCollisionShape()->getBoundingSphere(positionObject,radiusObject);
-                                    Vector3 positionObjectNonBT(positionObject.x(), positionObject.y(), positionObject.z());
-                                    if (((tempPosition-positionObjectNonBT).length()<radiusObject) && (vergleicheQuader((tempPosition-positionObjectNonBT),(*it)->getScale3D()))){
-                                        collision=true; break;
-                                    }
-                                }
-                            if(collision) break;
-                            }
-                    if(collision)break;
-                    addVirtualCheckPoint(racepoint1, racepoint2->getCheckpointIndex(), possiblePosition);
-                    return;
-                }
-
-            }
-        }
-
+        {
+
+            if (dynamic_cast<RaceCheckPoint*>(*it)!=NULL)
+            {   continue;} // does not work jet
+
+            problematicObjects.insert(problematicObjects.end(), *it);
+            //it->getScale3D();// vector fuer halbe wuerfellaenge
+        }
+
+        if(!directLinePossible(racepoint1, racepoint2, problematicObjects)) {
+            computeVirtualCheckpoint(racepoint1, racepoint2, problematicObjects);
+        }
+
+
+//
+//        do{
+//            zufall=Vector3(rnd(),rnd(),rnd());//random
+//        }while((zufall.crossProduct(objectmiddle-racepoint1->getPosition())).length()==0);
+//
+//        Vector3 normalvec=zufall.crossProduct(objectmiddle-racepoint1->getPosition());
+//        // a'/b'=a/b => a' =b'*a/b
+//        float laengeNormalvec=(objectmiddle-racepoint1->getPosition()).length()/sqrt((objectmiddle-racepoint1->getPosition()).squaredLength()-radius*radius)*radius;
+//        addVirtualCheckPoint(racepoint1,racepoint2->getCheckpointIndex(), objectmiddle+normalvec/normalvec.length()*laengeNormalvec);
+
+//        Vector3 richtungen [6];
+//        richtungen[0]= Vector3(1,0,0);
+//        richtungen[1]= Vector3(-1,0,0);
+//        richtungen[2]= Vector3(0,1,0);
+//        richtungen[3]= Vector3(0,-1,0);
+//        richtungen[4]= Vector3(0,0,1);
+//        richtungen[5]= Vector3(0,0,-1);
+//
+//        for (int i = 0; i< 6; i++)
+//        {
+//            const int STEPS=100;
+//            const float PHI=1.1;
+//            bool collision=false;
+//
+//            for (int j =0; j<STEPS; j++)
+//            {
+//                Vector3 tempPosition=(point1 - (point2-point1+richtungen[i]*PHI)*(float)j/STEPS);
+//                for (std::vector<StaticEntity*>::iterator it = problematicObjects.begin(); it!=problematicObjects.end(); ++it)
+//                {
+//                    btVector3 positionObject;
+//                    btScalar radiusObject;
+//                    if((*it)==NULL)
+//                    {   orxout()<<"Problempoint 1.1"<<endl; continue;}
+//                    //TODO: Probably it points on a wrong object
+//                    for (int everyShape=0; (*it)->getAttachedCollisionShape(everyShape)!=0; everyShape++)
+//                    {
+//                        if((*it)->getAttachedCollisionShape(everyShape)->getCollisionShape()==NULL)
+//                        {    continue;}
+//
+//                        orxout()<<"Problempoint 2.1"<<endl;
+//                        (*it)->getAttachedCollisionShape(everyShape)->getCollisionShape()->getBoundingSphere(positionObject,radiusObject);
+//                        Vector3 positionObjectNonBT(positionObject.x(), positionObject.y(), positionObject.z());
+//                        if (((tempPosition - positionObjectNonBT).length()<radiusObject) && (vergleicheQuader((tempPosition-positionObjectNonBT),(*it)->getScale3D())))
+//                        {
+//                            collision=true; break;
+//                        }
+//                    }
+//                    if(collision) break;
+//                }
+//                if(collision)break;
+//            }
+//            if(collision) continue;
+//            // no collision => possible Way
+//            for (float j =0; j<STEPS; j++)
+//            {
+//                Vector3 possiblePosition=(point1 - (point2-point1+richtungen[i]*PHI)*j/STEPS);
+//                collision=false;
+//                for(int ij=0; ij<STEPS; j++)
+//                {
+//                    Vector3 tempPosition=(possiblePosition - (point2-possiblePosition)*(float)ij/STEPS);
+//                    for (std::vector<StaticEntity*>::iterator it = problematicObjects.begin(); it!=problematicObjects.end(); ++it)
+//                    {
+//                        btVector3 positionObject;
+//                        btScalar radiusObject;
+//                        if((*it)==NULL)
+//                        {   orxout()<<"Problempoint 1"<<endl; continue;}
+//                        for (int everyShape=0; (*it)->getAttachedCollisionShape(everyShape)!=0; everyShape++)
+//                        {
+//                            if((*it)->getAttachedCollisionShape(everyShape)->getCollisionShape()==NULL)
+//                            {   orxout()<<"Problempoint 2.2"<<endl; continue;}
+//                            (*it)->getAttachedCollisionShape(everyShape)->getCollisionShape()->getBoundingSphere(positionObject,radiusObject);
+//                            Vector3 positionObjectNonBT(positionObject.x(), positionObject.y(), positionObject.z());
+//                            if (((tempPosition-positionObjectNonBT).length()<radiusObject) && (vergleicheQuader((tempPosition-positionObjectNonBT),(*it)->getScale3D())))
+//                            {
+//                                collision=true; break;
+//                            }
+//                        }
+//                        if(collision) break;
+//                    }
+//                    if(collision)break;
+//                    //addVirtualCheckPoint(racepoint1, racepoint2->getCheckpointIndex(), possiblePosition);
+//                    return;
+//                }
+//
+//            }
+//        }
 
     }
Index: code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.h
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.h	(revision 9490)
+++ code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.h	(revision 9507)
@@ -50,12 +50,17 @@
             RaceCheckPoint* nextPointFind(RaceCheckPoint*);
             RaceCheckPoint* adjustNextPoint();
-            std::vector<RaceCheckPoint*> findStaticCheckpoints(std::vector<RaceCheckPoint*>);
+            std::vector<RaceCheckPoint*> findStaticCheckpoints(std::vector<
+                    RaceCheckPoint*>);
             std::vector<RaceCheckPoint*> staticCheckpoints();
-            int rekSimulationCheckpointsReached(RaceCheckPoint*, std::map<RaceCheckPoint*, int>*);
+            int rekSimulationCheckpointsReached(RaceCheckPoint*, std::map<
+                    RaceCheckPoint*, int>*);
             // same as SpaceRaceManager, but needed to add virtuell Checkpoints ( Checkpoints which don't exist but needed to avoid collisions with big Objects)
             RaceCheckPoint* findCheckpoint(int index) const;
-            RaceCheckPoint* addVirtualCheckPoint(RaceCheckPoint*, int , Vector3);
-            void placeVirtualCheckpoints(RaceCheckPoint*,RaceCheckPoint*);
+            RaceCheckPoint
+                    * addVirtualCheckPoint(RaceCheckPoint*, int , Vector3);
+            void placeVirtualCheckpoints(RaceCheckPoint*, RaceCheckPoint*);
             bool vergleicheQuader(Vector3, Vector3);
+            bool directLinePossible(RaceCheckPoint*, RaceCheckPoint*, std::vector<StaticEntity*>);
+            void computeVirtualCheckpoint(RaceCheckPoint*, RaceCheckPoint*, std::vector<StaticEntity*>);
 
         public:
