[9399] | 1 | /* |
---|
[9412] | 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
[9399] | 4 | * |
---|
[9412] | 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
[9399] | 22 | * Created on: Oct 8, 2012 |
---|
| 23 | * Author: purgham |
---|
| 24 | */ |
---|
[9459] | 25 | |
---|
| 26 | /** |
---|
| 27 | * Conventions: |
---|
| 28 | * -first Checkpoint has index 0 |
---|
| 29 | * -staticCheckPoint= static Point (see def over = constructor) |
---|
| 30 | */ |
---|
| 31 | |
---|
| 32 | /*TODO: |
---|
| 33 | * tICK KORRIGIEREN |
---|
| 34 | * |
---|
| 35 | * |
---|
| 36 | */ |
---|
[9399] | 37 | #include <gametypes/SpaceRaceController.h> |
---|
| 38 | #include "core/CoreIncludes.h" |
---|
| 39 | #include "core/XMLPort.h" |
---|
[9412] | 40 | #include "gametypes/SpaceRaceManager.h" |
---|
[9487] | 41 | #include "collisionshapes/CollisionShape.h" |
---|
| 42 | #include "BulletCollision/CollisionShapes/btCollisionShape.h" |
---|
[9399] | 43 | |
---|
[9523] | 44 | |
---|
[9399] | 45 | namespace orxonox |
---|
| 46 | { |
---|
[9667] | 47 | RegisterClass(SpaceRaceController); |
---|
[9399] | 48 | |
---|
[9470] | 49 | const int ADJUSTDISTANCE = 500; |
---|
[9507] | 50 | const int MINDISTANCE = 5; |
---|
[9412] | 51 | /* |
---|
| 52 | * Idea: Find static Point (checkpoints the spaceship has to reach) |
---|
| 53 | */ |
---|
[9667] | 54 | SpaceRaceController::SpaceRaceController(Context* context) : |
---|
| 55 | ArtificialController(context) |
---|
[9432] | 56 | { |
---|
[9512] | 57 | RegisterObject(SpaceRaceController) |
---|
| 58 | ; std::vector<RaceCheckPoint*> checkpoints; |
---|
[9508] | 59 | |
---|
[9523] | 60 | virtualCheckPointIndex = -2; |
---|
| 61 | for (ObjectList<SpaceRaceManager>::iterator it = ObjectList<SpaceRaceManager>::begin(); it != ObjectList<SpaceRaceManager>::end(); ++it) |
---|
[9432] | 62 | { |
---|
| 63 | checkpoints = it->getAllCheckpoints(); |
---|
[9523] | 64 | nextRaceCheckpoint_ = it->findCheckpoint(0); |
---|
[9432] | 65 | } |
---|
[9399] | 66 | |
---|
[9432] | 67 | OrxAssert(!checkpoints.empty(), "No Checkpoints in Level"); |
---|
[9523] | 68 | checkpoints_ = checkpoints; |
---|
[9513] | 69 | /*orxout()<<"es gibt: "<<checkpoints_.size()<<"checkpoints"<<endl; |
---|
[9512] | 70 | for(std::vector<RaceCheckPoint*>::iterator it=checkpoints_.begin(); it!=checkpoints_.end(); it++) |
---|
| 71 | { |
---|
| 72 | orxout()<<"Checkpoint "<<(*it)->getCheckpointIndex()<<"; NExtReal: "; |
---|
| 73 | std::set<int> temp =(*it)->getNextCheckpoints(); |
---|
| 74 | for (std::set<int>::iterator ii =temp.begin(); ii!=temp.end(); ii++) |
---|
| 75 | { |
---|
| 76 | orxout()<<(*ii)<<", "; |
---|
| 77 | } |
---|
[9508] | 78 | |
---|
[9512] | 79 | orxout()<<" NextVirtual: "; |
---|
| 80 | temp=(*it)->getVirtualNextCheckpoints(); |
---|
| 81 | for (std::set<int>::iterator ii =temp.begin(); ii!=temp.end(); ii++) |
---|
| 82 | { |
---|
| 83 | orxout()<<(*ii)<<", "; |
---|
| 84 | } |
---|
| 85 | orxout()<<endl<<endl; |
---|
| 86 | |
---|
[9513] | 87 | }//ausgabe*/ |
---|
[9667] | 88 | /* |
---|
[9523] | 89 | for (std::vector<RaceCheckPoint*>::iterator it = checkpoints.begin(); it != checkpoints.end(); ++it) |
---|
[9507] | 90 | { |
---|
| 91 | std::set<int> nextCheckPoints = ((*it)->getNextCheckpoints()); |
---|
[9512] | 92 | if(!nextCheckPoints.empty()) |
---|
| 93 | { |
---|
| 94 | for (std::set<int>::iterator numb = nextCheckPoints.begin(); numb!=nextCheckPoints.end(); numb++) |
---|
| 95 | { |
---|
[9507] | 96 | RaceCheckPoint* point2 = findCheckpoint((*numb)); |
---|
| 97 | |
---|
[9523] | 98 | //if(point2 != NULL) |
---|
| 99 | //placeVirtualCheckpoints((*it), point2); |
---|
[9507] | 100 | } |
---|
| 101 | } |
---|
[9667] | 102 | } |
---|
| 103 | */ |
---|
| 104 | /* |
---|
[9512] | 105 | for(std::vector<RaceCheckPoint*>::iterator it=checkpoints_.begin(); it!=checkpoints_.end(); it++) |
---|
| 106 | { |
---|
| 107 | orxout()<<"Checkpoint "<<(*it)->getCheckpointIndex()<<"; NExtReal: "; |
---|
| 108 | std::set<int> temp =(*it)->getNextCheckpoints(); |
---|
| 109 | for (std::set<int>::iterator ii =temp.begin(); ii!=temp.end(); ii++) |
---|
| 110 | { |
---|
| 111 | orxout()<<(*ii)<<", "; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | orxout()<<" NextVirtual: "; |
---|
| 115 | temp=(*it)->getVirtualNextCheckpoints(); |
---|
| 116 | for (std::set<int>::iterator ii =temp.begin(); ii!=temp.end(); ii++) |
---|
| 117 | { |
---|
| 118 | orxout()<<(*ii)<<", "; |
---|
| 119 | } |
---|
| 120 | orxout()<<endl; |
---|
| 121 | |
---|
| 122 | }//ausgabe |
---|
[9513] | 123 | orxout()<<"es gibt: "<<checkpoints_.size()<<"checkpoints"<<endl;*/ |
---|
[9432] | 124 | staticRacePoints_ = findStaticCheckpoints(checkpoints); |
---|
[9451] | 125 | // initialisation of currentRaceCheckpoint_ |
---|
| 126 | currentRaceCheckpoint_ = NULL; |
---|
[9459] | 127 | |
---|
[9508] | 128 | int i; |
---|
[9523] | 129 | for (i = -2; findCheckpoint(i) != NULL; i--) |
---|
[9512] | 130 | { |
---|
[9508] | 131 | continue; |
---|
| 132 | } |
---|
[9513] | 133 | //orxout()<<"Die ANzahl der virtuellen CP betraegt: "<< (-i)-2<<endl; |
---|
[9508] | 134 | |
---|
[9459] | 135 | } |
---|
| 136 | |
---|
| 137 | //------------------------------ |
---|
| 138 | // functions for initialisation |
---|
| 139 | |
---|
| 140 | void SpaceRaceController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 141 | { |
---|
| 142 | SUPER(SpaceRaceController, XMLPort, xmlelement, mode); |
---|
| 143 | XMLPortParam(ArtificialController, "accuracy", setAccuracy, getAccuracy, xmlelement, mode).defaultValues(100.0f); |
---|
| 144 | XMLPortObject(ArtificialController, WorldEntity, "waypoints", addWaypoint, getWaypoint, xmlelement, mode); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | /* |
---|
| 148 | * called from constructor 'SpaceRaceController' |
---|
| 149 | * returns a vector of static Point (checkpoints the spaceship has to reach) |
---|
| 150 | */ |
---|
[9470] | 151 | std::vector<RaceCheckPoint*> SpaceRaceController::findStaticCheckpoints(std::vector<RaceCheckPoint*> allCheckpoints) |
---|
[9459] | 152 | { |
---|
[9523] | 153 | std::map<RaceCheckPoint*, int> * zaehler = new std::map<RaceCheckPoint*, int>(); // counts how many times the checkpoint was reached (for simulation) |
---|
[9459] | 154 | for (unsigned int i = 0; i < allCheckpoints.size(); i++) |
---|
| 155 | { |
---|
| 156 | zaehler->insert(std::pair<RaceCheckPoint*, int>(allCheckpoints[i],0)); |
---|
| 157 | } |
---|
[9470] | 158 | int maxWays = rekSimulationCheckpointsReached(zaehler->begin()->first, zaehler); |
---|
[9459] | 159 | |
---|
| 160 | std::vector<RaceCheckPoint*> returnVec; |
---|
| 161 | returnVec.clear(); |
---|
[9523] | 162 | for (std::map<RaceCheckPoint*, int>::iterator iter = zaehler->begin(); iter != zaehler->end(); iter++) |
---|
[9459] | 163 | { |
---|
| 164 | if (iter->second == maxWays) |
---|
| 165 | { |
---|
| 166 | //returnVec.insert(allCheckpoints[1]); |
---|
| 167 | returnVec.insert(returnVec.end(), iter->first); |
---|
[9441] | 168 | } |
---|
| 169 | } |
---|
[9459] | 170 | delete zaehler; |
---|
| 171 | return returnVec; |
---|
| 172 | } |
---|
[9399] | 173 | |
---|
[9459] | 174 | /* |
---|
| 175 | * called from 'findStaticCheckpoints' |
---|
| 176 | * return how many ways go from the given Checkpoint to the last Checkpoint (of the Game) |
---|
| 177 | */ |
---|
[9470] | 178 | int SpaceRaceController::rekSimulationCheckpointsReached(RaceCheckPoint* currentCheckpoint, std::map<RaceCheckPoint*, int>* zaehler) |
---|
[9459] | 179 | { |
---|
[9507] | 180 | |
---|
[9459] | 181 | if (currentCheckpoint->isLast()) |
---|
| 182 | {// last point reached |
---|
[9508] | 183 | |
---|
[9459] | 184 | (*zaehler)[currentCheckpoint] += 1; |
---|
| 185 | return 1; // 1 Way form the last point to this one |
---|
| 186 | } |
---|
| 187 | else |
---|
| 188 | { |
---|
| 189 | int numberOfWays = 0; // counts number of ways from this Point to the last point |
---|
[9508] | 190 | for (std::set<int>::iterator it = currentCheckpoint->getVirtualNextCheckpoints().begin(); it!= currentCheckpoint->getVirtualNextCheckpoints().end(); ++it) |
---|
[9459] | 191 | { |
---|
[9523] | 192 | if(currentCheckpoint == findCheckpoint(*it)) |
---|
[9512] | 193 | { |
---|
[9513] | 194 | //orxout() << currentCheckpoint->getCheckpointIndex()<<endl; |
---|
[9507] | 195 | continue; |
---|
| 196 | } |
---|
[9523] | 197 | if(findCheckpoint(*it) == NULL) |
---|
| 198 | {orxout()<<"Problematic Point: "<<(*it)<<endl;} |
---|
[9512] | 199 | numberOfWays += rekSimulationCheckpointsReached(findCheckpoint(*it), zaehler); |
---|
[9459] | 200 | } |
---|
| 201 | (*zaehler)[currentCheckpoint] += numberOfWays; |
---|
| 202 | return numberOfWays; // returns the number of ways from this point to the last one |
---|
| 203 | } |
---|
[9432] | 204 | } |
---|
[9399] | 205 | |
---|
[9459] | 206 | //------------------------------------- |
---|
| 207 | // functions for dynamic Way-search |
---|
| 208 | |
---|
[9432] | 209 | int SpaceRaceController::distanceSpaceshipToCheckPoint(RaceCheckPoint* CheckPoint) |
---|
| 210 | { |
---|
| 211 | if (this->getControllableEntity() != NULL) |
---|
| 212 | { |
---|
| 213 | return (CheckPoint->getPosition()- this->getControllableEntity()->getPosition()).length(); |
---|
| 214 | } |
---|
| 215 | return -1; |
---|
[9412] | 216 | } |
---|
| 217 | |
---|
[9459] | 218 | /* |
---|
| 219 | * called by: 'tick' or 'adjustNextPoint' |
---|
| 220 | * returns the next Checkpoint which the shortest way contains |
---|
| 221 | */ |
---|
[9432] | 222 | RaceCheckPoint* SpaceRaceController::nextPointFind(RaceCheckPoint* raceCheckpoint) |
---|
| 223 | { |
---|
[9523] | 224 | int distances[] = {-1, -1, -1}; |
---|
[9432] | 225 | int temp_i = 0; |
---|
[9508] | 226 | for (std::set<int>::iterator it =raceCheckpoint->getVirtualNextCheckpoints().begin(); it!= raceCheckpoint->getVirtualNextCheckpoints().end(); ++it) |
---|
[9432] | 227 | { |
---|
[9459] | 228 | distances[temp_i] = recCalculateDistance(findCheckpoint(*it), this->getControllableEntity()->getPosition()); |
---|
[9432] | 229 | temp_i++; |
---|
| 230 | } |
---|
| 231 | if (distances[0] > distances[1] && distances[1] != -1) |
---|
| 232 | { |
---|
| 233 | if (distances[2] < distances[1] && distances[2] != -1) |
---|
| 234 | { |
---|
[9508] | 235 | return findCheckpoint(*raceCheckpoint->getVirtualNextCheckpoints().end()); // return checkpoint with ID of raceCheckpoint->getNextCheckpoints() [2] |
---|
[9432] | 236 | } |
---|
| 237 | else |
---|
| 238 | { |
---|
[9508] | 239 | std::set<int>::iterator temp = raceCheckpoint->getVirtualNextCheckpoints().begin(); |
---|
[9459] | 240 | return findCheckpoint(*(++temp)); // return [1] |
---|
[9432] | 241 | } |
---|
| 242 | } |
---|
| 243 | else |
---|
| 244 | { |
---|
| 245 | if (distances[2] < distances[0] && distances[2] != -1) |
---|
| 246 | { |
---|
[9508] | 247 | return findCheckpoint(*raceCheckpoint->getVirtualNextCheckpoints().end()); // return [2] |
---|
[9432] | 248 | } |
---|
| 249 | else |
---|
| 250 | { |
---|
[9508] | 251 | return findCheckpoint(*raceCheckpoint->getVirtualNextCheckpoints().begin()); // return [0] |
---|
[9432] | 252 | } |
---|
| 253 | } |
---|
| 254 | } |
---|
[9412] | 255 | |
---|
[9459] | 256 | /* |
---|
| 257 | * called from 'nextPointFind' |
---|
| 258 | * returns the distance between "currentPosition" and the next static checkpoint that can be reached from "currentCheckPoint" |
---|
| 259 | */ |
---|
[9432] | 260 | int SpaceRaceController::recCalculateDistance(RaceCheckPoint* currentCheckPoint, Vector3 currentPosition) |
---|
| 261 | { |
---|
[9459] | 262 | // find: looks if the currentCheckPoint is a staticCheckPoint (staticCheckPoint is the same as: static Point) |
---|
| 263 | if (std::find(staticRacePoints_.begin(), staticRacePoints_.end(), currentCheckPoint) != staticRacePoints_.end()) |
---|
[9432] | 264 | { |
---|
| 265 | return (currentCheckPoint->getPosition() - currentPosition).length(); |
---|
| 266 | } |
---|
| 267 | else |
---|
| 268 | { |
---|
| 269 | int minimum = std::numeric_limits<int>::max(); |
---|
[9523] | 270 | for (std::set<int>::iterator it = currentCheckPoint->getVirtualNextCheckpoints().begin(); it != currentCheckPoint->getVirtualNextCheckpoints().end(); ++it) |
---|
[9459] | 271 | { |
---|
| 272 | int dist_currentCheckPoint_currentPosition = static_cast<int> ((currentPosition- currentCheckPoint->getPosition()).length()); |
---|
[9441] | 273 | |
---|
[9523] | 274 | minimum = std::min(minimum, dist_currentCheckPoint_currentPosition + recCalculateDistance(findCheckpoint(*it), currentCheckPoint->getPosition())); |
---|
[9441] | 275 | // minimum of distanz from 'currentPosition' to the next static Checkpoint |
---|
[9459] | 276 | } |
---|
[9432] | 277 | return minimum; |
---|
| 278 | } |
---|
| 279 | } |
---|
[9412] | 280 | |
---|
[9459] | 281 | /*called by 'tick' |
---|
| 282 | *adjust chosen way of the Spaceship every "AdjustDistance" because spaceship could be displaced through an other one |
---|
[9432] | 283 | */ |
---|
[9459] | 284 | RaceCheckPoint* SpaceRaceController::adjustNextPoint() |
---|
[9432] | 285 | { |
---|
[9459] | 286 | if (currentRaceCheckpoint_ == NULL) // no Adjust possible |
---|
| 287 | |
---|
[9432] | 288 | { |
---|
[9459] | 289 | return nextRaceCheckpoint_; |
---|
[9432] | 290 | } |
---|
[9508] | 291 | if ((currentRaceCheckpoint_->getVirtualNextCheckpoints()).size() == 1) // no Adjust possible |
---|
[9399] | 292 | |
---|
[9432] | 293 | { |
---|
[9459] | 294 | return nextRaceCheckpoint_; |
---|
[9432] | 295 | } |
---|
[9459] | 296 | |
---|
| 297 | //Adjust possible |
---|
| 298 | |
---|
| 299 | return nextPointFind(currentRaceCheckpoint_); |
---|
[9432] | 300 | } |
---|
[9399] | 301 | |
---|
[9459] | 302 | RaceCheckPoint* SpaceRaceController::findCheckpoint(int index) const |
---|
[9507] | 303 | { |
---|
| 304 | for (size_t i = 0; i < this->checkpoints_.size(); ++i) |
---|
[9667] | 305 | if (this->checkpoints_[i]->getCheckpointIndex() == index) |
---|
| 306 | return this->checkpoints_[i]; |
---|
[9507] | 307 | return NULL; |
---|
| 308 | } |
---|
[9459] | 309 | |
---|
[9523] | 310 | /*RaceCheckPoint* SpaceRaceController::addVirtualCheckPoint( RaceCheckPoint* previousCheckpoint, int indexFollowingCheckPoint , Vector3 virtualCheckPointPosition ) |
---|
[9507] | 311 | { |
---|
[9513] | 312 | orxout()<<"add VCP at"<<virtualCheckPointPosition.x<<", "<<virtualCheckPointPosition.y<<", "<<virtualCheckPointPosition.z<<endl; |
---|
[9507] | 313 | RaceCheckPoint* newTempRaceCheckPoint; |
---|
[9512] | 314 | for (ObjectList<SpaceRaceManager>::iterator it = ObjectList<SpaceRaceManager>::begin(); it!= ObjectList<SpaceRaceManager>::end(); ++it) |
---|
| 315 | { |
---|
[9507] | 316 | newTempRaceCheckPoint = new RaceCheckPoint((*it)); |
---|
| 317 | } |
---|
[9508] | 318 | newTempRaceCheckPoint->setVisible(false); |
---|
[9459] | 319 | newTempRaceCheckPoint->setPosition(virtualCheckPointPosition); |
---|
| 320 | newTempRaceCheckPoint->setCheckpointIndex(virtualCheckPointIndex); |
---|
| 321 | newTempRaceCheckPoint->setLast(false); |
---|
[9508] | 322 | newTempRaceCheckPoint->setNextVirtualCheckpointsAsVector3(Vector3(indexFollowingCheckPoint,-1,-1)); |
---|
[9459] | 323 | |
---|
[9508] | 324 | Vector3 temp = previousCheckpoint->getVirtualNextCheckpointsAsVector3(); |
---|
[9513] | 325 | //orxout()<<"temp bei 0: ="<< temp.x<< temp.y<< temp.z<<endl; |
---|
[9459] | 326 | checkpoints_.insert(checkpoints_.end(), newTempRaceCheckPoint); |
---|
[9487] | 327 | int positionInNextCheckPoint; |
---|
[9507] | 328 | for (int i = 0; i <3; i++) |
---|
| 329 | { |
---|
[9508] | 330 | if(previousCheckpoint->getVirtualNextCheckpointsAsVector3()[i] == indexFollowingCheckPoint) |
---|
[9512] | 331 | positionInNextCheckPoint=i; |
---|
[9487] | 332 | } |
---|
[9507] | 333 | switch(positionInNextCheckPoint) |
---|
| 334 | { |
---|
[9459] | 335 | case 0: temp.x=virtualCheckPointIndex; break; |
---|
| 336 | case 1: temp.y=virtualCheckPointIndex; break; |
---|
| 337 | case 2: temp.z=virtualCheckPointIndex; break; |
---|
[9432] | 338 | } |
---|
[9508] | 339 | previousCheckpoint->setNextVirtualCheckpointsAsVector3(temp); //Existiert internes Problem bei negativen index fueer next Checkpoint |
---|
[9459] | 340 | virtualCheckPointIndex--; |
---|
[9513] | 341 | //orxout()<<"temp bei 1: ="<< temp.x<< temp.y<< temp.z<<endl; |
---|
| 342 | //orxout()<<"temp nach ausgabe: "<<previousCheckpoint->getVirtualNextCheckpointsAsVector3().x<<previousCheckpoint->getVirtualNextCheckpointsAsVector3().y<<previousCheckpoint->getVirtualNextCheckpointsAsVector3().z<<endl; |
---|
[9508] | 343 | //OrxAssert(virtualCheckPointIndex < -1, "TO much virtual cp"); |
---|
[9667] | 344 | orxout()<<"id: "<< previousCheckpoint->getCheckpointIndex() <<", following:"<<indexFollowingCheckPoint<<" : "<<temp.x<<", "<<temp.y<<", "<<temp.z<<"; "; |
---|
[9512] | 345 | temp=previousCheckpoint->getNextCheckpointsAsVector3(); |
---|
| 346 | orxout()<<"id: "<< previousCheckpoint->getCheckpointIndex() <<": "<<temp.x<<", "<<temp.y<<", "<<temp.z<<"; "; |
---|
[9667] | 347 | orxout()<<endl; |
---|
[9487] | 348 | return newTempRaceCheckPoint; |
---|
[9523] | 349 | }*/ |
---|
[9399] | 350 | |
---|
[9432] | 351 | SpaceRaceController::~SpaceRaceController() |
---|
| 352 | { |
---|
[9507] | 353 | for (int i =-1; i>virtualCheckPointIndex; i--) |
---|
| 354 | { |
---|
[9459] | 355 | delete findCheckpoint(i); |
---|
| 356 | } |
---|
[9432] | 357 | } |
---|
[9399] | 358 | |
---|
[9459] | 359 | void SpaceRaceController::tick(float dt) |
---|
[9432] | 360 | { |
---|
[9459] | 361 | if (this->getControllableEntity() == NULL || this->getControllableEntity()->getPlayer() == NULL ) |
---|
[9523] | 362 | { |
---|
| 363 | //orxout()<< this->getControllableEntity() << " in tick"<<endl; |
---|
| 364 | return; |
---|
| 365 | } |
---|
[9459] | 366 | //FOR virtual Checkpoints |
---|
[9507] | 367 | if(nextRaceCheckpoint_->getCheckpointIndex() < 0) |
---|
| 368 | { |
---|
[9508] | 369 | if( distanceSpaceshipToCheckPoint(nextRaceCheckpoint_) < 200) |
---|
[9507] | 370 | { |
---|
[9459] | 371 | currentRaceCheckpoint_=nextRaceCheckpoint_; |
---|
| 372 | nextRaceCheckpoint_ = nextPointFind(nextRaceCheckpoint_); |
---|
| 373 | lastPositionSpaceship=this->getControllableEntity()->getPosition(); |
---|
[9513] | 374 | //orxout()<< "CP "<< currentRaceCheckpoint_->getCheckpointIndex()<<" chanched to: "<< nextRaceCheckpoint_->getCheckpointIndex()<<endl; |
---|
[9459] | 375 | } |
---|
| 376 | } |
---|
[9399] | 377 | |
---|
[9432] | 378 | if (nextRaceCheckpoint_->playerWasHere(this->getControllableEntity()->getPlayer())) |
---|
| 379 | {//Checkpoint erreicht |
---|
[9508] | 380 | |
---|
[9523] | 381 | currentRaceCheckpoint_ = nextRaceCheckpoint_; |
---|
[9432] | 382 | OrxAssert(nextRaceCheckpoint_, "next race checkpoint undefined"); |
---|
| 383 | nextRaceCheckpoint_ = nextPointFind(nextRaceCheckpoint_); |
---|
[9523] | 384 | lastPositionSpaceship = this->getControllableEntity()->getPosition(); |
---|
[9513] | 385 | //orxout()<< "CP "<< currentRaceCheckpoint_->getCheckpointIndex()<<" chanched to: "<< nextRaceCheckpoint_->getCheckpointIndex()<<endl; |
---|
[9432] | 386 | } |
---|
[9470] | 387 | else if ((lastPositionSpaceship-this->getControllableEntity()->getPosition()).length()/dt > ADJUSTDISTANCE) |
---|
[9432] | 388 | { |
---|
| 389 | nextRaceCheckpoint_ = adjustNextPoint(); |
---|
[9523] | 390 | lastPositionSpaceship = this->getControllableEntity()->getPosition(); |
---|
[9432] | 391 | } |
---|
[9507] | 392 | |
---|
[9508] | 393 | // Abmessung fuer MINDISTANCE gut; |
---|
[9512] | 394 | |
---|
[9523] | 395 | else if((lastPositionSpaceship - this->getControllableEntity()->getPosition()).length()/dt < MINDISTANCE ) |
---|
[9507] | 396 | { |
---|
[9523] | 397 | this->moveToPosition(Vector3(rnd()*100, rnd()*100, rnd()*100)); |
---|
[9459] | 398 | this->spin(); |
---|
[9470] | 399 | //orxout(user_status) << "Mindistance reached" << std::endl; |
---|
| 400 | return; |
---|
[9459] | 401 | } |
---|
[9470] | 402 | //orxout(user_status) << "dt= " << dt << "; distance= " << (lastPositionSpaceship-this->getControllableEntity()->getPosition()).length() <<std::endl; |
---|
[9523] | 403 | lastPositionSpaceship = this->getControllableEntity()->getPosition(); |
---|
[9432] | 404 | this->moveToPosition(nextRaceCheckpoint_->getPosition()); |
---|
[9487] | 405 | } |
---|
[9459] | 406 | |
---|
[9487] | 407 | // True if a coordinate of 'pointToPoint' is smaller then the corresponding coordinate of 'groesse' |
---|
[9507] | 408 | bool SpaceRaceController::vergleicheQuader(Vector3 pointToPoint, Vector3 groesse) |
---|
| 409 | { |
---|
[9523] | 410 | if(abs(pointToPoint.x) < groesse.x) |
---|
| 411 | return true; |
---|
| 412 | if(abs(pointToPoint.y) < groesse.y) |
---|
| 413 | return true; |
---|
| 414 | if(abs(pointToPoint.z) < groesse.z) |
---|
| 415 | return true; |
---|
| 416 | return false; |
---|
[9459] | 417 | |
---|
[9432] | 418 | } |
---|
[9399] | 419 | |
---|
[9512] | 420 | bool SpaceRaceController::directLinePossible(RaceCheckPoint* racepoint1, RaceCheckPoint* racepoint2,std::vector<StaticEntity*> allObjects) |
---|
| 421 | { |
---|
[9487] | 422 | |
---|
[9523] | 423 | Vector3 cP1ToCP2 = (racepoint2->getPosition() - racepoint1->getPosition()) / (racepoint2->getPosition() - racepoint1->getPosition()).length(); //unit Vector |
---|
| 424 | Vector3 centerCP1 = racepoint1->getPosition(); |
---|
[9507] | 425 | btVector3 positionObject; |
---|
| 426 | btScalar radiusObject; |
---|
[9470] | 427 | |
---|
[9523] | 428 | for (std::vector<StaticEntity*>::iterator it = allObjects.begin(); it != allObjects.end(); ++it) |
---|
[9512] | 429 | { |
---|
[9523] | 430 | for (int everyShape=0; (*it)->getAttachedCollisionShape(everyShape) != 0; everyShape++) |
---|
[9512] | 431 | { |
---|
[9507] | 432 | btCollisionShape* currentShape = (*it)->getAttachedCollisionShape(everyShape)->getCollisionShape(); |
---|
| 433 | if(currentShape == NULL) |
---|
[9512] | 434 | continue; |
---|
[9487] | 435 | |
---|
[9507] | 436 | currentShape->getBoundingSphere(positionObject,radiusObject); |
---|
| 437 | Vector3 positionObjectNonBT(positionObject.x(), positionObject.y(), positionObject.z()); |
---|
[9512] | 438 | if((powf((cP1ToCP2.dotProduct(centerCP1-positionObjectNonBT)),2)-(centerCP1-positionObjectNonBT).dotProduct(centerCP1-positionObjectNonBT)+powf(radiusObject, 2))>0) |
---|
| 439 | { |
---|
[9507] | 440 | return false; |
---|
| 441 | } |
---|
[9487] | 442 | |
---|
[9507] | 443 | } |
---|
| 444 | } |
---|
| 445 | return true; |
---|
[9487] | 446 | |
---|
[9507] | 447 | } |
---|
| 448 | |
---|
[9523] | 449 | /*void SpaceRaceController::computeVirtualCheckpoint(RaceCheckPoint* racepoint1, RaceCheckPoint* racepoint2, std::vector<StaticEntity*> allObjects) |
---|
[9512] | 450 | { |
---|
| 451 | Vector3 cP1ToCP2=(racepoint2->getPosition()-racepoint1->getPosition()) / (racepoint2->getPosition()-racepoint1->getPosition()).length(); //unit Vector |
---|
| 452 | Vector3 centerCP1=racepoint1->getPosition(); |
---|
| 453 | btVector3 positionObject; |
---|
| 454 | btScalar radiusObject; |
---|
[9507] | 455 | |
---|
[9523] | 456 | for (std::vector<StaticEntity*>::iterator it = allObjects.begin(); it != allObjects.end(); ++it) |
---|
[9512] | 457 | { |
---|
[9523] | 458 | for (int everyShape=0; (*it)->getAttachedCollisionShape(everyShape) != 0; everyShape++) |
---|
[9512] | 459 | { |
---|
| 460 | btCollisionShape* currentShape = (*it)->getAttachedCollisionShape(everyShape)->getCollisionShape(); |
---|
| 461 | if(currentShape == NULL) |
---|
| 462 | continue; |
---|
[9507] | 463 | |
---|
[9512] | 464 | currentShape->getBoundingSphere(positionObject,radiusObject); |
---|
| 465 | Vector3 positionObjectNonBT(positionObject.x(), positionObject.y(), positionObject.z()); |
---|
[9523] | 466 | Vector3 norm_r_CP = cP1ToCP2.crossProduct(centerCP1-positionObjectNonBT); |
---|
| 467 | |
---|
| 468 | if(norm_r_CP.length() == 0){ |
---|
| 469 | Vector3 zufall; |
---|
| 470 | do{ |
---|
[9512] | 471 | zufall=Vector3(rnd(),rnd(),rnd());//random |
---|
[9523] | 472 | }while((zufall.crossProduct(cP1ToCP2)).length() == 0); |
---|
| 473 | norm_r_CP=zufall.crossProduct(cP1ToCP2); |
---|
| 474 | } |
---|
| 475 | Vector3 VecToVCP = norm_r_CP.crossProduct(cP1ToCP2); |
---|
| 476 | float distanzToCP1 = sqrt(powf(radiusObject,4)/(powf((centerCP1-positionObjectNonBT).length(), 2)-powf(radiusObject,2))+powf(radiusObject,2)); |
---|
| 477 | float distanzToCP2 = sqrt(powf(radiusObject,4)/(powf((racepoint2->getPosition()-positionObjectNonBT).length(), 2)-powf(radiusObject,2))+powf(radiusObject,2)); |
---|
| 478 | float distanz = std::max(distanzToCP1,distanzToCP2); |
---|
| 479 | //float distanz = 0.0f; //TEMPORARY |
---|
| 480 | Vector3 newCheckpointPositionPos = positionObjectNonBT+(distanz*VecToVCP)/VecToVCP.length(); |
---|
| 481 | Vector3 newCheckpointPositionNeg = positionObjectNonBT-(distanz*VecToVCP)/VecToVCP.length(); |
---|
| 482 | if((newCheckpointPositionPos - centerCP1).length() + (newCheckpointPositionPos - (centerCP1+cP1ToCP2)).length() < (newCheckpointPositionNeg - centerCP1).length() + (newCheckpointPositionNeg - (centerCP1+cP1ToCP2)).length() ) |
---|
| 483 | { |
---|
| 484 | RaceCheckPoint* newVirtualCheckpoint = addVirtualCheckPoint(racepoint1,racepoint2->getCheckpointIndex(), newCheckpointPositionPos); |
---|
| 485 | } |
---|
| 486 | else |
---|
| 487 | { |
---|
| 488 | RaceCheckPoint* newVirtualCheckpoint = addVirtualCheckPoint(racepoint1,racepoint2->getCheckpointIndex(), newCheckpointPositionNeg); |
---|
| 489 | } |
---|
| 490 | return; |
---|
[9512] | 491 | } |
---|
| 492 | } |
---|
[9507] | 493 | |
---|
[9523] | 494 | }*/ |
---|
[9507] | 495 | |
---|
[9523] | 496 | /*void SpaceRaceController::placeVirtualCheckpoints(RaceCheckPoint* racepoint1, RaceCheckPoint* racepoint2) |
---|
[9507] | 497 | { |
---|
| 498 | Vector3 point1 = racepoint1->getPosition(); |
---|
| 499 | Vector3 point2 = racepoint2->getPosition(); |
---|
| 500 | std::vector<StaticEntity*> problematicObjects; |
---|
| 501 | |
---|
| 502 | for (ObjectList<StaticEntity>::iterator it = ObjectList<StaticEntity>::begin(); it!= ObjectList<StaticEntity>::end(); ++it) |
---|
| 503 | { |
---|
| 504 | |
---|
[9523] | 505 | if (dynamic_cast<RaceCheckPoint*>(*it) != NULL) |
---|
| 506 | { |
---|
| 507 | continue; |
---|
| 508 | } // does not work jet |
---|
[9507] | 509 | |
---|
| 510 | problematicObjects.insert(problematicObjects.end(), *it); |
---|
| 511 | //it->getScale3D();// vector fuer halbe wuerfellaenge |
---|
[9487] | 512 | } |
---|
[9470] | 513 | |
---|
[9512] | 514 | if(!directLinePossible(racepoint1, racepoint2, problematicObjects)) |
---|
| 515 | { |
---|
[9513] | 516 | //orxout()<<"From "<<racepoint1->getCheckpointIndex()<<" to "<<racepoint2->getCheckpointIndex()<<"produces: "<< virtualCheckPointIndex<<endl; |
---|
[9507] | 517 | computeVirtualCheckpoint(racepoint1, racepoint2, problematicObjects); |
---|
| 518 | } |
---|
[9487] | 519 | |
---|
[9512] | 520 | // |
---|
| 521 | // do{ |
---|
| 522 | // zufall=Vector3(rnd(),rnd(),rnd());//random |
---|
| 523 | // }while((zufall.crossProduct(objectmiddle-racepoint1->getPosition())).length()==0); |
---|
| 524 | // |
---|
| 525 | // Vector3 normalvec=zufall.crossProduct(objectmiddle-racepoint1->getPosition()); |
---|
| 526 | // // a'/b'=a/b => a' =b'*a/b |
---|
| 527 | // float laengeNormalvec=(objectmiddle-racepoint1->getPosition()).length()/sqrt((objectmiddle-racepoint1->getPosition()).squaredLength()-radius*radius)*radius; |
---|
| 528 | // addVirtualCheckPoint(racepoint1,racepoint2->getCheckpointIndex(), objectmiddle+normalvec/normalvec.length()*laengeNormalvec); |
---|
[9507] | 529 | |
---|
[9512] | 530 | // Vector3 richtungen [6]; |
---|
| 531 | // richtungen[0]= Vector3(1,0,0); |
---|
| 532 | // richtungen[1]= Vector3(-1,0,0); |
---|
| 533 | // richtungen[2]= Vector3(0,1,0); |
---|
| 534 | // richtungen[3]= Vector3(0,-1,0); |
---|
| 535 | // richtungen[4]= Vector3(0,0,1); |
---|
| 536 | // richtungen[5]= Vector3(0,0,-1); |
---|
| 537 | // |
---|
| 538 | // for (int i = 0; i< 6; i++) |
---|
| 539 | // { |
---|
| 540 | // const int STEPS=100; |
---|
| 541 | // const float PHI=1.1; |
---|
| 542 | // bool collision=false; |
---|
| 543 | // |
---|
| 544 | // for (int j =0; j<STEPS; j++) |
---|
| 545 | // { |
---|
| 546 | // Vector3 tempPosition=(point1 - (point2-point1+richtungen[i]*PHI)*(float)j/STEPS); |
---|
| 547 | // for (std::vector<StaticEntity*>::iterator it = problematicObjects.begin(); it!=problematicObjects.end(); ++it) |
---|
| 548 | // { |
---|
| 549 | // btVector3 positionObject; |
---|
| 550 | // btScalar radiusObject; |
---|
| 551 | // if((*it)==NULL) |
---|
| 552 | // { orxout()<<"Problempoint 1.1"<<endl; continue;} |
---|
| 553 | // //TODO: Probably it points on a wrong object |
---|
| 554 | // for (int everyShape=0; (*it)->getAttachedCollisionShape(everyShape)!=0; everyShape++) |
---|
| 555 | // { |
---|
| 556 | // if((*it)->getAttachedCollisionShape(everyShape)->getCollisionShape()==NULL) |
---|
| 557 | // { continue;} |
---|
| 558 | // |
---|
| 559 | // orxout()<<"Problempoint 2.1"<<endl; |
---|
| 560 | // (*it)->getAttachedCollisionShape(everyShape)->getCollisionShape()->getBoundingSphere(positionObject,radiusObject); |
---|
| 561 | // Vector3 positionObjectNonBT(positionObject.x(), positionObject.y(), positionObject.z()); |
---|
| 562 | // if (((tempPosition - positionObjectNonBT).length()<radiusObject) && (vergleicheQuader((tempPosition-positionObjectNonBT),(*it)->getScale3D()))) |
---|
| 563 | // { |
---|
| 564 | // collision=true; break; |
---|
| 565 | // } |
---|
| 566 | // } |
---|
| 567 | // if(collision) break; |
---|
| 568 | // } |
---|
| 569 | // if(collision)break; |
---|
| 570 | // } |
---|
| 571 | // if(collision) continue; |
---|
| 572 | // // no collision => possible Way |
---|
| 573 | // for (float j =0; j<STEPS; j++) |
---|
| 574 | // { |
---|
| 575 | // Vector3 possiblePosition=(point1 - (point2-point1+richtungen[i]*PHI)*j/STEPS); |
---|
| 576 | // collision=false; |
---|
| 577 | // for(int ij=0; ij<STEPS; j++) |
---|
| 578 | // { |
---|
| 579 | // Vector3 tempPosition=(possiblePosition - (point2-possiblePosition)*(float)ij/STEPS); |
---|
| 580 | // for (std::vector<StaticEntity*>::iterator it = problematicObjects.begin(); it!=problematicObjects.end(); ++it) |
---|
| 581 | // { |
---|
| 582 | // btVector3 positionObject; |
---|
| 583 | // btScalar radiusObject; |
---|
| 584 | // if((*it)==NULL) |
---|
| 585 | // { orxout()<<"Problempoint 1"<<endl; continue;} |
---|
| 586 | // for (int everyShape=0; (*it)->getAttachedCollisionShape(everyShape)!=0; everyShape++) |
---|
| 587 | // { |
---|
| 588 | // if((*it)->getAttachedCollisionShape(everyShape)->getCollisionShape()==NULL) |
---|
| 589 | // { orxout()<<"Problempoint 2.2"<<endl; continue;} |
---|
| 590 | // (*it)->getAttachedCollisionShape(everyShape)->getCollisionShape()->getBoundingSphere(positionObject,radiusObject); |
---|
| 591 | // Vector3 positionObjectNonBT(positionObject.x(), positionObject.y(), positionObject.z()); |
---|
| 592 | // if (((tempPosition-positionObjectNonBT).length()<radiusObject) && (vergleicheQuader((tempPosition-positionObjectNonBT),(*it)->getScale3D()))) |
---|
| 593 | // { |
---|
| 594 | // collision=true; break; |
---|
| 595 | // } |
---|
| 596 | // } |
---|
| 597 | // if(collision) break; |
---|
| 598 | // } |
---|
| 599 | // if(collision)break; |
---|
| 600 | // //addVirtualCheckPoint(racepoint1, racepoint2->getCheckpointIndex(), possiblePosition); |
---|
| 601 | // return; |
---|
| 602 | // } |
---|
| 603 | // |
---|
| 604 | // } |
---|
| 605 | // } |
---|
[9507] | 606 | |
---|
[9523] | 607 | }*/ |
---|
[9399] | 608 | } |
---|