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