Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/track_manager.cc @ 3802

Last change on this file since 3802 was 3710, checked in by patrick, 20 years ago

orxonox/trunk: added the TestBullet to framework, made some doxygen, works

File size: 25.3 KB
RevLine 
[3311]1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
[3433]12   main-programmer: Benjamin Grauer
[3311]13   co-programmer: ...
14*/
15
[3591]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_TRACK_MANAGER
[3311]17
18#include "track_manager.h"
[3495]19
[3608]20#include "base_object.h"
[3433]21#include "p_node.h"
[3528]22#include "track_node.h"
[3608]23#include "stdincl.h"
24#include "list.h"
[3528]25
[3608]26
27
[3495]28#include <stdarg.h>
29
[3311]30using namespace std;
31
[3331]32/**
33   \brief initializes a TrackElement (sets the default values)
34*/
35TrackElement::TrackElement(void)
36{
[3332]37  this->isFresh = true;
[3354]38  this->isHotPoint = false;
[3331]39  this->isSavePoint = false;
40  this->isFork = false;
41  this->isJoined = false;
[3356]42  this->mainJoin = false;
[3331]43  this->ID = -1;
[3588]44  this->startingTime = 0;
45  this->duration = TMAN_DEFAULT_DURATION;
[3433]46  this->endTime = 1;
47  this->jumpTime = 0;
[3596]48  this->width = TMAN_DEFAULT_WIDTH;
[3331]49  this->nodeCount = 0;
[3348]50  this->childCount = 0;
[3332]51  this->name = NULL;
[3331]52  this->curve = NULL;
53  this->children = NULL;
[3527]54
55  this->history = NULL;
56
[3522]57  this->condFunc = &TrackElement::random;
[3331]58}
[3311]59
[3331]60/**
[3332]61    \brief destroys all alocated memory)
[3331]62    \todo eventually when deleting a TrackElement you would not like to delete all its preceding TrackElements
63*/
64TrackElement::~TrackElement(void)
65{
[3332]66  if (this->name)
67    delete []name;
[3331]68  if (this->curve)
69    delete this->curve;
[3356]70  if ((!this->isJoined &&this->childCount > 0) || (this->isJoined && this->mainJoin))
[3331]71    {
[3661]72      tIterator<TrackElement>* iterator = this->children->getIterator();
73      TrackElement* enumElem = iterator->nextElement();
[3594]74      while (enumElem)
75        {
76          delete enumElem;
[3661]77          enumElem = iterator->nextElement();
[3594]78        }
[3661]79      delete iterator;
[3356]80      delete this->children;
[3331]81    }
82}
83
[3332]84/**
85   \brief Searches through all the TrackElements for trackID.
86   \param trackID The ID to search for.
87   \returns The TrackElement if Found, NULL otherwise.
88   
89   \todo make this more modular, to search for different things
90*/
91TrackElement* TrackElement::findByID(unsigned int trackID)
92{
93  // return if Found.
94  if (this->ID == trackID)
95    return this;
96  // search on.
97  if (this->childCount > 0)
[3594]98    {
[3661]99      tIterator<TrackElement>* iterator = this->children->getIterator();
100      TrackElement* enumElem = iterator->nextElement();
[3594]101      TrackElement* tmpElem;
102      while (enumElem)
103        {
104          if ((tmpElem = enumElem->findByID(trackID)))
105            return tmpElem;
[3661]106          enumElem = iterator->nextElement();
[3594]107        }
[3661]108      delete iterator;
[3594]109    }
110  else 
111    return NULL;
[3332]112}
[3331]113
114
[3522]115/**
116   \brief checks if there are any BackLoops in the Track
117   \param trackElem the trackElement to check about
118   it simply does this by looking if the current trackElem is found again somewhere else in the Track
119*/
120bool TrackElement::backLoopCheck(TrackElement* trackElem)
121{
122  if (this->childCount == 0)
123    return true;
124  else
125    {
[3661]126      tIterator<TrackElement>* iterator = this->children->getIterator();
127      TrackElement* enumElem = iterator->nextElement();
[3594]128      while (enumElem)
129        {
130          if(!enumElem->backLoopCheck(trackElem))
131            return false;
[3661]132          enumElem = iterator->nextElement();
[3594]133        }
[3661]134      delete iterator;
[3522]135     
136      return true;
137    }
138}
[3331]139
[3522]140/**
[3594]141   \param childNumber which child to return
142   \returns the n-the children (starting at 0)
143*/
144TrackElement* TrackElement::getChild(int childCount)
145{
146  if (this->childCount == 0)
147    return NULL;
148  if (childCount > this->childCount)
149    childCount = this->childCount;
150 
151  TrackElement* enumElem = this->children->enumerate();
152  for (int i = 0; i < childCount; i++)
153    enumElem = this->children->nextElement();
154  return enumElem;
155}
156
157/**
[3588]158   \param name the Name to set.
159*/
160void TrackElement::setName(const char* name)
161{
162  //  delete the old name
163  if (this->name)
164    delete []this->name;
165  // if a name was given already.
166  if (name)
167    {
168      this->name = new char[strlen(name)+1];
169      strcpy(this->name, name);
170    }
171  else 
172    this->name = NULL;
173}
174
175/**
176   \returns The name of this TrackElement
177*/
178char* TrackElement::getName(void) const
179{
180  return this->name;
181}
182
[3593]183/**
184   \brief prints out debug information about this TrackElement
185*/
186void TrackElement::debug(void)
187{
188  PRINT(0)("--== TrackElement:%i ==--", this->ID);
189  if(this->getName())
190    PRINT(0)("Name: %s::", this->getName());
191  if(this->isFresh)
192    PRINT(0)("  -- has not jet eddited in any way --\n");
193  PRINT(0)("\n   TimeTable: startingTime=%f; endTime=%f; duration=%f; jumpTime=%f\n", this->startingTime, this->endTime, this->duration, this->jumpTime);
194  PRINT(0)("   consists of %d Points\n", this->nodeCount);
195  if (this->childCount == 0)
196    PRINT(0)("   has no child\n");
197  else if (this->childCount == 1)
[3594]198    PRINT(0)("   has 1 child: =%d=\n", this->getChild(0)->ID);
[3593]199  else if (this->childCount > 1)
200    {
201      PRINT(0)("   has %d children: ", this->childCount);
[3594]202      TrackElement* enumElem = this->children->enumerate();
203      while (enumElem)
204        {
205          PRINT(0)("=%d= ", enumElem->ID);
206          enumElem = this->children->nextElement();
207        }
[3593]208      PRINT(0)("\n");
209    }
210 
211  if(this->isHotPoint)
212    PRINT(0)("   is a special Point:\n");
213  if(this->isSavePoint)
214    PRINT(0)("    is a SavePoint\n");
215  if(this->isFork)
216    {
217      PRINT(0)("    is A Fork with with %d children.\n", this->childCount);
218    }
219  if(this->isJoined)
220    PRINT(0)("   is Joined at the End\n");
221 
222  if(!this->backLoopCheck(this)) /* this should not happen */
223    PRINT(2)(" THERE IS A BACKLOOP TO THIS ELEMENT\n");
224}
[3588]225
226/**
[3522]227   \brief CONDITION that chooses the first child for the decision (static)
228   \param nothing Nothing in this function
229   \returns the chosen child
230*/
231int TrackElement::lowest(void* nothing)
232{
233  return 0;
234}
[3332]235
[3522]236/**
237   \brief CONDITION that chooses the last child for the decision (static)
238   \param nothing Nothing in this function
239   \returns the chosen child
240*/
241int TrackElement::highest(void* nothing)
242{ 
243  return this->childCount-1;
244}
[3332]245
[3522]246/**
247   \brief CONDITION that chooses a random child for the decision (static)
248   \param nothing Nothing in this function
249   \returns the chosen child
250*/
251int TrackElement::random(void* nothing)
252{
253  int i = (int)floor ((float)rand()/(float)RAND_MAX * (float)this->childCount);
254  if (i >= this->childCount)
255    return this->childCount-1;
256  else 
257    return i;
258}
259
260/**
261   \brief CONDITION that chooses child 0, if the node(probably Player)
262   is left of its parent (z<0)) and 1/right otherwise.
263   \param node The node to act upon.
264   \returns the chosen child
265*/
266int TrackElement::leftRight(void* node)
267{
268  PNode* tmpNode = (PNode*)node;
269
[3675]270  if (tmpNode->getRelCoor()->z < 0)
[3522]271    return 0;
272  else 
273    return 1;
274}
275
276
277/**
278   \brief CONDITION that chooses the child, that has the nearest distance to the node (probably player).
279   \param node The node to act upon.
280   \returns the chosen child
281
282   This is rather dangerous, because one must carefully set the points on the curve.
283   The best Way is to set the nodes as wide away of each other as possible,
284   but take into consideration, that if the nodes are to far from a center node, the center will be chosen.
285   (play with this!!).
286*/
287int TrackElement::nearest(void* node)
288{
289  PNode* tmpNode = (PNode*)node;
290
[3675]291  Vector nodeRelCoord = *tmpNode->getRelCoor();
[3522]292  float minDist = 100000000;
[3594]293  int childNumber = 0;
294  int i = 0;
295
296  TrackElement* enumElem = this->children->enumerate();
297  while (enumElem)
[3522]298    {
[3594]299      float dist = (nodeRelCoord - enumElem->curve->getNode(4)).len();
[3522]300      if (dist < minDist)
301        {
302          minDist = dist;
[3594]303          childNumber = i;
[3522]304        }
[3594]305      i++;
306      enumElem = this->children->nextElement();
[3522]307    }
[3594]308
309  PRINTF(4)("PathDecision with nearest algorithm: %d\n", childNumber);
310  return childNumber;
[3522]311}
312
313
[3599]314////////////////////////
315///// TRACKMANAGER /////
316////////////////////////
[3311]317/**
318   \brief standard constructor
319
320*/
[3354]321TrackManager::TrackManager(void)
[3311]322{
[3594]323  this->setClassName("TrackManager");
[3528]324 
[3594]325  TrackManager::singletonRef = this;
[3331]326
327  PRINTF(3)("Initializing the TrackManager\n");
[3348]328  this->firstTrackElem = new TrackElement();
329  this->firstTrackElem->ID = 1;
[3331]330  this->currentTrackElem = firstTrackElem;
331  this->localTime = 0;
332  this->maxTime = 0;
[3348]333  this->trackElemCount = 1;
[3594]334  this->setBindSlave(this->trackNode = new TrackNode());
[3311]335}
336
337
338/**
[3331]339   \brief standard destructor
[3311]340*/
[3354]341TrackManager::~TrackManager(void)
[3330]342{
[3331]343  PRINTF(3)("Destruct TrackManager\n");
[3311]344
[3594]345  PRINTF(4)("Deleting all the TrackElements\n");
[3331]346  delete this->firstTrackElem;
[3335]347
[3331]348  // we do not have a TrackManager anymore
[3594]349  TrackManager::singletonRef = NULL;
[3330]350}
351
[3543]352//! Singleton Reference to TrackManager
[3331]353TrackManager* TrackManager::singletonRef = NULL;
354
[3330]355/**
[3331]356   \returns The reference on the TrackManager.
357
358   If the TrackManager does not exist, it will be created.
359*/
360TrackManager* TrackManager::getInstance(void) 
361{
[3594]362  if (!TrackManager::singletonRef)
363    TrackManager::singletonRef = new TrackManager();
364  return TrackManager::singletonRef;
[3331]365}
366
367/**
[3335]368   \brief reserves Space for childCount children
369   \param childCount The Count of children to make space for.
370*/
371void TrackManager::initChildren(unsigned int childCount)
372{
373  this->currentTrackElem->childCount = childCount;
[3433]374  this->currentTrackElem->mainJoin = true;
[3594]375  this->currentTrackElem->children =  new tList<TrackElement>();
376  for (int i = 0; i < childCount; i++)
[3348]377    {
[3594]378      TrackElement* newElem = new TrackElement();
379      this->currentTrackElem->children->add(newElem);
380      newElem->ID = ++trackElemCount;
381      newElem->startingTime = this->currentTrackElem->endTime + this->currentTrackElem->jumpTime;
382      this->addPoint(this->currentTrackElem->curve->getNode(this->currentTrackElem->curve->getNodeCount()), this->currentTrackElem->getChild(i));
[3348]383    }
[3588]384  if (childCount == 1)
[3594]385    this->currentTrackElem->getChild(0)->setName(this->currentTrackElem->getName());
[3335]386}
387
388/**
[3330]389   \brief Searches for a given trackID.
390   \param trackID the trackID to search for.
[3332]391   \returns The TrackElement #trackID if found, NULL otherwise.
[3330]392*/
[3332]393TrackElement* TrackManager::findTrackElementByID(unsigned int trackID) const
[3330]394{
[3332]395  return firstTrackElem->findByID(trackID);
[3330]396}
397
398// INITIALIZE //
399
400/**
401   \brief Sets the trackID we are working on.
402   \param trackID the trackID we are working on
403*/
[3332]404void TrackManager::workOn(unsigned int trackID)
[3330]405{
[3355]406  TrackElement* tmpElem = findTrackElementByID(trackID);
407  if (tmpElem)
408    this->currentTrackElem = tmpElem;
409  else
[3594]410    PRINTF(2)("TrackElement not Found, leaving unchanged\n");
411  PRINTF(4)("now Working on %d\n", this->currentTrackElem->ID);
[3355]412
[3330]413}
414
415/**
416   \brief Sets the Type of the Curve
[3522]417   \param curveType The Type to set
418   \param trackElem the TrackElement that should get a new Curve.
[3330]419*/
[3433]420void TrackManager::setCurveType(CurveType curveType, TrackElement* trackElem)
[3330]421{
[3433]422  if (!trackElem->isFresh)
[3332]423    {
424      PRINTF(2)("It is not possible to change the type of a Curve after you have have appended some points to it\n");
425      return;
426    }
[3588]427  this->curveType = curveType;
[3332]428  switch (curveType)
429    {
430    case BEZIERCURVE:
[3433]431      trackElem->curve = new BezierCurve();
[3332]432      break;
[3595]433
[3332]434    }
[3330]435}
436
437/**
[3333]438   \brief Sets the duration of the current path in seconds.
439   \param time The duration in seconds.
[3330]440*/
441
[3333]442void TrackManager::setDuration(float time)
[3330]443{
[3333]444  this->currentTrackElem->duration = time;
[3433]445  this->currentTrackElem->endTime = this->currentTrackElem->startingTime + time;
[3330]446}
447
448/**
449   \brief adds a point to the current TrackElement
450   \param newPoint The point to add.
451*/
[3333]452bool TrackManager::addPoint(Vector newPoint)
[3330]453{
[3352]454  return this->addPoint(newPoint, this->currentTrackElem);
455}
456
457/**
458   \brief adds a point to trackElem
459   \param newPoint The point to add.
460   \param trackElem The TrackElement to add the Point to
461*/
462bool TrackManager::addPoint(Vector newPoint, TrackElement* trackElem)
463{
464  if (trackElem->isFresh)
[3332]465    {
[3588]466      this->setCurveType(TMAN_DEFAULT_CURVETYPE, trackElem);
[3352]467      trackElem->isFresh = false;
[3332]468    }
[3352]469  trackElem->curve->addNode(newPoint);
470  trackElem->nodeCount++;
[3330]471}
472
473/**
474   \brief adds save/splitpoint.
475   \param newPoint The point to add.
[3333]476   \returns A Pointer to a newly appended Curve
[3330]477*/
[3333]478int TrackManager::addHotPoint(Vector newPoint)
[3330]479{
[3594]480  PRINTF(4)("setting up a HotPoint\n");
[3332]481  if (this->currentTrackElem->isFresh)
482    {
483      this->setCurveType(BEZIERCURVE);
484      this->currentTrackElem->isFresh = false;
485    }
[3330]486
[3332]487  // \todo HotPoint Handling.
488  this->currentTrackElem->curve->addNode(newPoint);
[3351]489  this->currentTrackElem->nodeCount++;
[3354]490  this->initChildren(1);
[3594]491  this->currentTrackElem = this->currentTrackElem->getChild(0);
[3330]492}
493
494/**
495   \brief Sets the last HotPoint into a savePoint.
[3333]496   \returns A Pointer to a newly appended Curve
[3330]497   
498   If no HotPoint was defined the last added Point will be rendered into a savePoint. \n
499   If the HotPoint was defined as a fork the Point will \b not be set into a savePoint.
500*/
[3333]501int TrackManager::setSavePoint(void)
[3330]502{
[3594]503  PRINTF(4)("setting up a SavePoint.\n");
[3332]504  if (this->currentTrackElem->isFork || this->currentTrackElem->isSavePoint)
[3594]505    {
506      PRINTF(2)("%d is already finished \n", currentTrackElem->ID);
507      return this->currentTrackElem->getChild(0)->ID;
508    }
[3332]509  this->currentTrackElem->isSavePoint = true;
[3354]510  this->currentTrackElem->isHotPoint = true;
[3332]511
[3335]512  this->initChildren(1);
[3594]513  this->currentTrackElem = this->currentTrackElem->getChild(0);
[3330]514}
515
516/**
517   \brief adds some interessting non-linear movments through the level.
518   \param count The Count of childrens the current HotPoint will have.
519
520   If no HotPoint was defined the last added Point will be rendered into a fork. \n
521   If the HotPoint was defined as a savePoint the Point will \b not be set into a fork.
522*/
[3332]523void TrackManager::fork(unsigned int count, ...)
[3330]524{
[3351]525  int* trackIDs = new int[count];
526  this->forkV(count, trackIDs);
[3332]527  va_list ID;
528  va_start (ID, count);
529  for(int i = 0; i < count; i++)
530    {
[3351]531      *va_arg (ID, int*) = trackIDs[i];
[3332]532    }
[3351]533  va_end(ID); 
[3332]534  delete []trackIDs;
[3330]535}
536
537/**
538   \brief adds some interessting non-linear movments through the level.
539   \param count The Count of childrens the current HotPoint will have.
540   \param trackIDs A Pointer to an Array of ints which will hold the trackID's (the user will have to reserve space for this).
541
542   \see void TrackManager::fork(int count, ...)
[3332]543
544   \todo initialisation is wrong!! also in setSavePoint.
[3330]545*/
[3332]546void TrackManager::forkV(unsigned int count, int* trackIDs)
[3330]547{
[3594]548  PRINTF(4)("Forking with %d children\n", count);
[3332]549  if (this->currentTrackElem->isSavePoint)
550    return;
551  this->currentTrackElem->isFork = true;
[3354]552  this->currentTrackElem->isHotPoint = true;
[3351]553  for(int i = 0; i < count; i++)
554    trackIDs[i]=this->trackElemCount+1+i;
[3335]555  this->initChildren(count);
[3330]556}
557
558/**
559   \brief decides under what condition a certain Path will be chosen.
[3522]560   \param cond the CONDITION of the decision
561   \param subject the Subject that will be decided upon with CONDITION cond.
562*/
563void TrackManager::condition(CONDITION cond, void* subject)
564{
565  this->condition(this->currentTrackElem->ID, cond, subject);
566}
567/**
568   \brief decides under what condition a certain Path will be chosen.
[3330]569   \param groupID the ID on which to choose the preceding move
[3522]570   \param cond the CONDITION of the decision
571   \param subject the Subject that will be decided upon with CONDITION cond.
[3330]572*/
[3522]573void TrackManager::condition(unsigned int groupID, CONDITION cond, void* subject)
[3330]574{
[3522]575  TrackElement* tmpElem = this->findTrackElementByID(groupID);
[3594]576  if (!tmpElem->isFork)
[3522]577    {
[3594]578      PRINTF(2)("%d is not a Fork, and no condition can be set in this case\n", tmpElem->ID);
579      return;
[3522]580    }
[3594]581  else
582    {
583      switch (cond)
584        {
585        case LOWEST:
586          tmpElem->condFunc = &TrackElement::lowest;
587          break;
588        case HIGHEST:
589          tmpElem->condFunc = &TrackElement::highest;
590          break;
591        case RANDOM: 
592          tmpElem->condFunc = &TrackElement::random;
593          break;
594        case LEFTRIGHT:
595          tmpElem->condFunc = &TrackElement::leftRight;
596          break;
597        case NEAREST:
598          tmpElem->condFunc = &TrackElement::nearest;
599          break;
600        case ENEMYKILLED:
601          break;
602        }
603      tmpElem->subject=subject;
604    }
[3330]605}
606
[3522]607
[3330]608/**
609   \brief joins some tracks together again.
610   \param count The count of Paths to join.
611
612   Join will set the localTime to the longest time a Path has to get to this Point. \n
[3354]613   Join will join all curves to the first curve, meaning that all the tangents will be matched.
[3330]614*/
[3332]615void TrackManager::join(unsigned int count, ...)
[3330]616{
[3332]617  int* trackIDs = new int [count];
618  va_list ID;
619  va_start (ID, count);
620  for(int i = 0; i < count; i++)
621    {
622      trackIDs[i] = va_arg (ID, int);
623    }
624  va_end(ID);
625  this->joinV(count, trackIDs);
626  delete []trackIDs;
[3330]627}
628
629/**
630   \brief joins some tracks together again.
631   \param count The count of Paths to join.
632   \param trackIDs an Array with the trackID's to join
633
634   \see void TrackManager::join(int count, ...)
635*/
[3332]636void TrackManager::joinV(unsigned int count, int* trackIDs)
[3330]637{
[3594]638  PRINTF(3)("Joining %d tracks and merging to Track %d\n", count, trackIDs[0]);
[3354]639
[3522]640  // checking if there is a back-loop-connection and ERROR if it is.
641  TrackElement* tmpTrackElem = this->findTrackElementByID(trackIDs[0]);
642  if (!tmpTrackElem->backLoopCheck(tmpTrackElem))
[3594]643    PRINTF(2)("Backloop connection detected at joining trackElements\n");
[3522]644
[3354]645  // chanching work-on to temporary value. going back at the end.
646  int tmpCurrentWorkingID = this->currentTrackElem->ID;
647  this->workOn(trackIDs[0]);
[3433]648  TrackElement* firstJoint = this->currentTrackElem;
649  float tmpLatestTime = firstJoint->endTime;
[3354]650
651  Vector tmpEndPoint = firstJoint->curve->getNode(firstJoint->curve->getNodeCount());
[3433]652  Vector tmpTangentPoint = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()-1);
653  Vector tmpc2Point = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()-2);
[3354]654  firstJoint->isJoined = true;
[3433]655  //  firstJoint->mainJoin = true;
[3354]656  if(!firstJoint->isHotPoint)
657    this->setSavePoint();
[3433]658  // Timing:
659  for (int i = 0; i < count; i++)
660    {
661      TrackElement* tmpJoinElem = this->findTrackElementByID(trackIDs[i]);
662      if (tmpJoinElem->childCount == 0
663          && tmpJoinElem->endTime > tmpLatestTime)
664        tmpLatestTime = tmpJoinElem->endTime;
665    }
666  // time the main Join.
667  firstJoint->jumpTime = tmpLatestTime - firstJoint->endTime;
668 
669  // Joining:
[3354]670  for (int i = 1; i < count; i++)
[3352]671    {
672      TrackElement* tmpJoinElem = this->findTrackElementByID(trackIDs[i]);
[3354]673      if (tmpJoinElem->childCount > 0)
674        printf("!!This Curve has children, and as such will not be joined!!\n You can try joining other childless TrackElements to this one!");
675      else
676        {
[3433]677          this->addPoint(tmpc2Point, tmpJoinElem);
[3354]678          this->addPoint(tmpTangentPoint, tmpJoinElem);
679          this->addPoint(tmpEndPoint, tmpJoinElem);
[3433]680          // time all other Joins
681          tmpJoinElem->jumpTime = tmpLatestTime - tmpJoinElem->endTime;
[3354]682         
683          //Copying Joint-Info
684          tmpJoinElem->children = firstJoint->children;
685          tmpJoinElem->childCount = firstJoint->childCount;
686          tmpJoinElem->isSavePoint = firstJoint->isSavePoint;
687          tmpJoinElem->isFork = firstJoint->isFork;
[3433]688
[3354]689          tmpJoinElem->isJoined = true;
690        }
[3352]691    }
[3433]692  if(firstJoint->childCount > 0)
[3594]693    {
694      TrackElement* enumElem = firstJoint->children->enumerate();
695      while (enumElem)
696        {
697          PRINTF(4)("Setting startingTime of %d to %f.\n", enumElem->ID, tmpLatestTime);
698          enumElem->startingTime = tmpLatestTime;
699          enumElem->endTime = tmpLatestTime + enumElem->duration;
700         
701          enumElem = firstJoint->children->nextElement();
702        }
703    }
[3354]704  // returning to the TrackElement we were working on.
705  this->workOn(tmpCurrentWorkingID);
[3330]706}
707
[3433]708/**
709   \brief finalizes the TrackSystem. after this it will not be editable anymore
710
711   \todo check for any inconsistencies, output errors
712*/
713void TrackManager::finalize(void)
714{
715  for (int i = 1; i<= trackElemCount ;i++)
716    {
717      TrackElement* tmpElem = findTrackElementByID(i);
[3594]718      if (tmpElem->childCount > 0 && tmpElem->mainJoin)
[3433]719        {
[3594]720
721          TrackElement* enumElem = tmpElem->children->enumerate();
722          while (enumElem)
[3433]723            {
724             
725              // c1-continuity
[3594]726              enumElem->curve->addNode(enumElem->curve->getNode(0) +
727                                                   ((enumElem->curve->getNode(0) - 
[3433]728                                                    tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1))
729                                                    ),2);
[3594]730              enumElem->nodeCount++;
[3433]731              // c2-continuity
[3594]732              enumElem->curve->addNode((tmpElem->curve->getNode(tmpElem->curve->getNodeCount())-
[3433]733                                                    tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) * 4 +
734                                                   tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-2), 3);
[3594]735              enumElem->nodeCount++;                                               
736              PRINTF(5)("accelerations: %d-in: count: %d, %f, %f, %f\n                  %d-out: count: %d %f, %f, %f\n",
[3433]737                     tmpElem->ID, tmpElem->nodeCount,
738                     tmpElem->curve->calcAcc(0.999).x, tmpElem->curve->calcAcc(0.999).y, tmpElem->curve->calcAcc(0.999).z,
[3594]739                     enumElem->ID, enumElem->nodeCount,
740                     enumElem->curve->calcAcc(0).x, enumElem->curve->calcAcc(0).y, enumElem->curve->calcAcc(0).z);
741             
742              enumElem = tmpElem->children->nextElement();
[3433]743            }
744        }
745    }
[3601]746  for (int i = 1; i <=trackElemCount;i++)
747    if (this->findTrackElementByID(i)->endTime > this->maxTime)
748      this->maxTime = findTrackElementByID(i)->endTime; // very bad implemented :/
[3433]749}
750
751
[3330]752// RUNTIME //
753
754/**
755   \brief calculates the Position for the localTime of the Track.
756   \returns the calculated Position
757*/
[3332]758Vector TrackManager::calcPos() const
[3330]759{
[3348]760  return this->currentTrackElem->curve->calcPos((this->localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration);
[3330]761}
762
763/**
764   \brief calculates the Rotation for the localTime of the Track.
765   \returns the calculated Rotation
766*/
[3332]767Vector TrackManager::calcDir() const
[3330]768{
[3433]769  return this->currentTrackElem->curve->calcDir((this->localTime - this->currentTrackElem->startingTime)/this->currentTrackElem->duration);
[3330]770}
771
772/**
[3596]773   \returns the current Width of the track
774*/
775float TrackManager::getWidth(void) const
776{
777  return this->currentTrackElem->width;
778}
779
780/**
[3330]781   \brief Advances the local-time of the Track around dt
782   \param dt The time about which to advance.
[3333]783
784   This function also checks, if the TrackElement has to be changed.
[3330]785*/
786void TrackManager::tick(float dt)
787{
[3433]788  dt /= 1000;
[3591]789  PRINTF(4)("CurrentTrackID: %d, LocalTime is: %f, timestep is: %f\n", this->currentTrackElem->ID, this->localTime, dt);
[3348]790  if (this->localTime <= this->firstTrackElem->duration)
791    this->jumpTo(this->localTime);
[3601]792  if (this->localTime <= this->maxTime)
793    this->localTime += dt;
[3433]794  if (this->localTime > this->currentTrackElem->endTime
795      && this->currentTrackElem->children)
796    {
[3527]797      if (this->currentTrackElem->jumpTime != 0.0)
[3433]798        this->jumpTo(this->localTime + this->currentTrackElem->jumpTime);
[3527]799      // jump to the next TrackElement and also set the history of the new Element to the old one.
800      TrackElement* tmpHistoryElem = this->currentTrackElem;
[3594]801      this->currentTrackElem = this->currentTrackElem->getChild(this->choosePath(this->currentTrackElem));
[3527]802      this->currentTrackElem->history = tmpHistoryElem;
[3433]803    }
804  if (this->bindSlave)
805    {
806      Vector tmp = this->calcPos();
807      Quaternion quat = Quaternion(this->calcDir(), Vector(this->currentTrackElem->curve->calcAcc((localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration).x,1,this->currentTrackElem->curve->calcAcc((localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration).z)); 
[3539]808
809      Vector v(0.0, 1.0, 0.0);
[3543]810      Quaternion q(-PI/2, v);
[3539]811      quat = quat * q;
812
[3433]813      this->bindSlave->setAbsCoor(&tmp);
814      this->bindSlave->setAbsDir(&quat);
815    }
[3330]816}
817
818/**
[3331]819   \brief Jumps to a certain point on the Track.
820   \param time The time on the Track to jump to.
821
822   This should be used to Jump backwards on a Track, because moving forward means to change between the Path. (it then tries to choose the default.)
823   Max is trackLengthMax.
824*/
825void TrackManager::jumpTo(float time)
826{
[3348]827  if (time == 0)
828    this->currentTrackElem = this->firstTrackElem;
829  this->localTime = time;
[3331]830}
831
832/**
[3330]833   \brief a Function that decides which Path we should follow.
[3522]834   \param trackElem The Path to choose.
[3330]835   
836*/
[3522]837int TrackManager::choosePath(TrackElement* trackElem)
[3330]838{
[3522]839  return (trackElem->*(trackElem->condFunc))(trackElem->subject);
[3330]840}
841
[3433]842/**
843   \brief Sets the PNode, that should be moved along the Tack
844   \param bindSlave the PNode to set
845*/
846void TrackManager::setBindSlave(PNode* bindSlave)
847{
[3556]848  this->bindSlave = bindSlave;
[3433]849}
[3350]850
[3556]851/**
852   \returns the main TrackNode
853*/
854PNode* TrackManager::getTrackNode(void)
855{
856  return this->trackNode;
857}
[3350]858
859// DEBUG //
860
861/**
862   \brief Imports a model of the Graph into the OpenGL-environment.
863   \param dt The Iterator used in seconds for Painting the Graph.
864
865   This is for testing facility only. Do this if you want to see the Path inside the Level.
866   eventually this will all be packed into a gl-list.
867*/
868void TrackManager::drawGraph(float dt) const
869{
870  for (int i = 1; i <= trackElemCount; i++)
871    {
[3352]872      glBegin(GL_LINE_STRIP);
[3350]873      TrackElement* tmpElem = this->findTrackElementByID(i);
874      if (tmpElem->curve)
875        for(float f = 0.0; f < 1.0; f+=dt)
876          {
877            //      printf("%f, %f, %f\n",trackManager->calcPos().x, trackManager->calcPos().y, trackManager->calcPos().z);
878            Vector tmpVector = tmpElem->curve->calcPos(f);
879            glVertex3f(tmpVector.x, tmpVector.y, tmpVector.z);
880          }
[3710]881      glEnd();
[3350]882    }
883}
884
[3433]885/**
886   \brief outputs debug information about the trackManager
887   \param level how much debug
888*/
[3350]889void TrackManager::debug(unsigned int level) const
890{
[3522]891  PRINT(0)("=========================================\n");
892  PRINT(0)("= CLASS TRACKMANAGER::debug information =\n");
893  PRINT(0)("=========================================\n");
894  //  PRINT(0)("Status is: %
895  PRINT(0)(" Consists of %d elements\n", this->trackElemCount);
896  PRINT(0)(" localTime is: %f\n", this->localTime);
[3350]897  if (level >= 2)
898    {
899      for (int i = 1; i <= trackElemCount; i++)
900        {
901          TrackElement* tmpElem = this->findTrackElementByID(i);
[3593]902          tmpElem->debug();
[3350]903        }
904    }
[3522]905  PRINT(0)("-----------------------------------------\n");
[3350]906}
Note: See TracBrowser for help on using the repository browser.