Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9965


Ignore:
Timestamp:
Jan 3, 2014, 9:57:52 PM (11 years ago)
Author:
landauf
Message:

simplified algorithm. turns out this code actually tries to find the minimal distance (but it only worked with max 3 possible next checkpoints)

File:
1 edited

Legend:

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

    r9964 r9965  
    222222    RaceCheckPoint* SpaceRaceController::nextPointFind(RaceCheckPoint* raceCheckpoint)
    223223    {
    224         float distances[] = {-1, -1, -1};
    225         int temp_i = 0;
    226         for (std::set<int>::iterator it =raceCheckpoint->getNextCheckpoints().begin(); it!= raceCheckpoint->getNextCheckpoints().end(); ++it)
    227         {
    228             distances[temp_i] = recCalculateDistance(findCheckpoint(*it), this->getControllableEntity()->getPosition());
    229             temp_i++;
    230         }
    231         if (distances[0] > distances[1] && distances[1] >= 0)
    232         {
    233             if (distances[2] < distances[1] && distances[2] >= 0)
    234             {
    235                 return findCheckpoint(*raceCheckpoint->getNextCheckpoints().end()); // return checkpoint with ID of raceCheckpoint->getNextCheckpoints() [2]
    236             }
    237             else
    238             {
    239                 std::set<int>::iterator temp = raceCheckpoint->getNextCheckpoints().begin();
    240                 return findCheckpoint(*(++temp)); // return [1]
    241             }
    242         }
    243         else
    244         {
    245             if (distances[2] < distances[0] && distances[2] >= 0)
    246             {
    247                 return findCheckpoint(*raceCheckpoint->getNextCheckpoints().end()); // return [2]
    248             }
    249             else
    250             {
    251                 return findCheckpoint(*raceCheckpoint->getNextCheckpoints().begin()); // return [0]
    252             }
    253         }
     224        float minDistance = 0;
     225        RaceCheckPoint* minNextRaceCheckPoint = NULL;
     226
     227        // find the next checkpoint with the minimal distance
     228        for (std::set<int>::iterator it = raceCheckpoint->getNextCheckpoints().begin(); it != raceCheckpoint->getNextCheckpoints().end(); ++it)
     229        {
     230            RaceCheckPoint* nextRaceCheckPoint = findCheckpoint(*it);
     231            float distance = recCalculateDistance(nextRaceCheckPoint, this->getControllableEntity()->getPosition());
     232
     233            if (distance < minDistance || minNextRaceCheckPoint == NULL)
     234            {
     235                minDistance = distance;
     236                minNextRaceCheckPoint = nextRaceCheckPoint;
     237            }
     238        }
     239
     240        return minNextRaceCheckPoint;
    254241    }
    255242
Note: See TracChangeset for help on using the changeset viewer.