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