1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
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 | * |
---|
22 | * Created on: Oct 8, 2012findCheck |
---|
23 | * Author: purgham |
---|
24 | */ |
---|
25 | |
---|
26 | #include <gametypes/SpaceRaceController.h> |
---|
27 | #include "core/CoreIncludes.h" |
---|
28 | #include "core/XMLPort.h" |
---|
29 | #include "gametypes/SpaceRaceManager.h" |
---|
30 | #include "collisionshapes/CollisionShape.h" |
---|
31 | #include "BulletCollision/CollisionShapes/btCollisionShape.h" |
---|
32 | #include "SpaceRace.h" |
---|
33 | |
---|
34 | |
---|
35 | namespace orxonox |
---|
36 | { |
---|
37 | RegisterClass(SpaceRaceController); |
---|
38 | |
---|
39 | const int ADJUSTDISTANCE = 500; |
---|
40 | const int MINDISTANCE = 5; |
---|
41 | |
---|
42 | /* |
---|
43 | * Idea: Find static Point (checkpoints the spaceship has to reach) |
---|
44 | */ |
---|
45 | SpaceRaceController::SpaceRaceController(Context* context) : |
---|
46 | ArtificialController(context) |
---|
47 | { |
---|
48 | RegisterObject(SpaceRaceController); |
---|
49 | //this->parentRace = nullptr; |
---|
50 | |
---|
51 | std::vector<RaceCheckPoint*> checkpoints; |
---|
52 | |
---|
53 | virtualCheckPointIndex = -2; |
---|
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>()) |
---|
57 | { |
---|
58 | checkpoints = manager->getAllCheckpoints(); |
---|
59 | nextRaceCheckpoint_ = manager->findCheckpoint(0); |
---|
60 | } |
---|
61 | |
---|
62 | OrxAssert(!checkpoints.empty(), "No Checkpoints in Level"); |
---|
63 | checkpoints_ = checkpoints; |
---|
64 | staticRacePoints_ = findStaticCheckpoints(nextRaceCheckpoint_, checkpoints); |
---|
65 | // initialisation of currentRaceCheckpoint_ |
---|
66 | currentRaceCheckpoint_ = nullptr; |
---|
67 | |
---|
68 | int i; |
---|
69 | for (i = -2; findCheckpoint(i) != nullptr; i--) // WIESO? |
---|
70 | { |
---|
71 | continue; |
---|
72 | } |
---|
73 | |
---|
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 | */ |
---|
90 | std::vector<RaceCheckPoint*> SpaceRaceController::findStaticCheckpoints(RaceCheckPoint* currentCheckpoint, const std::vector<RaceCheckPoint*>& allCheckpoints) |
---|
91 | { |
---|
92 | std::map<RaceCheckPoint*, int> zaehler; // counts how many times the checkpoint was reached (for simulation) |
---|
93 | for (RaceCheckPoint* checkpoint : allCheckpoints) |
---|
94 | { |
---|
95 | zaehler.insert(std::pair<RaceCheckPoint*, int>(checkpoint,0)); |
---|
96 | } |
---|
97 | int maxWays = rekSimulationCheckpointsReached(currentCheckpoint, zaehler); |
---|
98 | |
---|
99 | std::vector<RaceCheckPoint*> returnVec; |
---|
100 | for (const auto& mapEntry : zaehler) |
---|
101 | { |
---|
102 | if (mapEntry.second == maxWays) |
---|
103 | { |
---|
104 | returnVec.push_back(mapEntry.first); |
---|
105 | } |
---|
106 | } |
---|
107 | return returnVec; |
---|
108 | } |
---|
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(); |
---|
116 | |
---|
117 | } |
---|
118 | /* |
---|
119 | * called from 'findStaticCheckpoints' |
---|
120 | * return how many ways go from the given Checkpoint to the last Checkpoint (of the Game) |
---|
121 | */ |
---|
122 | int SpaceRaceController::rekSimulationCheckpointsReached(RaceCheckPoint* currentCheckpoint, std::map<RaceCheckPoint*, int>& zaehler) |
---|
123 | { |
---|
124 | |
---|
125 | if (currentCheckpoint->isLast()) |
---|
126 | {// last point reached |
---|
127 | |
---|
128 | zaehler[currentCheckpoint] += 1; |
---|
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 |
---|
134 | for (int checkpointIndex : currentCheckpoint->getNextCheckpoints()) |
---|
135 | { |
---|
136 | if (findCheckpoint(checkpointIndex) == nullptr){ |
---|
137 | orxout(internal_warning) << "Problematic Point: " << checkpointIndex << endl; |
---|
138 | } |
---|
139 | if (currentCheckpoint == findCheckpoint(checkpointIndex)) |
---|
140 | { |
---|
141 | orxout() << currentCheckpoint->getCheckpointIndex()<<endl; |
---|
142 | continue; |
---|
143 | } |
---|
144 | |
---|
145 | else |
---|
146 | numberOfWays += rekSimulationCheckpointsReached(findCheckpoint(checkpointIndex), zaehler); |
---|
147 | |
---|
148 | } |
---|
149 | zaehler[currentCheckpoint] += numberOfWays; |
---|
150 | return numberOfWays; // returns the number of ways from this point to the last one |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | //------------------------------------- |
---|
155 | // functions for dynamic Way-search |
---|
156 | |
---|
157 | float SpaceRaceController::distanceSpaceshipToCheckPoint(RaceCheckPoint* CheckPoint) |
---|
158 | { |
---|
159 | if (this->getControllableEntity() != nullptr) |
---|
160 | { |
---|
161 | return (CheckPoint->getPosition()- this->getControllableEntity()->getPosition()).length(); |
---|
162 | } |
---|
163 | return -1; |
---|
164 | } |
---|
165 | |
---|
166 | /* |
---|
167 | * called by: 'tick' or 'adjustNextPoint' |
---|
168 | * returns the next Checkpoint which the shortest way contains |
---|
169 | */ |
---|
170 | RaceCheckPoint* SpaceRaceController::nextPointFind(RaceCheckPoint* raceCheckpoint) |
---|
171 | { |
---|
172 | float minDistance = 0; |
---|
173 | RaceCheckPoint* minNextRaceCheckPoint = nullptr; |
---|
174 | |
---|
175 | // find the next checkpoint with the minimal distance |
---|
176 | for (int checkpointIndex : raceCheckpoint->getNextCheckpoints()) |
---|
177 | { |
---|
178 | RaceCheckPoint* nextRaceCheckPoint = findCheckpoint(checkpointIndex); |
---|
179 | |
---|
180 | float distance = recCalculateDistance(nextRaceCheckPoint, this->getControllableEntity()->getPosition()); |
---|
181 | |
---|
182 | if (distance < minDistance || minNextRaceCheckPoint == nullptr) |
---|
183 | { |
---|
184 | minDistance = distance; |
---|
185 | minNextRaceCheckPoint = nextRaceCheckPoint; |
---|
186 | |
---|
187 | } |
---|
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(); |
---|
191 | |
---|
192 | } |
---|
193 | if(minNextRaceCheckPoint == nullptr) { orxout()<<"nullptr found @192 SpaceRaceController" << endl;} |
---|
194 | return minNextRaceCheckPoint; |
---|
195 | } |
---|
196 | |
---|
197 | /* |
---|
198 | * called from 'nextPointFind' |
---|
199 | * returns the distance between "currentPosition" and the next static checkpoint that can be reached from "currentCheckPoint" |
---|
200 | */ |
---|
201 | float SpaceRaceController::recCalculateDistance(RaceCheckPoint* currentCheckPoint, const Vector3& currentPosition) |
---|
202 | { |
---|
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()) |
---|
205 | { |
---|
206 | return (currentCheckPoint->getPosition() - currentPosition).length(); |
---|
207 | } |
---|
208 | else |
---|
209 | { |
---|
210 | float minimum = std::numeric_limits<float>::max(); |
---|
211 | for (int checkpointIndex : currentCheckPoint->getNextCheckpoints()) |
---|
212 | { |
---|
213 | int dist_currentCheckPoint_currentPosition = static_cast<int> ((currentPosition- currentCheckPoint->getPosition()).length()); |
---|
214 | |
---|
215 | minimum = std::min(minimum, dist_currentCheckPoint_currentPosition + recCalculateDistance(findCheckpoint(checkpointIndex), currentCheckPoint->getPosition())); |
---|
216 | // minimum of distanz from 'currentPosition' to the next static Checkpoint |
---|
217 | } |
---|
218 | return minimum; |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | /*called by 'tick' |
---|
223 | *adjust chosen way of the Spaceship every "AdjustDistance" because spaceship could be displaced through an other one |
---|
224 | */ |
---|
225 | RaceCheckPoint* SpaceRaceController::adjustNextPoint() |
---|
226 | { |
---|
227 | if (currentRaceCheckpoint_ == nullptr) // no Adjust possible |
---|
228 | |
---|
229 | { |
---|
230 | if(nextRaceCheckpoint_ == nullptr) orxout()<<"nullptr found @218 SpaceRaceController" << endl; |
---|
231 | |
---|
232 | return nextRaceCheckpoint_; |
---|
233 | } |
---|
234 | if ((currentRaceCheckpoint_->getNextCheckpoints()).size() == 1) // no Adjust possible |
---|
235 | |
---|
236 | { |
---|
237 | if(nextRaceCheckpoint_ == nullptr) orxout()<<"nullptr found @223 SpaceRaceController" << endl; |
---|
238 | |
---|
239 | return nextRaceCheckpoint_; |
---|
240 | } |
---|
241 | |
---|
242 | //Adjust possible |
---|
243 | |
---|
244 | return nextPointFind(currentRaceCheckpoint_); |
---|
245 | } |
---|
246 | |
---|
247 | |
---|
248 | |
---|
249 | |
---|
250 | |
---|
251 | |
---|
252 | RaceCheckPoint* SpaceRaceController::findCheckpoint(int index) const |
---|
253 | { |
---|
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; |
---|
268 | } |
---|
269 | |
---|
270 | |
---|
271 | |
---|
272 | /*RaceCheckPoint* SpaceRaceController::addVirtualCheckPoint( RaceCheckPoint* previousCheckpoint, int indexFollowingCheckPoint , const Vector3& virtualCheckPointPosition ) |
---|
273 | { |
---|
274 | orxout()<<"add VCP at"<<virtualCheckPointPosition.x<<", "<<virtualCheckPointPosition.y<<", "<<virtualCheckPointPosition.z<<endl; |
---|
275 | RaceCheckPoint* newTempRaceCheckPoint; |
---|
276 | ObjectList<SpaceRaceManager> list; |
---|
277 | for (ObjectList<SpaceRaceManager>::iterator it = list.begin(); it!= list.end(); ++it) |
---|
278 | { |
---|
279 | newTempRaceCheckPoint = new RaceCheckPoint((*it)); |
---|
280 | } |
---|
281 | newTempRaceCheckPoint->setVisible(false); |
---|
282 | newTempRaceCheckPoint->setPosition(virtualCheckPointPosition); |
---|
283 | newTempRaceCheckPoint->setCheckpointIndex(virtualCheckPointIndex); |
---|
284 | newTempRaceCheckPoint->setLast(false); |
---|
285 | newTempRaceCheckPoint->setNextVirtualCheckpointsAsVector3(Vector3(indexFollowingCheckPoint,-1,-1)); |
---|
286 | |
---|
287 | Vector3 temp = previousCheckpoint->getVirtualNextCheckpointsAsVector3(); |
---|
288 | //orxout()<<"temp bei 0: ="<< temp.x<< temp.y<< temp.z<<endl; |
---|
289 | checkpoints_.insert(checkpoints_.end(), newTempRaceCheckPoint); |
---|
290 | int positionInNextCheckPoint; |
---|
291 | for (int i = 0; i <3; i++) |
---|
292 | { |
---|
293 | if(previousCheckpoint->getVirtualNextCheckpointsAsVector3()[i] == indexFollowingCheckPoint) |
---|
294 | positionInNextCheckPoint=i; |
---|
295 | } |
---|
296 | switch(positionInNextCheckPoint) |
---|
297 | { |
---|
298 | case 0: temp.x=virtualCheckPointIndex; break; |
---|
299 | case 1: temp.y=virtualCheckPointIndex; break; |
---|
300 | case 2: temp.z=virtualCheckPointIndex; break; |
---|
301 | } |
---|
302 | previousCheckpoint->setNextVirtualCheckpointsAsVector3(temp); //Existiert internes Problem bei negativen index fueer next Checkpoint |
---|
303 | virtualCheckPointIndex--; |
---|
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; |
---|
306 | //OrxAssert(virtualCheckPointIndex < -1, "TO much virtual cp"); |
---|
307 | orxout()<<"id: "<< previousCheckpoint->getCheckpointIndex() <<", following:"<<indexFollowingCheckPoint<<" : "<<temp.x<<", "<<temp.y<<", "<<temp.z<<"; "; |
---|
308 | temp=previousCheckpoint->getNextCheckpointsAsVector3(); |
---|
309 | orxout()<<"id: "<< previousCheckpoint->getCheckpointIndex() <<": "<<temp.x<<", "<<temp.y<<", "<<temp.z<<"; "; |
---|
310 | orxout()<<endl; |
---|
311 | return newTempRaceCheckPoint; |
---|
312 | }*/ |
---|
313 | |
---|
314 | SpaceRaceController::~SpaceRaceController() |
---|
315 | { |
---|
316 | if (this->isInitialized()) |
---|
317 | { |
---|
318 | for (int i =-1; i>virtualCheckPointIndex; i--) |
---|
319 | delete findCheckpoint(i); |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | void SpaceRaceController::tick(float dt) |
---|
324 | { |
---|
325 | |
---|
326 | |
---|
327 | if (this->getControllableEntity() == nullptr || this->getControllableEntity()->getPlayer() == nullptr ) |
---|
328 | { |
---|
329 | //orxout()<< this->getControllableEntity() << " in tick"<<endl; |
---|
330 | return; |
---|
331 | } |
---|
332 | //FOR virtual Checkpoints |
---|
333 | if(nextRaceCheckpoint_->getCheckpointIndex() < 0) |
---|
334 | { |
---|
335 | if( distanceSpaceshipToCheckPoint(nextRaceCheckpoint_) < 200) |
---|
336 | { |
---|
337 | currentRaceCheckpoint_=nextRaceCheckpoint_; |
---|
338 | nextRaceCheckpoint_ = nextPointFind(nextRaceCheckpoint_); |
---|
339 | lastPositionSpaceship=this->getControllableEntity()->getPosition(); |
---|
340 | //orxout()<< "CP "<< currentRaceCheckpoint_->getCheckpointIndex()<<" chanched to: "<< nextRaceCheckpoint_->getCheckpointIndex()<<endl; |
---|
341 | } |
---|
342 | } |
---|
343 | |
---|
344 | if (nextRaceCheckpoint_->playerWasHere(this->getControllableEntity()->getPlayer())) |
---|
345 | {//Checkpoint erreicht |
---|
346 | |
---|
347 | currentRaceCheckpoint_ = nextRaceCheckpoint_; |
---|
348 | OrxAssert(nextRaceCheckpoint_, "next race checkpoint undefined"); |
---|
349 | nextRaceCheckpoint_ = nextPointFind(nextRaceCheckpoint_); |
---|
350 | lastPositionSpaceship = this->getControllableEntity()->getPosition(); |
---|
351 | //orxout()<< "CP "<< currentRaceCheckpoint_->getCheckpointIndex()<<" chanched to: "<< nextRaceCheckpoint_->getCheckpointIndex()<<endl; |
---|
352 | } |
---|
353 | |
---|
354 | else if ((lastPositionSpaceship-this->getControllableEntity()->getPosition()).length()/dt > ADJUSTDISTANCE) |
---|
355 | { |
---|
356 | nextRaceCheckpoint_ = adjustNextPoint(); |
---|
357 | if(nextRaceCheckpoint_ == nullptr) orxout()<<"nullptr found @327 SpaceRaceController" << endl; |
---|
358 | |
---|
359 | lastPositionSpaceship = this->getControllableEntity()->getPosition(); |
---|
360 | } |
---|
361 | |
---|
362 | // Abmessung fuer MINDISTANCE gut; |
---|
363 | |
---|
364 | else if((lastPositionSpaceship - this->getControllableEntity()->getPosition()).length()/dt < MINDISTANCE ) |
---|
365 | { |
---|
366 | this->moveToPosition(Vector3(rnd()*100, rnd()*100, rnd()*100)); |
---|
367 | this->spin(); |
---|
368 | //orxout(user_status) << "Mindistance reached" << std::endl; |
---|
369 | return; |
---|
370 | } |
---|
371 | //orxout(user_status) << "dt= " << dt << "; distance= " << (lastPositionSpaceship-this->getControllableEntity()->getPosition()).length() <<std::endl; |
---|
372 | /*lastPositionSpaceship = this->getControllableEntity()->getPosition(); |
---|
373 | |
---|
374 | SpaceRace obj=new SpaceRace(); |
---|
375 | obj.setParentRace(parentRace); |
---|
376 | this->parentRace=obj.parentRace;*/ |
---|
377 | |
---|
378 | this->boostControl(); |
---|
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 | |
---|
390 | this->moveToPosition(nextRaceCheckpoint_->getPosition()); |
---|
391 | |
---|
392 | this->boostControl(); |
---|
393 | } |
---|
394 | |
---|
395 | |
---|
396 | /*void SpaceRaceController::setParentRace(parentRace){ |
---|
397 | this->parentRace=parentRace; |
---|
398 | }*/ |
---|
399 | // True if a coordinate of 'pointToPoint' is smaller then the corresponding coordinate of 'groesse' |
---|
400 | bool SpaceRaceController::vergleicheQuader(const Vector3& pointToPoint, const Vector3& groesse) |
---|
401 | { |
---|
402 | if(std::abs(pointToPoint.x) < groesse.x) |
---|
403 | return true; |
---|
404 | if(std::abs(pointToPoint.y) < groesse.y) |
---|
405 | return true; |
---|
406 | if(std::abs(pointToPoint.z) < groesse.z) |
---|
407 | return true; |
---|
408 | return false; |
---|
409 | |
---|
410 | } |
---|
411 | |
---|
412 | bool SpaceRaceController::directLinePossible(RaceCheckPoint* racepoint1, RaceCheckPoint* racepoint2, const std::vector<StaticEntity*>& allObjects) |
---|
413 | { |
---|
414 | |
---|
415 | Vector3 cP1ToCP2 = (racepoint2->getPosition() - racepoint1->getPosition()) / (racepoint2->getPosition() - racepoint1->getPosition()).length(); //unit Vector |
---|
416 | Vector3 centerCP1 = racepoint1->getPosition(); |
---|
417 | btVector3 positionObject; |
---|
418 | btScalar radiusObject; |
---|
419 | |
---|
420 | for (StaticEntity* object : allObjects) |
---|
421 | { |
---|
422 | for (int everyShape=0; object->getAttachedCollisionShape(everyShape) != nullptr; everyShape++) |
---|
423 | { |
---|
424 | btCollisionShape* currentShape = object->getAttachedCollisionShape(everyShape)->getCollisionShape(); |
---|
425 | if(currentShape == nullptr) |
---|
426 | continue; |
---|
427 | |
---|
428 | currentShape->getBoundingSphere(positionObject,radiusObject); |
---|
429 | Vector3 positionObjectNonBT(positionObject.x(), positionObject.y(), positionObject.z()); |
---|
430 | if((powf((cP1ToCP2.dotProduct(centerCP1-positionObjectNonBT)),2)-(centerCP1-positionObjectNonBT).dotProduct(centerCP1-positionObjectNonBT)+powf(radiusObject, 2))>0) |
---|
431 | { |
---|
432 | return false; |
---|
433 | } |
---|
434 | |
---|
435 | } |
---|
436 | } |
---|
437 | return true; |
---|
438 | |
---|
439 | } |
---|
440 | |
---|
441 | /*void SpaceRaceController::computeVirtualCheckpoint(RaceCheckPoint* racepoint1, RaceCheckPoint* racepoint2, const std::vector<StaticEntity*>& allObjects) |
---|
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; |
---|
447 | |
---|
448 | for (std::vector<StaticEntity*>::iterator it = allObjects.begin(); it != allObjects.end(); ++it) |
---|
449 | { |
---|
450 | for (int everyShape=0; (*it)->getAttachedCollisionShape(everyShape) != nullptr; everyShape++) |
---|
451 | { |
---|
452 | btCollisionShape* currentShape = (*it)->getAttachedCollisionShape(everyShape)->getCollisionShape(); |
---|
453 | if(currentShape == nullptr) |
---|
454 | continue; |
---|
455 | |
---|
456 | currentShape->getBoundingSphere(positionObject,radiusObject); |
---|
457 | Vector3 positionObjectNonBT(positionObject.x(), positionObject.y(), positionObject.z()); |
---|
458 | Vector3 norm_r_CP = cP1ToCP2.crossProduct(centerCP1-positionObjectNonBT); |
---|
459 | |
---|
460 | if(norm_r_CP.length() == 0){ |
---|
461 | Vector3 zufall; |
---|
462 | do{ |
---|
463 | zufall=Vector3(rnd(),rnd(),rnd());//random |
---|
464 | }while((zufall.crossProduct(cP1ToCP2)).length() == 0); |
---|
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)); |
---|
470 | float distanz = std::max(distanzToCP1,distanzToCP2); |
---|
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; |
---|
483 | } |
---|
484 | } |
---|
485 | |
---|
486 | }*/ |
---|
487 | |
---|
488 | /*void SpaceRaceController::placeVirtualCheckpoints(RaceCheckPoint* racepoint1, RaceCheckPoint* racepoint2) |
---|
489 | { |
---|
490 | Vector3 point1 = racepoint1->getPosition(); |
---|
491 | Vector3 point2 = racepoint2->getPosition(); |
---|
492 | std::vector<StaticEntity*> problematicObjects; |
---|
493 | |
---|
494 | ObjectList<StaticEntity> list; |
---|
495 | for (ObjectList<StaticEntity>::iterator it = list.begin(); it!= list.end(); ++it) |
---|
496 | { |
---|
497 | |
---|
498 | if (dynamic_cast<RaceCheckPoint*>(*it) != nullptr) |
---|
499 | { |
---|
500 | continue; |
---|
501 | } // does not work jet |
---|
502 | |
---|
503 | problematicObjects.insert(problematicObjects.end(), *it); |
---|
504 | //it->getScale3D();// vector fuer halbe wuerfellaenge |
---|
505 | } |
---|
506 | |
---|
507 | if(!directLinePossible(racepoint1, racepoint2, problematicObjects)) |
---|
508 | { |
---|
509 | //orxout()<<"From "<<racepoint1->getCheckpointIndex()<<" to "<<racepoint2->getCheckpointIndex()<<"produces: "<< virtualCheckPointIndex<<endl; |
---|
510 | computeVirtualCheckpoint(racepoint1, racepoint2, problematicObjects); |
---|
511 | } |
---|
512 | |
---|
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); |
---|
522 | |
---|
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; |
---|
544 | // if((*it)==nullptr) |
---|
545 | // { orxout()<<"Problempoint 1.1"<<endl; continue;} |
---|
546 | // //TODO: Probably it points on a wrong object |
---|
547 | // for (int everyShape=0; (*it)->getAttachedCollisionShape(everyShape)!=nullptr; everyShape++) |
---|
548 | // { |
---|
549 | // if((*it)->getAttachedCollisionShape(everyShape)->getCollisionShape()==nullptr) |
---|
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; |
---|
577 | // if((*it)==nullptr) |
---|
578 | // { orxout()<<"Problempoint 1"<<endl; continue;} |
---|
579 | // for (int everyShape=0; (*it)->getAttachedCollisionShape(everyShape)!=nullptr; everyShape++) |
---|
580 | // { |
---|
581 | // if((*it)->getAttachedCollisionShape(everyShape)->getCollisionShape()==nullptr) |
---|
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 | // } |
---|
599 | |
---|
600 | }*/ |
---|
601 | } |
---|