00001 #include "Track.h"
00002 #include "Application.h"
00003 #include "Visitor.h"
00004
00005 int Track::s_nextID = 0;
00006
00007 void Track::setID()
00008 {
00009 m_ID = s_nextID;
00010 ++s_nextID;
00011 }
00012
00013 Track::Track(Ogre::SceneManager* _manager) : GameObject()
00014 {
00015 setID();
00016 m_name = "Track" + Ogre::StringConverter::toString(getID());
00017 m_sceneManager = _manager;
00018 Ogre::SceneNode* sceneNode = m_sceneManager->getRootSceneNode()->createChildSceneNode(m_name + "sceneNode");
00019
00020 setSceneNode(sceneNode);
00021 sceneNode->setPosition(0,0,0);
00022
00023 m_spline = new Ogre::SimpleSpline();
00024 m_splineEqualised = 0;
00025 m_manual = 0;
00026 m_trackWidth = 10.0f;
00027 m_equaliseDistance = 1.0f;
00028 setInterpSteps(10);
00029 setClosePoints(3);
00030 }
00031
00032 void Track::redraw()
00033 {
00034
00035 if (m_manual == 0)
00036 {
00037
00038 Ogre::ManualObject* splineLine;
00039
00040 splineLine = m_sceneManager->createManualObject(m_name + "Mesh");
00041
00042
00043
00044
00045 splineLine->setDynamic(true);
00046
00047
00048 getSceneNode()->attachObject(splineLine);
00049
00050
00051 setManual(splineLine);
00052
00053 }
00054
00055
00056 buildTrackFromSpline();
00057
00058
00059 if (getManual() != 0)
00060 {
00061
00062
00063
00064
00065
00066
00067
00068 getManual()->clear();
00069
00070
00071
00072
00073 Ogre::Real interpAmount = getTrackWidth();
00074
00075 Ogre::Real lastj = 0.0;
00076
00077 for (int i = 0; i < getSplineEqualised()->getNumPoints(); ++i)
00078 {
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 }
00095 }
00096
00097 }
00098
00099 Ogre::ManualObject* Track::getManual() const
00100 {
00101 return m_manual;
00102 }
00103
00104 void Track::setManual(Ogre::ManualObject* _val)
00105 {
00106 m_manual = _val;
00107 }
00108
00109 Ogre::SimpleSpline* Track::getSpline() const
00110 {
00111 return m_spline;
00112 }
00113
00114 void Track::setSpline(Ogre::SimpleSpline* _val)
00115 {
00116 m_spline = _val;
00117 recalcControlObjects();
00118 }
00119
00120 Ogre::SimpleSpline* Track::getSplineEqualised() const
00121 {
00122 return m_splineEqualised;
00123 }
00124
00125 void Track::setSplineEqualised(Ogre::SimpleSpline* _val)
00126 {
00127 m_splineEqualised = _val;
00128 }
00129
00130 int Track::getClosePoints() const
00131 {
00132 return m_numClosePoints;
00133 }
00134
00135 void Track::setClosePoints(const int& _val)
00136 {
00137 m_numClosePoints = _val;
00138 }
00139
00140 Ogre::Real Track::getTrackWidth() const
00141 {
00142 return m_trackWidth;
00143 }
00144
00145 void Track::setTrackWidth(const Ogre::Real& _val)
00146 {
00147 m_trackWidth = _val;
00148 }
00149
00150 int Track::getInterpSteps() const
00151 {
00152 return m_interpSteps;
00153 }
00154
00155 void Track::setInterpSteps(const int& _val)
00156 {
00157 m_interpSteps = _val;
00158 m_interpAmount = 1.0f / m_interpSteps;
00159 }
00160
00161 Ogre::Real Track::getInterpAmount() const
00162 {
00163 return m_interpAmount;
00164 }
00165
00166
00167
00168
00169 void Track::equaliseSpline(Ogre::Real _dist)
00170 {
00171 if (_dist == 0.0)
00172 {
00173 _dist = m_equaliseDistance;
00174 }
00175 else
00176 {
00177 m_equaliseDistance = _dist;
00178 }
00179
00180 Ogre::Real lastInterpPoint = 0.0;
00181 Ogre::Real length = 0.0;
00182
00183
00184 int i = 1;
00185
00186 int lastPoint = 0;
00187
00188
00189 Ogre::Vector3 lastAddedPoint = Ogre::Vector3::ZERO;
00190
00191
00192 Ogre::Vector3 startPoint = getSpline()->getPoint(lastPoint);
00193 Ogre::Vector3 endPoint;
00194
00195
00196 int numPoints = getSpline()->getNumPoints() - (getClosePoints() - 1);
00197
00198
00199 Ogre::Real distSquared = Ogre::Math::Pow(_dist, 2);
00200
00201
00202 Ogre::SimpleSpline* equalisedSpline = new Ogre::SimpleSpline();
00203
00204
00205 equalisedSpline->setAutoCalculate(false);
00206
00207
00208 equalisedSpline->addPoint(startPoint);
00209
00210
00211
00212 while (i < numPoints) {
00213
00214 endPoint = getSpline()->getPoint(i);
00215 length = endPoint.squaredDistance(startPoint);
00216
00217
00218 while (length < distSquared && i < numPoints - 1) {
00219 lastPoint = i;
00220 endPoint = getSpline()->getPoint(++i);
00221 length = endPoint.squaredDistance(startPoint);
00222 lastInterpPoint = 0.0;
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233 Ogre::Real partStart = lastInterpPoint;
00234
00235 Ogre::Real partEnd = 1.0;
00236
00237 Ogre::Real partMid;
00238
00239 Ogre::Vector3 partMidPoint;
00240
00241 Ogre::Real partLength;
00242
00243 Ogre::Vector3 partStartPoint = getSpline()->interpolate(lastPoint, partStart);
00244
00245 Ogre::Real newSquaredDist = Ogre::Math::Pow(_dist - startPoint.distance(partStartPoint), 2);
00246
00247 while(1)
00248 {
00249
00250 partMid = (partStart + partEnd) * 0.5;
00251
00252 partMidPoint = getSpline()->interpolate(lastPoint, partMid);
00253
00254 partLength = partMidPoint.squaredDistance(partStartPoint);
00255
00256
00257
00258
00259 if (fabs(partLength - newSquaredDist) < 0.00001f || fabs(partStart - partEnd) < 0.00001f)
00260 break;
00261
00262 if (partLength > newSquaredDist)
00263 {
00264 partEnd = partMid;
00265 }
00266
00267 else
00268 {
00269 partStart = partMid;
00270 }
00271 }
00272
00273
00274 startPoint = partMidPoint;
00275
00276 lastInterpPoint = partMid;
00277
00278
00279
00280
00281
00282
00283
00284
00285 equalisedSpline->addPoint(startPoint);
00286
00287
00288
00289 if (i == numPoints - 1 && fabs(lastInterpPoint - 1.0) < 0.00001f) break;
00290 }
00291
00292 equalisedSpline->recalcTangents();
00293
00294
00295 if (m_splineEqualised != 0) delete m_splineEqualised;
00296
00297
00298 setSplineEqualised(equalisedSpline);
00299 }
00300
00301 void Track::controlObjectMoved()
00302 {
00303 if (getSpline() != 0)
00304 {
00305
00306 equaliseSpline(2.0);
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317 redrawFake();
00318 }
00319 }
00320
00321 void Track::recalcControlObjects()
00322 {
00323
00324 TrackControlObject* controlObject;
00325
00326
00327
00328
00329
00330 int numRequired = getSpline()->getNumPoints();
00331
00332
00333 if (numRequired < m_controlObjects.size())
00334 {
00335 for (int i = numRequired; i < m_controlObjects.size(); ++i)
00336 {
00337 controlObject = m_controlObjects[i];
00338 delete controlObject;
00339 }
00340
00341
00342 m_controlObjects.resize(numRequired);
00343 }
00344
00345 for (int i = 0; i < numRequired; ++i)
00346 {
00347
00348
00349 if (i >= m_controlObjects.size())
00350 {
00351 if (getSpline() != 0)
00352 {
00353 controlObject = new TrackControlObject(i,getSpline()->getPoint(i),this);
00354 if (i > (getClosePoints() - 1) && i < (numRequired - (getClosePoints() + 2)))
00355 {
00356 controlObject->setMoveable(true);
00357 }
00358 else
00359 {
00360 controlObject->setMoveable(false);
00361 }
00362
00363
00364
00365 controlObject->getSceneNode()->setInitialState();
00366 controlObject->getSceneNode()->setVisible(true);
00367 controlObject->setLink(0);
00368 m_controlObjects.push_back(controlObject);
00369 }
00370 }
00371 else
00372 {
00373 controlObject = m_controlObjects[i];
00374
00375 if (i > (getClosePoints() - 1) && i < (numRequired - (getClosePoints() + 2)))
00376 {
00377 controlObject->setMoveable(true);
00378 }
00379 else
00380 {
00381 controlObject->setMoveable(false);
00382 }
00383 controlObject->setPointID(i);
00384
00385
00386 controlObject->getSceneNode()->setPosition(getSpline()->getPoint(i));
00387 Ogre::Vector3 initialPos = getSpline()->getPoint(i);
00388 controlObject->setInitialPosition(initialPos);
00389 controlObject->getSceneNode()->setInitialState();
00390 controlObject->setLink(0);
00391 controlObject->getSceneNode()->setVisible(true);
00392 }
00393 }
00394 }
00395
00396 void Track::recalcPhysicalObjects()
00397 {
00398 TrackPhysicalObject* controlObject = 0;
00399 TrackPhysicalObject* lastObject = 0;
00400
00401
00402
00403
00404
00405 int numRequired = getSplineEqualised()->getNumPoints() - 1;
00406
00407
00408 if (numRequired < m_controlObjectsEqualised.size())
00409 {
00410 for (int i = numRequired; i < m_controlObjectsEqualised.size(); ++i)
00411 {
00412 controlObject = m_controlObjectsEqualised[i];
00413 delete controlObject;
00414 }
00415
00416
00417 m_controlObjectsEqualised.resize(numRequired);
00418 }
00419
00420 for (int i = 0; i < numRequired; ++i)
00421 {
00422
00423
00424 if (i >= m_controlObjectsEqualised.size())
00425 {
00426 if (getSplineEqualised() != 0)
00427 {
00428 controlObject = new TrackPhysicalObject(i,getSplineEqualised()->getPoint(i),this);
00429 controlObject->setMoveable(false);
00430
00431
00432 controlObject->getSceneNode()->setInitialState();
00433 controlObject->setPrevSec(lastObject);
00434 m_controlObjectsEqualised.push_back(controlObject);
00435 }
00436 }
00437 else
00438 {
00439 controlObject = m_controlObjectsEqualised[i];
00440 controlObject->getSceneNode()->resetToInitialState();
00441 controlObject->getSceneNode()->setPosition(getSplineEqualised()->getPoint(i));
00442 controlObject->setPrevSec(lastObject);
00443 controlObject->getSceneNode()->setInitialState();
00444 }
00445 lastObject = controlObject;
00446 }
00447
00448 if (m_controlObjectsEqualised.size() > 0) m_controlObjectsEqualised[0]->setPrevSec(lastObject);
00449 }
00450
00451 void Track::recalc()
00452 {
00453
00454 recalcControlObjects();
00455 recalcPhysicalObjects();
00456
00457
00458 linkClosedSpline();
00459
00460
00461 redraw();
00462 }
00463
00464 void Track::closeSpline()
00465 {
00466 for (int i = 0; i < getClosePoints(); ++i)
00467 {
00468 getSpline()->addPoint(getSpline()->getPoint(i));
00469 }
00470 }
00471
00472 void Track::linkClosedSpline()
00473 {
00474
00475 std::vector<TrackControlObject*>::iterator iterator;
00476
00477 TrackControlObject* first;
00478 TrackControlObject* second;
00479
00480 int i = 0;
00481 while (i < getClosePoints())
00482 {
00483 for (iterator = m_controlObjects.begin(); iterator != m_controlObjects.end(); ++iterator)
00484 {
00485 if ((*iterator)->getPointID() == i)
00486 {
00487 first = (*iterator);
00488 break;
00489 }
00490 }
00491 for (iterator = m_controlObjects.begin(); iterator != m_controlObjects.end(); ++iterator)
00492 {
00493 if ((*iterator)->getPointID() == (getSpline()->getNumPoints() - (getClosePoints() - i)))
00494 {
00495 second = (*iterator);
00496 break;
00497 }
00498 }
00499 first->setLink(second);
00500 second->setLink(first);
00501 second->getSceneNode()->setVisible(false);
00502 ++i;
00503 }
00504
00505 }
00506
00507 void Track::buildTrackFromSpline()
00508 {
00509
00510
00511
00512
00513 Ogre::SimpleSpline* equalisedSpline = getSplineEqualised();
00514
00515
00516 int numPoints = equalisedSpline->getNumPoints() - 1;
00517
00518 if (numPoints > 0)
00519 {
00520
00521
00522 Ogre::Vector3 currPoint;
00523 Ogre::Vector3 nextPoint;
00524
00525
00526 TrackPhysicalObject* currTrackObject = 0;
00527
00528
00529
00530 currTrackObject = m_controlObjectsEqualised[numPoints - 1];
00531
00532
00533
00534
00535 currTrackObject->setTrackAvDirection(Ogre::Vector3::UNIT_X);
00536
00537
00538
00539
00540 currPoint = equalisedSpline->getPoint(currTrackObject->getNextSec()->getPointID());
00541
00542
00543
00544
00545 for (int i = 0; i < numPoints; ++i)
00546 {
00547
00548 currTrackObject = currTrackObject->getNextSec();
00549
00550
00551 nextPoint = equalisedSpline->getPoint(currTrackObject->getNextSec()->getPointID());
00552
00553 Ogre::Vector3 direction = nextPoint - currPoint;
00554
00555 currTrackObject->setTrackAvDirection(direction.normalisedCopy());
00556 currTrackObject->calcAvRotation();
00557
00558 currPoint = nextPoint;
00559
00560 }
00561
00562 for (int i = 0; i < numPoints; ++i)
00563 {
00564
00565
00566
00567 currTrackObject = currTrackObject->getNextSec();
00568 currTrackObject->rebuildSegment();
00569
00570 }
00571 }
00572 }
00573
00574 void Track::finalise()
00575 {
00576
00577
00578 std::vector<TrackControlObject*>::iterator controlIterator;
00579 std::vector<TrackPhysicalObject*>::iterator physicalIterator;
00580
00581
00582 for (controlIterator = m_controlObjects.begin(); controlIterator != m_controlObjects.end(); ++controlIterator)
00583 {
00584
00585 (*controlIterator)->setMoveable(false);
00586
00587 }
00588
00589 for (physicalIterator = m_controlObjectsEqualised.begin(); physicalIterator != m_controlObjectsEqualised.end(); ++physicalIterator)
00590 {
00591
00592 (*physicalIterator)->createPhysicalMesh();
00593 }
00594
00595
00596 }
00597
00598 void Track::buildIntroTrackType(int _length, Ogre::Real _distBetween, Ogre::Vector3 _startPos, Ogre::Real _distSideways)
00599 {
00600
00601
00602 getSpline()->addPoint(_startPos);
00603
00604 Ogre::Vector3 lastPoint = _startPos;
00605
00606 for (int i = 1; i < _length; ++i)
00607 {
00608 Ogre::Vector3 newPoint = Ogre::Vector3::ZERO;
00609
00610 newPoint.x = i * _distBetween;
00611
00612
00613
00614
00615
00616
00617 getSpline()->addPoint(newPoint);
00618 lastPoint = newPoint;
00619 }
00620
00621
00622 getSpline()->addPoint(lastPoint + (Ogre::Vector3::UNIT_X * 30));
00623 getSpline()->addPoint(lastPoint + (Ogre::Vector3::UNIT_X * 60));
00624 getSpline()->addPoint(lastPoint + (Ogre::Vector3::UNIT_X * 90));
00625 getSpline()->addPoint(lastPoint + (Ogre::Vector3::UNIT_X * 120));
00626
00627 Visitor* visitor = new Visitor();
00628
00629 GameObject* gameObject = new GameObject();
00630
00631 Ogre::SceneNode* sceneNode = getSceneNode()->createChildSceneNode(m_name + "tunnelNode");
00632 sceneNode->setPosition(lastPoint);
00633 gameObject->setSceneNode(sceneNode);
00634
00635 Ogre::Entity* sceneryEntity = m_sceneManager->createEntity(m_name + "tunnelEntity","tunnel.mesh");
00636
00637
00638 gameObject->setEntity(sceneryEntity);
00639 sceneryEntity->visitRenderables(visitor);
00640 gameObject->getSceneNode()->setDirection(Ogre::Vector3::UNIT_X);
00641 gameObject->setFriction(50.0f);
00642 gameObject->setName(m_name + "createdTunnelGameObject");
00643
00644 gameObject->makePhysicalConcave();
00645
00646
00647 gameObject = new GameObject();
00648
00649 sceneNode = getSceneNode()->createChildSceneNode(m_name + "startObjNode");
00650 sceneNode->setPosition(_startPos);
00651 gameObject->setSceneNode(sceneNode);
00652
00653 sceneryEntity = m_sceneManager->createEntity(m_name + "startObjEntity","startLine.mesh");
00654
00655
00656 gameObject->setEntity(sceneryEntity);
00657 sceneryEntity->setMaterialName("startLine");
00658 sceneryEntity->visitRenderables(visitor);
00659 gameObject->getSceneNode()->setDirection(Ogre::Vector3::UNIT_X);
00660 gameObject->setFriction(50.0f);
00661 gameObject->setName(m_name + "createdstartObjGameObject");
00662 gameObject->makePhysicalConcave();
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684 setTrackWidth(15.0);
00685 equaliseSpline(_distBetween);
00686 recalc();
00687 finalise();
00688
00689 }
00690
00692 void Track::createFromFakeTrack(Ogre::Vector3 _startPos, Track* _fakeTrack)
00693 {
00694
00695
00696
00697
00698
00699
00700
00701 setSpline(_fakeTrack->getSpline());
00702
00703 for (int i = 0; i < getSpline()->getNumPoints(); ++i)
00704 {
00705 Ogre::Vector3 oldPos = getSpline()->getPoint(i);
00706 Ogre::Vector3 newPos = Ogre::Vector3::ZERO;
00707 newPos.x = oldPos.x * 10.0f;
00708 newPos.z = oldPos.y * 10.0f;
00709 getSpline()->updatePoint(i,newPos);
00710 }
00711
00712
00713 setTrackWidth(15.0);
00714 equaliseSpline(20.0);
00715 recalc();
00716 hideNodes();
00717 finalise();
00718
00719
00720 }
00721
00724 void Track::regenerateFakeTrack(int _val)
00725 {
00726
00727 delete m_spline;
00728 m_spline = new Ogre::SimpleSpline();
00729
00730 getSpline()->addPoint(Ogre::Vector3(0.0,0.0,0));
00731 getSpline()->addPoint(Ogre::Vector3(1.0,0.0,0));
00732 getSpline()->addPoint(Ogre::Vector3(2.0,0.0,0));
00733 getSpline()->addPoint(Ogre::Vector3(3,0.0,0));
00734 getSpline()->addPoint(Ogre::Vector3(6,-3.0,0));
00735
00736 if (_val == 1)
00737 {
00738 getSpline()->addPoint(Ogre::Vector3(0.0,-9.0,0.0));
00739 }
00740 if (_val == 2)
00741 {
00742 getSpline()->addPoint(Ogre::Vector3(3.0,-6.0,0.0));
00743 getSpline()->addPoint(Ogre::Vector3(-3.0,-6.0,0.0));
00744 }
00745 if (_val == 3)
00746 {
00747 getSpline()->addPoint(Ogre::Vector3(3.0,-6.0,0.0));
00748 getSpline()->addPoint(Ogre::Vector3(0.0,-9.0,0.0));
00749 getSpline()->addPoint(Ogre::Vector3(-3.0,-6.0,0.0));
00750 }
00751 if (_val == 4)
00752 {
00753 getSpline()->addPoint(Ogre::Vector3(3.0,-6.0,0.0));
00754 getSpline()->addPoint(Ogre::Vector3(6.0,-6.0,0.0));
00755 getSpline()->addPoint(Ogre::Vector3(0.0,-9.0,0.0));
00756 getSpline()->addPoint(Ogre::Vector3(-3.0,-6.0,0.0));
00757 }
00758 if (_val == 5)
00759 {
00760 getSpline()->addPoint(Ogre::Vector3(3.0,-6.0,0.0));
00761 getSpline()->addPoint(Ogre::Vector3(6.0,-6.0,0.0));
00762 getSpline()->addPoint(Ogre::Vector3(0.0,-9.0,0.0));
00763 getSpline()->addPoint(Ogre::Vector3(-6.0,-6.0,0.0));
00764 getSpline()->addPoint(Ogre::Vector3(-3.0,-6.0,0.0));
00765 }
00766 if (_val == 6)
00767 {
00768 getSpline()->addPoint(Ogre::Vector3(9.0,0.0,0.0));
00769 getSpline()->addPoint(Ogre::Vector3(3.0,-6.0,0.0));
00770 getSpline()->addPoint(Ogre::Vector3(6.0,-6.0,0.0));
00771 getSpline()->addPoint(Ogre::Vector3(0.0,-9.0,0.0));
00772 getSpline()->addPoint(Ogre::Vector3(-6.0,-6.0,0.0));
00773 getSpline()->addPoint(Ogre::Vector3(-3.0,-6.0,0.0));
00774 }
00775 if (_val == 7)
00776 {
00777 getSpline()->addPoint(Ogre::Vector3(9.0,0.0,0.0));
00778 getSpline()->addPoint(Ogre::Vector3(3.0,-6.0,0.0));
00779 getSpline()->addPoint(Ogre::Vector3(6.0,-6.0,0.0));
00780 getSpline()->addPoint(Ogre::Vector3(0.0,-9.0,0.0));
00781 getSpline()->addPoint(Ogre::Vector3(-6.0,-6.0,0.0));
00782 getSpline()->addPoint(Ogre::Vector3(-3.0,-6.0,0.0));
00783 getSpline()->addPoint(Ogre::Vector3(-9.0,0.0,0.0));
00784 }
00785 if (_val == 8)
00786 {
00787 getSpline()->addPoint(Ogre::Vector3(9.0,0.0,0.0));
00788 getSpline()->addPoint(Ogre::Vector3(3.0,-6.0,0.0));
00789 getSpline()->addPoint(Ogre::Vector3(6.0,-6.0,0.0));
00790 getSpline()->addPoint(Ogre::Vector3(0.0,-9.0,0.0));
00791 getSpline()->addPoint(Ogre::Vector3(-6.0,-12.0,0.0));
00792 getSpline()->addPoint(Ogre::Vector3(-6.0,-6.0,0.0));
00793 getSpline()->addPoint(Ogre::Vector3(-3.0,-6.0,0.0));
00794 getSpline()->addPoint(Ogre::Vector3(-9.0,0.0,0.0));
00795 }
00796 if (_val == 9)
00797 {
00798 getSpline()->addPoint(Ogre::Vector3(9.0,0.0,0.0));
00799 getSpline()->addPoint(Ogre::Vector3(3.0,-6.0,0.0));
00800 getSpline()->addPoint(Ogre::Vector3(6.0,-6.0,0.0));
00801 getSpline()->addPoint(Ogre::Vector3(6.0,-12.0,0.0));
00802 getSpline()->addPoint(Ogre::Vector3(0.0,-9.0,0.0));
00803 getSpline()->addPoint(Ogre::Vector3(-6.0,-12.0,0.0));
00804 getSpline()->addPoint(Ogre::Vector3(-6.0,-6.0,0.0));
00805 getSpline()->addPoint(Ogre::Vector3(-3.0,-6.0,0.0));
00806 getSpline()->addPoint(Ogre::Vector3(-9.0,0.0,0.0));
00807 }
00808 getSpline()->addPoint(Ogre::Vector3(-6,-3.0,0));
00809 getSpline()->addPoint(Ogre::Vector3(-3,0.0,0));
00810 getSpline()->addPoint(Ogre::Vector3(-2,0.0,0));
00811 getSpline()->addPoint(Ogre::Vector3(-1,0.0,0));
00812 getSpline()->addPoint(Ogre::Vector3(-0,0.0,0));
00813
00814 closeSpline();
00815
00816 recalcControlObjects();
00817 equaliseSpline(2.0);
00818
00819
00820
00821
00822 linkClosedSpline();
00823
00824
00825 redrawFake();
00826 }
00827
00828
00829 int Track::getTotalLaps()
00830 {
00831 return m_totalLaps;
00832 }
00833 int Track::getCurrentLap()
00834 {
00835 return m_currentLap;
00836 }
00837
00838
00839 Ogre::Real Track::getTotalTime()
00840 {
00841 return m_totalTime;
00842 }
00843 Ogre::Real Track::getCurrentLapTime()
00844 {
00845 return m_currentLapTime;
00846 }
00847 Ogre::Real Track::getBestLapTime()
00848 {
00849 return m_bestLapTime;
00850 }
00851
00852
00853 void Track::setTotalLaps(int _val)
00854 {
00855 m_totalLaps = _val;
00856 }
00857 void Track::setCurrentLap(int _val)
00858 {
00859 m_currentLap = _val;
00860 }
00861
00862
00863 void Track::setTotalTime(Ogre::Real _val)
00864 {
00865 m_totalTime = _val;
00866 }
00867 void Track::setCurrentLapTime(Ogre::Real _val)
00868 {
00869 m_currentLapTime = _val;
00870 }
00871 void Track::setBestLapTime(Ogre::Real _val)
00872 {
00873 m_bestLapTime = _val;
00874 }
00875
00876 void Track::startRace()
00877 {
00878 Application::getSingleton()->setGameState(RACING);
00879 m_currentLap = 0;
00880 m_totalTime = 0;
00881 m_currentLapTime = 0;
00882 m_bestLapTime = 0;
00883 updateLapCount();
00884 updateLapTimes();
00885 }
00886
00888 void Track::endRace()
00889 {
00890 Application::getSingleton()->setGameState(RACE_FINISH);
00891 }
00892
00894 void Track::updateLapCount()
00895 {
00896 m_currentLap += 1;
00897 if (m_currentLap > m_totalLaps)
00898 {
00899 endRace();
00900 }
00901 else
00902 {
00903 Application::getSingleton()->getGUI()->updateLapCount(m_currentLap, m_totalLaps);
00904 }
00905 }
00906
00908 void Track::updateLapTimes()
00909 {
00910 Application::getSingleton()->getGUI()->updateTimer(m_bestLapTime, m_currentLapTime, m_totalTime);
00911 }
00912
00913 bool Track::move(Ogre::Real _time)
00914 {
00915 if (Application::getSingleton()->getGameState() == RACING)
00916 {
00917 m_totalTime += _time;
00918 m_currentLapTime += _time;
00919 updateLapTimes();
00920 }
00921 return false;
00922 }
00923
00925 void Track::informOfContact(TrackPhysicalObject* _val)
00926 {
00927 m_encounters.push_back(_val);
00928 m_encounters.sort();
00929 m_encounters.unique();
00930
00931
00932 if (m_encounters.size() == m_controlObjectsEqualised.size() -1)
00933 {
00934 std::vector<TrackPhysicalObject*>::iterator physicalIterator;
00935
00936 for (physicalIterator = m_controlObjectsEqualised.begin(); physicalIterator != m_controlObjectsEqualised.end(); ++physicalIterator)
00937 {
00938 (*physicalIterator)->resetContactCheck();
00939 }
00940 if ((m_bestLapTime < 0.01) || (m_currentLapTime < m_bestLapTime))
00941 {
00942 m_bestLapTime = m_currentLapTime;
00943 }
00944 m_currentLapTime = 0;
00945 updateLapTimes();
00946 updateLapCount();
00947 m_encounters.clear();
00948 }
00949
00950 }
00951
00952 void Track::redrawFake()
00953 {
00954
00955 if (m_manual == 0)
00956 {
00957
00958 Ogre::ManualObject* splineLine;
00959
00960 splineLine = m_sceneManager->createManualObject(m_name + "Mesh");
00961
00962
00963
00964
00965 splineLine->setDynamic(true);
00966
00967
00968 getSceneNode()->attachObject(splineLine);
00969
00970
00971 setManual(splineLine);
00972
00973 }
00974
00975
00976
00977
00978
00979 if (getManual() != 0)
00980 {
00981
00982
00983
00984
00985
00986
00987
00988 getManual()->clear();
00989
00990
00991
00992
00993 Ogre::Real interpAmount = 0.2f;
00994
00995 Ogre::Real lastj = 0.0;
00996
00997 for (int i = 0; i < getSplineEqualised()->getNumPoints(); ++i)
00998 {
00999
01000 getManual()->begin("BaseWhiteNoLighting", Ogre::RenderOperation::OT_LINE_LIST);
01001
01002
01003 for (Ogre::Real j = 0.0; j <= 1.0 + interpAmount; j += interpAmount)
01004 {
01005
01006 getManual()->position(getSplineEqualised()->interpolate(i,lastj));
01007 getManual()->position(getSplineEqualised()->interpolate(i,j));
01008 lastj = j;
01009 }
01010 lastj = 0.0;
01011
01012 getManual()->end();
01013 }
01014 }
01015
01016 }
01017
01018 Ogre::SceneManager* Track::getSceneManager()
01019 {
01020 return m_sceneManager;
01021 }
01022
01023 void Track::hideNodes()
01024 {
01025 std::vector<TrackControlObject*>::iterator iterator;
01026
01027 for (iterator = m_controlObjects.begin(); iterator != m_controlObjects.end(); ++iterator)
01028 {
01029 (*iterator)->getSceneNode()->setVisible(false);
01030 }
01031
01032 }