Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9517


Ignore:
Timestamp:
Dec 15, 2012, 2:41:57 PM (12 years ago)
Author:
jo
Message:

Little code cleanup.

Location:
code/branches/Racingbot/src/modules/gametypes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.cc

    r9513 r9517  
    4242#include "BulletCollision/CollisionShapes/btCollisionShape.h"
    4343
     44
    4445namespace orxonox
    4546{
     
    5758;        std::vector<RaceCheckPoint*> checkpoints;
    5859
    59         virtualCheckPointIndex=-2;
    60         for (ObjectList<SpaceRaceManager>::iterator it = ObjectList<SpaceRaceManager>::begin(); it!= ObjectList<SpaceRaceManager>::end(); ++it)
     60        virtualCheckPointIndex = -2;
     61        for (ObjectList<SpaceRaceManager>::iterator it = ObjectList<SpaceRaceManager>::begin(); it != ObjectList<SpaceRaceManager>::end(); ++it)
    6162        {
    6263            checkpoints = it->getAllCheckpoints();
    63             nextRaceCheckpoint_=it->findCheckpoint(0);
     64            nextRaceCheckpoint_ = it->findCheckpoint(0);
    6465        }
    6566
    6667        OrxAssert(!checkpoints.empty(), "No Checkpoints in Level");
    67         checkpoints_=checkpoints;
     68        checkpoints_ = checkpoints;
    6869        /*orxout()<<"es gibt: "<<checkpoints_.size()<<"checkpoints"<<endl;
    6970        for(std::vector<RaceCheckPoint*>::iterator it=checkpoints_.begin(); it!=checkpoints_.end(); it++)
     
    8687        }//ausgabe*/
    8788
    88         for( std::vector<RaceCheckPoint*>::iterator it = checkpoints.begin(); it!=checkpoints.end(); ++it)
     89        for (std::vector<RaceCheckPoint*>::iterator it = checkpoints.begin(); it != checkpoints.end(); ++it)
    8990        {
    9091            std::set<int> nextCheckPoints = ((*it)->getNextCheckpoints());
     
    124125
    125126        int i;
    126         for (i=-2; findCheckpoint(i)!= NULL; i--)
     127        for (i = -2; findCheckpoint(i) != NULL; i--)
    127128        {
    128129            continue;
     
    140141        XMLPortParam(ArtificialController, "accuracy", setAccuracy, getAccuracy, xmlelement, mode).defaultValues(100.0f);
    141142        XMLPortObject(ArtificialController, WorldEntity, "waypoints", addWaypoint, getWaypoint, xmlelement, mode);
    142 
    143143    }
    144144
     
    149149    std::vector<RaceCheckPoint*> SpaceRaceController::findStaticCheckpoints(std::vector<RaceCheckPoint*> allCheckpoints)
    150150    {
    151         std::map<RaceCheckPoint*, int> * zaehler = new std::map<RaceCheckPoint*, int>(); // counts how many times the checkpoit was reached (for simulation)
     151        std::map<RaceCheckPoint*, int> * zaehler = new std::map<RaceCheckPoint*, int>(); // counts how many times the checkpoint was reached (for simulation)
    152152        for (unsigned int i = 0; i < allCheckpoints.size(); i++)
    153153        {
     
    158158        std::vector<RaceCheckPoint*> returnVec;
    159159        returnVec.clear();
    160         for (std::map<RaceCheckPoint*, int>::iterator iter = zaehler->begin(); iter!= zaehler->end(); iter++)
     160        for (std::map<RaceCheckPoint*, int>::iterator iter = zaehler->begin(); iter != zaehler->end(); iter++)
    161161        {
    162162            if (iter->second == maxWays)
     
    188188            for (std::set<int>::iterator it = currentCheckpoint->getVirtualNextCheckpoints().begin(); it!= currentCheckpoint->getVirtualNextCheckpoints().end(); ++it)
    189189            {
    190                 if(currentCheckpoint==findCheckpoint(*it))
     190                if(currentCheckpoint == findCheckpoint(*it))
    191191                {
    192192                    //orxout() << currentCheckpoint->getCheckpointIndex()<<endl;
    193193                    continue;
    194194                }
    195                 if(findCheckpoint(*it)==NULL){orxout()<<"Problematic Point: "<<(*it)<<endl;}
     195                if(findCheckpoint(*it) == NULL)
     196                    {orxout()<<"Problematic Point: "<<(*it)<<endl;}
    196197                numberOfWays += rekSimulationCheckpointsReached(findCheckpoint(*it), zaehler);
    197198            }
     
    219220    RaceCheckPoint* SpaceRaceController::nextPointFind(RaceCheckPoint* raceCheckpoint)
    220221    {
    221         int distances[] =
    222         {   -1, -1, -1};
     222        int distances[] = {-1, -1, -1};
    223223        int temp_i = 0;
    224224        for (std::set<int>::iterator it =raceCheckpoint->getVirtualNextCheckpoints().begin(); it!= raceCheckpoint->getVirtualNextCheckpoints().end(); ++it)
     
    266266        {
    267267            int minimum = std::numeric_limits<int>::max();
    268             for (std::set<int>::iterator it = currentCheckPoint->getVirtualNextCheckpoints().begin(); it!= currentCheckPoint->getVirtualNextCheckpoints().end(); ++it)
     268            for (std::set<int>::iterator it = currentCheckPoint->getVirtualNextCheckpoints().begin(); it != currentCheckPoint->getVirtualNextCheckpoints().end(); ++it)
    269269            {
    270270                int dist_currentCheckPoint_currentPosition = static_cast<int> ((currentPosition- currentCheckPoint->getPosition()).length());
    271271
    272                 minimum= std::min(minimum, dist_currentCheckPoint_currentPosition + recCalculateDistance(findCheckpoint(*it), currentCheckPoint->getPosition()));
     272                minimum = std::min(minimum, dist_currentCheckPoint_currentPosition + recCalculateDistance(findCheckpoint(*it), currentCheckPoint->getPosition()));
    273273                // minimum of distanz from 'currentPosition' to the next static Checkpoint
    274274            }
     
    374374        {//Checkpoint erreicht
    375375
    376             currentRaceCheckpoint_=nextRaceCheckpoint_;
     376            currentRaceCheckpoint_ = nextRaceCheckpoint_;
    377377            OrxAssert(nextRaceCheckpoint_, "next race checkpoint undefined");
    378378            nextRaceCheckpoint_ = nextPointFind(nextRaceCheckpoint_);
    379             lastPositionSpaceship=this->getControllableEntity()->getPosition();
     379            lastPositionSpaceship = this->getControllableEntity()->getPosition();
    380380            //orxout()<< "CP "<< currentRaceCheckpoint_->getCheckpointIndex()<<" chanched to: "<< nextRaceCheckpoint_->getCheckpointIndex()<<endl;
    381381        }
     
    383383        {
    384384            nextRaceCheckpoint_ = adjustNextPoint();
    385             lastPositionSpaceship=this->getControllableEntity()->getPosition();
     385            lastPositionSpaceship = this->getControllableEntity()->getPosition();
    386386        }
    387387
    388388        // Abmessung fuer MINDISTANCE gut;
    389389
    390         else if((lastPositionSpaceship-this->getControllableEntity()->getPosition()).length()/dt< MINDISTANCE )
    391         {
    392             this->moveToPosition(Vector3(rnd()*100,rnd()*100,rnd()*100));
     390        else if((lastPositionSpaceship - this->getControllableEntity()->getPosition()).length()/dt < MINDISTANCE )
     391        {
     392            this->moveToPosition(Vector3(rnd()*100, rnd()*100, rnd()*100));
    393393            this->spin();
    394394            //orxout(user_status) << "Mindistance reached" << std::endl;
     
    396396        }
    397397        //orxout(user_status) << "dt= " << dt << ";  distance= " << (lastPositionSpaceship-this->getControllableEntity()->getPosition()).length() <<std::endl;
    398         lastPositionSpaceship=this->getControllableEntity()->getPosition();
     398        lastPositionSpaceship = this->getControllableEntity()->getPosition();
    399399        this->moveToPosition(nextRaceCheckpoint_->getPosition());
    400400    }
     
    403403    bool SpaceRaceController::vergleicheQuader(Vector3 pointToPoint, Vector3 groesse)
    404404    {
    405         if(abs(pointToPoint.x)<groesse.x)
    406         return true;
    407         if(abs(pointToPoint.y)<groesse.y)
    408         return true;
    409         if(abs(pointToPoint.z)<groesse.z)
    410         return true;
     405        if(abs(pointToPoint.x) < groesse.x)
     406            return true;
     407        if(abs(pointToPoint.y) < groesse.y)
     408            return true;
     409        if(abs(pointToPoint.z) < groesse.z)
     410            return true;
    411411
    412412    }
     
    415415    {
    416416
    417         Vector3 cP1ToCP2=(racepoint2->getPosition()-racepoint1->getPosition()) / (racepoint2->getPosition()-racepoint1->getPosition()).length(); //unit Vector
    418         Vector3 centerCP1=racepoint1->getPosition();
     417        Vector3 cP1ToCP2 = (racepoint2->getPosition() - racepoint1->getPosition()) / (racepoint2->getPosition() - racepoint1->getPosition()).length(); //unit Vector
     418        Vector3 centerCP1 = racepoint1->getPosition();
    419419        btVector3 positionObject;
    420420        btScalar radiusObject;
    421421
    422         for (std::vector<StaticEntity*>::iterator it = allObjects.begin(); it!=allObjects.end(); ++it)
    423         {
    424             for (int everyShape=0; (*it)->getAttachedCollisionShape(everyShape)!=0; everyShape++)
     422        for (std::vector<StaticEntity*>::iterator it = allObjects.begin(); it != allObjects.end(); ++it)
     423        {
     424            for (int everyShape=0; (*it)->getAttachedCollisionShape(everyShape) != 0; everyShape++)
    425425            {
    426426                btCollisionShape* currentShape = (*it)->getAttachedCollisionShape(everyShape)->getCollisionShape();
     
    441441    }
    442442
    443     void SpaceRaceController::computeVirtualCheckpoint(RaceCheckPoint* racepoint1, RaceCheckPoint* racepoint2,std::vector<StaticEntity*> allObjects)
     443    void SpaceRaceController::computeVirtualCheckpoint(RaceCheckPoint* racepoint1, RaceCheckPoint* racepoint2, std::vector<StaticEntity*> allObjects)
    444444    {
    445445        Vector3 cP1ToCP2=(racepoint2->getPosition()-racepoint1->getPosition()) / (racepoint2->getPosition()-racepoint1->getPosition()).length(); //unit Vector
     
    448448        btScalar radiusObject;
    449449
    450         for (std::vector<StaticEntity*>::iterator it = allObjects.begin(); it!=allObjects.end(); ++it)
    451         {
    452             for (int everyShape=0; (*it)->getAttachedCollisionShape(everyShape)!=0; everyShape++)
     450        for (std::vector<StaticEntity*>::iterator it = allObjects.begin(); it != allObjects.end(); ++it)
     451        {
     452            for (int everyShape=0; (*it)->getAttachedCollisionShape(everyShape) != 0; everyShape++)
    453453            {
    454454                btCollisionShape* currentShape = (*it)->getAttachedCollisionShape(everyShape)->getCollisionShape();
     
    458458                currentShape->getBoundingSphere(positionObject,radiusObject);
    459459                Vector3 positionObjectNonBT(positionObject.x(), positionObject.y(), positionObject.z());
    460                 if((powf((cP1ToCP2.dotProduct(centerCP1-positionObjectNonBT)),2)-(centerCP1-positionObjectNonBT).dotProduct(centerCP1-positionObjectNonBT)+powf(radiusObject, 2))>0)
    461                 {
    462                     Vector3 zufall;
    463                     Vector3 objectmiddle=positionObjectNonBT;
    464                     do
    465                     {
     460                                Vector3 norm_r_CP = cP1ToCP2.crossProduct(centerCP1-positionObjectNonBT);
     461
     462                                if(norm_r_CP.length == 0){
     463                                        Vector3 zufall;
     464                    do{
    466465                        zufall=Vector3(rnd(),rnd(),rnd());//random
    467                     }while((zufall.crossProduct(objectmiddle-racepoint1->getPosition())).length()==0);
    468 
    469                     Vector3 normalvec=zufall.crossProduct(objectmiddle-racepoint1->getPosition());
    470                     // a'/b'=a/b => a' =b'*a/b
    471                     float laengeNormalvec=(objectmiddle-racepoint1->getPosition()).length()/sqrt((objectmiddle-racepoint1->getPosition()).squaredLength()-radiusObject*radiusObject)*radiusObject;
    472                     RaceCheckPoint* newVirtualCheckpoint=addVirtualCheckPoint(racepoint1,racepoint2->getCheckpointIndex(), objectmiddle+normalvec/normalvec.length()*laengeNormalvec);
    473                     //placeVirtualCheckpoints(newVirtualCheckpoint, racepoint2);
    474                     return;
    475                 }
    476 
     466                    }while((zufall.crossProduct(cP1ToCP2)).length() == 0);
     467                                        norm_r_CP=zufall.crossProduct(cP1ToCP2);
     468                                }
     469                                Vector3 VecToVCP = norm_r_CP.crossProduct(cP1ToCP2);
     470                                float distanzToCP1 = sqrt(powf(radiusObject,4)/(powf((centerCP1-positionObjectNonBT).length(), 2)-powf(radiusObject,2))+powf(radiusObject,2));
     471                                float distanzToCP2 = sqrt(powf(radiusObject,4)/(powf((racepoint2->getPosition()-positionObjectNonBT).length(), 2)-powf(radiusObject,2))+powf(radiusObject,2));
     472                                //TODO float distanz=max(distanzToCP1,distanzToCP2);
     473                                float distanz = 0.0f; //TEMPORARY
     474                                Vector3 newCheckpointPositionPos = positionObjectNonBT+(distanz*VecToVCP)/VecToVCP.length();
     475                                Vector3 newCheckpointPositionNeg = positionObjectNonBT-(distanz*VecToVCP)/VecToVCP.length();
     476                                if((newCheckpointPositionPos-centerCP1).length+(newCheckpointPositionPos-(centerCP1+cP1ToCP2)).length < (newCheckpointPositionNeg-centerCP1).length+(newCheckpointPositionNeg-(centerCP1+cP1ToCP2)).length){
     477                                        RaceCheckPoint* newVirtualCheckpoint = addVirtualCheckPoint(racepoint1,racepoint2->getCheckpointIndex(), newCheckpointPositionPos);
     478                                }else{
     479                                        RaceCheckPoint* newVirtualCheckpoint = addVirtualCheckPoint(racepoint1,racepoint2->getCheckpointIndex(), newCheckpointPositionNeg);
     480                                }
     481                                return;
    477482            }
    478483        }
     
    489494        {
    490495
    491             if (dynamic_cast<RaceCheckPoint*>(*it)!=NULL)
     496            if (dynamic_cast<RaceCheckPoint*>(*it) != NULL)
    492497            {   continue;} // does not work jet
    493498
  • code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.h

    r9507 r9517  
    3232#include "gametypes/Gametype.h"
    3333#include "gametypes/RaceCheckPoint.h"
     34#include "util/Math.h"
    3435
    3536namespace orxonox
Note: See TracChangeset for help on using the changeset viewer.