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