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