Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged cleanup into trunk (only improvements)

File size: 9.6 KB
Line 
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.
15
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.
19
20   As we do this we have exactly the verticalscroller feeling we want.
21
22   main-programmer: Benjamin Knecht
23*/
24
25#include "util/loading/load_param.h"
26#include "track/track.h"
27#include "glincl.h"
28#include "p_node.h"
29
30#include "debug.h"
31
32ObjectListDefinition(Track);
33// CREATE_FACTORY(Track);
34
35
36/**
37 *  standard constructor
38*/
39Track::Track()
40{
41  this->init();
42}
43
44
45/**
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();
52
53  if (root != NULL)
54    this->loadParams(root);
55}
56
57
58/**
59 * initializes this class
60 */
61void Track::init()
62{
63  this->curveType = CURVE_BEZIER;
64//  this->startingTime = 0;
65  this->duration = 20;
66  this->endTime = 20;
67  this->width = 10;
68  this->curve = new BezierCurve();
69  this->trackNode = new PNode(PNode::getNullParent(), PNODE_ALL);
70  this->nodeCount = 0;
71  this->localTime = 0;
72  this->pause = false;
73}
74
75/**
76 *  standard destructor
77*/
78Track::~Track()
79{
80
81}
82
83
84void Track::loadParams(const TiXmlElement* root)
85{
86     LOAD_PARAM_START_CYCLE(root, element);
87     {
88           LoadParam_CYCLE(element, "addPoint", this, Track, addPoint)
89             .describe("Adds a new Point to the currently selected TrackElement");
90           LoadParam_CYCLE(element, "speed", this, Track, setSpeed)
91             .describe("Sets speed of traveling");
92           LoadParam_CYCLE(element, "mode", this, Track, setMode)
93             .describe("Sets mode of track behavior");
94     }
95     LOAD_PARAM_END_CYCLE(element);
96}
97
98
99
100/**
101 * This function adds a point with its coordinates to the track
102 * @param x
103 * @param y
104 * @param z
105 */
106void Track::addPoint(float x, float y, float z)
107{
108     this->addPointV(Vector (x,y,z));
109}
110
111
112/**
113 * This function adds a point to the track as a vector
114 * @param newPoint
115 */
116void Track::addPointV(Vector newPoint)
117{
118   this->curve->addNode(newPoint);
119   if( this->nodeCount == 0) this->trackNode->setAbsCoor(newPoint);
120   this->nodeCount++;
121   // PRINTF(0)("Point added to curve\n");
122}
123
124/**
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;
133     this->speed = speed;
134}
135
136/**
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/**
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/**
154 * We probably doesn't even need this
155 */
156//void Track::finalize()
157//{
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//     }
191
192
193
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      */
198//}
199
200Vector Track::calcPos() const
201{
202  return this->curve->calcPos(this->localTime/this->duration);
203}
204
205Vector Track::calcDir() const
206{
207  return this->curve->calcDir(this->localTime/this->duration);
208}
209
210void Track::tick(float dt)
211{
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)
216
217
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//     }
233   if (this->trackNode && !this->pause)
234   {
235       // tmp save
236        float oldTime = this->localTime;
237
238        if(this->mode == 0)
239        {
240              // PRINTF(0)("localTime %f and duration %f", this->localTime, this->duration);
241              if(this->localTime + dt < this->duration)
242                this->localTime += dt;
243        }
244        else
245        {
246            this->localTime += dt;
247            if(this->localTime >= this->duration)
248                this->localTime = 0;
249        }
250
251       if(oldTime != this->localTime)
252       {
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);
276       }
277    }
278}
279
280/**
281 * @returns the main TrackNode
282*/
283PNode* Track::getTrackNode()
284{
285  return this->trackNode;
286}
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*/
295void Track::drawGraph(float dt) const
296{
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
316
317      glBegin(GL_LINE_STRIP);
318        glColor3f(1.0, 1.0, 0.6);
319
320        Vector tmpVector;
321        for(float f = 0.0; f < 1.0; f+=dt)
322          {
323            //PRINTF(0)("drawing",this->calcPos().x, this->calcPos().y, this->calcPos().z);
324            tmpVector = this->curve->calcPos(f);
325            glVertex3f(tmpVector.x, tmpVector.y, tmpVector.z);
326          }
327      glEnd();
328
329      glPopAttrib();
330
331    glPopMatrix();
332}
Note: See TracBrowser for help on using the repository browser.