Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/util/track/track.cc @ 10621

Last change on this file since 10621 was 10618, checked in by bknecht, 18 years ago

merged cleanup into trunk (only improvements)

File size: 9.6 KB
RevLine 
[10084]1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2006 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:
12
13   This is anohter installment of the infamous track system. It serves for
14   temporary use only and is mainly a slimmed version of the old track.
[10088]15
[10084]16   The track is used to steer the spaceship. In this case the track will have
17   to control a PNode. The spaceship will be able to fly around this node.
18   The camera will always be focused on that point.
[10088]19
[10084]20   As we do this we have exactly the verticalscroller feeling we want.
[10088]21
[10084]22   main-programmer: Benjamin Knecht
23*/
24
[10085]25#include "util/loading/load_param.h"
[10088]26#include "track/track.h"
[10618]27#include "glincl.h"
[10088]28#include "p_node.h"
[10085]29
[10088]30#include "debug.h"
31
[10085]32ObjectListDefinition(Track);
[10088]33// CREATE_FACTORY(Track);
[10085]34
[10088]35
[10084]36/**
37 *  standard constructor
38*/
39Track::Track()
40{
[10088]41  this->init();
[10084]42}
[10085]43
[10088]44
[10085]45/**
[10088]46 * this is a constructor for use with the xml loading file
47 * @param root xml root element for this object
48 */
49Track::Track(const TiXmlElement* root)
50{
51  this->init();
[10097]52
53  if (root != NULL)
54    this->loadParams(root);
[10088]55}
56
57
58/**
59 * initializes this class
60 */
61void Track::init()
62{
63  this->curveType = CURVE_BEZIER;
[10096]64//  this->startingTime = 0;
[10297]65  this->duration = 20;
66  this->endTime = 20;
[10091]67  this->width = 10;
68  this->curve = new BezierCurve();
[10088]69  this->trackNode = new PNode(PNode::getNullParent(), PNODE_ALL);
[10284]70  this->nodeCount = 0;
[10335]71  this->localTime = 0;
[10500]72  this->pause = false;
[10088]73}
74
75/**
[10085]76 *  standard destructor
77*/
78Track::~Track()
79{
[10091]80
[10085]81}
82
[10088]83
[10085]84void Track::loadParams(const TiXmlElement* root)
85{
86     LOAD_PARAM_START_CYCLE(root, element);
87     {
[10284]88           LoadParam_CYCLE(element, "addPoint", this, Track, addPoint)
[10085]89             .describe("Adds a new Point to the currently selected TrackElement");
[10297]90           LoadParam_CYCLE(element, "speed", this, Track, setSpeed)
91             .describe("Sets speed of traveling");
[10385]92           LoadParam_CYCLE(element, "mode", this, Track, setMode)
93             .describe("Sets mode of track behavior");
[10085]94     }
95     LOAD_PARAM_END_CYCLE(element);
96}
97
[10088]98
99
100/**
[10096]101 * This function adds a point with its coordinates to the track
[10088]102 * @param x
103 * @param y
104 * @param z
105 */
[10085]106void Track::addPoint(float x, float y, float z)
107{
[10284]108     this->addPointV(Vector (x,y,z));
[10085]109}
110
[10088]111
112/**
[10096]113 * This function adds a point to the track as a vector
[10088]114 * @param newPoint
115 */
[10284]116void Track::addPointV(Vector newPoint)
[10085]117{
[10091]118   this->curve->addNode(newPoint);
[10284]119   if( this->nodeCount == 0) this->trackNode->setAbsCoor(newPoint);
[10091]120   this->nodeCount++;
[10297]121   // PRINTF(0)("Point added to curve\n");
[10085]122}
123
[10088]124/**
[10297]125 * This function sets the speed of the trackNode by altering the duration
126 * of the time the trackNode travels on the whole track. This is bad because
127 * the speed depends on the length of the curve. (by getting the curve's length
128 * this function will make a lot more sense)
129 */
130void Track::setSpeed(float speed)
131{
132     this->duration = this->duration/speed;
[10454]133     this->speed = speed;
[10297]134}
135
136/**
[10385]137 * Sets the mode of the track. 0 means wait at the end. 1 means rerun track
138 */
139void Track::setMode(int newMode)
140{
141     this->mode = newMode;
142}
143
144/**
[10498]145 * Sets the bool if the track runs (false) or not (true)
146 */
147void Track::pauseTrack(bool stop)
148{
149     this->pause = stop;
150}
151
152
153/**
[10096]154 * We probably doesn't even need this
[10088]155 */
[10284]156//void Track::finalize()
157//{
[10088]158//   for (int i = 1; i<= trackElemCount ;i++)
159//     {
160//       TrackElement* tmpElem = this->firstTrackElem->findByID(i);
161//       if( tmpElem->childCount > 0)
162//         {
163//           tIterator<TrackElement>* iterator = tmpElem->children->getIterator();
164//           TrackElement* enumElem = iterator->firstElement();
165//           //TrackElement* enumElem = tmpElem->children->enumerate();
166//           while (enumElem)
167//             {
168//
169//               // c1-continuity
170//               enumElem->curve->addNode(enumElem->curve->getNode(0) +
171//                                                    ((enumElem->curve->getNode(0) -
172//                                                     tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1))
173//                                                     ),2);
174//               enumElem->nodeCount++;
175//               // c2-continuity
176//               enumElem->curve->addNode((tmpElem->curve->getNode(tmpElem->curve->getNodeCount())-
177//                                                     tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) * 4 +
178//                                                    tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-2), 3);
179//               enumElem->nodeCount++;
180//               PRINTF(5)("accelerations: %d-in: count: %d, %f, %f, %f\n                  %d-out: count: %d %f, %f, %f\n",
181//                      tmpElem->ID, tmpElem->nodeCount,
182//                      tmpElem->curve->calcAcc(0.999).x, tmpElem->curve->calcAcc(0.999).y, tmpElem->curve->calcAcc(0.999).z,
183//                      enumElem->ID, enumElem->nodeCount,
184//                      enumElem->curve->calcAcc(0).x, enumElem->curve->calcAcc(0).y, enumElem->curve->calcAcc(0).z);
185//
186//               enumElem = iterator->nextElement();
187//             }
188//           delete iterator;
189//         }
190//     }
[10085]191
192
[10088]193
[10085]194  /*for (int i = 1; i <= trackElemCount;i++)
195    if (this->firstTrackElem->findByID(i)->endTime > this->maxTime)
196      this->maxTime = this->firstTrackElem->findByID(i)->endTime; // very bad implemented :/
197      */
[10284]198//}
[10085]199
[10091]200Vector Track::calcPos() const
[10085]201{
[10096]202  return this->curve->calcPos(this->localTime/this->duration);
[10085]203}
204
[10091]205Vector Track::calcDir() const
[10085]206{
[10096]207  return this->curve->calcDir(this->localTime/this->duration);
[10085]208}
209
210void Track::tick(float dt)
211{
[10088]212//   PRINTF(4)("CurrentTrackID: %d, LocalTime is: %f, timestep is: %f\n", this->currentTrackElem->ID, this->localTime, dt);
213//   if (this->localTime <= this->firstTrackElem->duration)
214//     this->jumpTo(this->localTime);
215//   if (this->localTime <= this->maxTime)
[10410]216
[10454]217
[10088]218//   if (this->localTime > this->currentTrackElem->endTime
219//       && this->currentTrackElem->children)
220//     {
221//       if (this->currentTrackElem->jumpTime != 0.0)
222//         this->jumpTo(this->localTime + this->currentTrackElem->jumpTime);
223//       // jump to the next TrackElement and also set the history of the new Element to the old one.
224//       TrackElement* tmpHistoryElem = this->currentTrackElem;
225//       this->currentTrackElem = this->currentTrackElem->getChild(this->choosePath(this->currentTrackElem));
226//       this->currentTrackElem->history = tmpHistoryElem;
227//       if (this->currentTrackElem->getName())
228//         {
229//           this->trackText->setText(this->currentTrackElem->getName());
230//           this->textAnimation->replay();
231//         }
232//     }
[10512]233   if (this->trackNode && !this->pause)
234   {
[10454]235       // tmp save
236        float oldTime = this->localTime;
237
238        if(this->mode == 0)
239        {
[10512]240              // PRINTF(0)("localTime %f and duration %f", this->localTime, this->duration);
241              if(this->localTime + dt < this->duration)
[10454]242                this->localTime += dt;
243        }
244        else
245        {
246            this->localTime += dt;
247            if(this->localTime >= this->duration)
248                this->localTime = 0;
249        }
250
[10512]251       if(oldTime != this->localTime)
[10454]252       {
[10512]253           Vector tmp = this->calcPos();
254   
255   
256           Vector dV = tmp - this->trackNode->getAbsCoor();
257           float dx = speed * dt;
258           float ratio = dx / dV.len();
259   
260           if( dt > 0.0f)
261           {
262              float newDt = dt * ratio;
263              this->localTime = oldTime + newDt;
264           }
265           tmp = this->calcPos();
266   
267           Vector v(0.0, 1.0, 0.0);
268           Quaternion quat = Quaternion(this->calcDir(), v);
269           Quaternion q(-PI/2, v);
270           quat = quat * q;
271   
272           // move trackNode of the track
273           this->trackNode->shiftCoor(tmp - this->trackNode->getAbsCoor());
274           // set direction and roll angle of trackNode
275           this->trackNode->setAbsDir(quat);
[10454]276       }
[10512]277    }
[10085]278}
279
280/**
281 * @returns the main TrackNode
282*/
[10091]283PNode* Track::getTrackNode()
[10085]284{
285  return this->trackNode;
286}
[10284]287
288/**
289 *  Imports a model of the Graph into the OpenGL-environment.
290 * @param dt The Iterator used in seconds for Painting the Graph.
291
292   This is for testing facility only. Do this if you want to see the Path inside the Level.
293   eventually this will all be packed into a gl-list.
294*/
[10410]295void Track::drawGraph(float dt) const
[10284]296{
[10410]297    glMatrixMode(GL_MODELVIEW);
298    glPushMatrix();
299
300    glPushAttrib(GL_ENABLE_BIT);
301
302    glDisable(GL_LIGHTING);
303    glDisable(GL_TEXTURE_2D);
304    glDisable(GL_BLEND);
305    glLineWidth(2.0);
306
307
308
309    PNode* node = PNode::getNullParent();
310    glTranslatef (node->getAbsCoor ().x,
311                  node->getAbsCoor ().y,
312                  node->getAbsCoor ().z);
313    Vector tmpRot = node->getAbsDir().getSpacialAxis();
314    glRotatef (node->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
315
[10412]316
[10284]317      glBegin(GL_LINE_STRIP);
[10410]318        glColor3f(1.0, 1.0, 0.6);
319
[10412]320        Vector tmpVector;
[10284]321        for(float f = 0.0; f < 1.0; f+=dt)
322          {
[10410]323            //PRINTF(0)("drawing",this->calcPos().x, this->calcPos().y, this->calcPos().z);
[10412]324            tmpVector = this->curve->calcPos(f);
[10284]325            glVertex3f(tmpVector.x, tmpVector.y, tmpVector.z);
326          }
327      glEnd();
[10410]328
329      glPopAttrib();
330
331    glPopMatrix();
332}
Note: See TracBrowser for help on using the repository browser.