Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/vs-enhencements/src/util/track/track.cc @ 10640

Last change on this file since 10640 was 10640, checked in by nicolasc, 17 years ago

visual version of the action box, currently in the track.{cc,h}

File size: 11.3 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 = 24;
68  this->height = 18;
69  this->depth = 200;
70  this->stretch = 4;
71  this->curve = new BezierCurve();
72  this->trackNode = new PNode(PNode::getNullParent(), PNODE_ALL);
73  this->nodeCount = 0;
74  this->localTime = 0;
75  this->pause = false;
76}
77
78/**
79 *  standard destructor
80*/
81Track::~Track()
82{
83
84}
85
86
87void Track::loadParams(const TiXmlElement* root)
88{
89     LOAD_PARAM_START_CYCLE(root, element);
90     {
91           LoadParam_CYCLE(element, "addPoint", this, Track, addPoint)
92             .describe("Adds a new Point to the currently selected TrackElement");
93           LoadParam_CYCLE(element, "speed", this, Track, setSpeed)
94             .describe("Sets speed of traveling");
95           LoadParam_CYCLE(element, "mode", this, Track, setMode)
96             .describe("Sets mode of track behavior");
97     }
98     LOAD_PARAM_END_CYCLE(element);
99}
100
101
102
103/**
104 * This function adds a point with its coordinates to the track
105 * @param x
106 * @param y
107 * @param z
108 */
109void Track::addPoint(float x, float y, float z)
110{
111     this->addPointV(Vector (x,y,z));
112}
113
114
115/**
116 * This function adds a point to the track as a vector
117 * @param newPoint
118 */
119void Track::addPointV(Vector newPoint)
120{
121   this->curve->addNode(newPoint);
122   if( this->nodeCount == 0) this->trackNode->setAbsCoor(newPoint);
123   this->nodeCount++;
124   // PRINTF(0)("Point added to curve\n");
125}
126
127/**
128 * This function sets the speed of the trackNode by altering the duration
129 * of the time the trackNode travels on the whole track. This is bad because
130 * the speed depends on the length of the curve. (by getting the curve's length
131 * this function will make a lot more sense)
132 */
133void Track::setSpeed(float speed)
134{
135     this->duration = this->duration/speed;
136     this->speed = speed;
137}
138
139/**
140 * Sets the mode of the track. 0 means wait at the end. 1 means rerun track
141 */
142void Track::setMode(int newMode)
143{
144     this->mode = newMode;
145}
146
147/**
148 * Sets the bool if the track runs (false) or not (true)
149 */
150void Track::pauseTrack(bool stop)
151{
152     this->pause = stop;
153}
154
155
156/**
157 * We probably doesn't even need this
158 */
159//void Track::finalize()
160//{
161//   for (int i = 1; i<= trackElemCount ;i++)
162//     {
163//       TrackElement* tmpElem = this->firstTrackElem->findByID(i);
164//       if( tmpElem->childCount > 0)
165//         {
166//           tIterator<TrackElement>* iterator = tmpElem->children->getIterator();
167//           TrackElement* enumElem = iterator->firstElement();
168//           //TrackElement* enumElem = tmpElem->children->enumerate();
169//           while (enumElem)
170//             {
171//
172//               // c1-continuity
173//               enumElem->curve->addNode(enumElem->curve->getNode(0) +
174//                                                    ((enumElem->curve->getNode(0) -
175//                                                     tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1))
176//                                                     ),2);
177//               enumElem->nodeCount++;
178//               // c2-continuity
179//               enumElem->curve->addNode((tmpElem->curve->getNode(tmpElem->curve->getNodeCount())-
180//                                                     tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) * 4 +
181//                                                    tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-2), 3);
182//               enumElem->nodeCount++;
183//               PRINTF(5)("accelerations: %d-in: count: %d, %f, %f, %f\n                  %d-out: count: %d %f, %f, %f\n",
184//                      tmpElem->ID, tmpElem->nodeCount,
185//                      tmpElem->curve->calcAcc(0.999).x, tmpElem->curve->calcAcc(0.999).y, tmpElem->curve->calcAcc(0.999).z,
186//                      enumElem->ID, enumElem->nodeCount,
187//                      enumElem->curve->calcAcc(0).x, enumElem->curve->calcAcc(0).y, enumElem->curve->calcAcc(0).z);
188//
189//               enumElem = iterator->nextElement();
190//             }
191//           delete iterator;
192//         }
193//     }
194
195
196
197  /*for (int i = 1; i <= trackElemCount;i++)
198    if (this->firstTrackElem->findByID(i)->endTime > this->maxTime)
199      this->maxTime = this->firstTrackElem->findByID(i)->endTime; // very bad implemented :/
200      */
201//}
202
203Vector Track::calcPos() const
204{
205  return this->curve->calcPos(this->localTime/this->duration);
206}
207
208Vector Track::calcDir() const
209{
210  return this->curve->calcDir(this->localTime/this->duration);
211}
212
213void Track::tick(float dt)
214{
215//   PRINTF(4)("CurrentTrackID: %d, LocalTime is: %f, timestep is: %f\n", this->currentTrackElem->ID, this->localTime, dt);
216//   if (this->localTime <= this->firstTrackElem->duration)
217//     this->jumpTo(this->localTime);
218//   if (this->localTime <= this->maxTime)
219
220
221//   if (this->localTime > this->currentTrackElem->endTime
222//       && this->currentTrackElem->children)
223//     {
224//       if (this->currentTrackElem->jumpTime != 0.0)
225//         this->jumpTo(this->localTime + this->currentTrackElem->jumpTime);
226//       // jump to the next TrackElement and also set the history of the new Element to the old one.
227//       TrackElement* tmpHistoryElem = this->currentTrackElem;
228//       this->currentTrackElem = this->currentTrackElem->getChild(this->choosePath(this->currentTrackElem));
229//       this->currentTrackElem->history = tmpHistoryElem;
230//       if (this->currentTrackElem->getName())
231//         {
232//           this->trackText->setText(this->currentTrackElem->getName());
233//           this->textAnimation->replay();
234//         }
235//     }
236   if (this->trackNode && !this->pause)
237   {
238       // tmp save
239        float oldTime = this->localTime;
240
241        if(this->mode == 0)
242        {
243              // PRINTF(0)("localTime %f and duration %f", this->localTime, this->duration);
244              if(this->localTime + dt < this->duration)
245                this->localTime += dt;
246        }
247        else
248        {
249            this->localTime += dt;
250            if(this->localTime >= this->duration)
251                this->localTime = 0;
252        }
253
254       if(oldTime != this->localTime)
255       {
256           Vector tmp = this->calcPos();
257   
258   
259           Vector dV = tmp - this->trackNode->getAbsCoor();
260           float dx = speed * dt;
261           float ratio = dx / dV.len();
262   
263           if( dt > 0.0f)
264           {
265              float newDt = dt * ratio;
266              this->localTime = oldTime + newDt;
267           }
268           tmp = this->calcPos();
269   
270           Vector v(0.0, 1.0, 0.0);
271           Quaternion quat = Quaternion(this->calcDir(), v);
272           Quaternion q(-PI/2, v);
273           quat = quat * q;
274   
275           // move trackNode of the track
276           this->trackNode->shiftCoor(tmp - this->trackNode->getAbsCoor());
277           // set direction and roll angle of trackNode
278           this->trackNode->setAbsDir(quat);
279       }
280    }
281}
282
283/**
284 * @returns the main TrackNode
285*/
286PNode* Track::getTrackNode()
287{
288  return this->trackNode;
289}
290
291/**
292 *  Imports a model of the Graph into the OpenGL-environment.
293 * @param dt The Iterator used in seconds for Painting the Graph.
294
295   This is for testing facility only. Do this if you want to see the Path inside the Level.
296   eventually this will all be packed into a gl-list.
297*/
298void Track::drawGraph(float dt) const
299{
300    glMatrixMode(GL_MODELVIEW);
301    glPushMatrix();
302
303    glPushAttrib(GL_ENABLE_BIT);
304
305    glDisable(GL_LIGHTING);
306    glDisable(GL_TEXTURE_2D);
307    glDisable(GL_BLEND);
308    glLineWidth(2.0);
309
310
311
312    PNode* node = PNode::getNullParent();
313    glTranslatef (node->getAbsCoor ().x,
314                  node->getAbsCoor ().y,
315                  node->getAbsCoor ().z);
316    Vector tmpRot = node->getAbsDir().getSpacialAxis();
317    glRotatef (node->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
318
319
320      glBegin(GL_LINE_STRIP);
321        glColor3f(1.0, 1.0, 0.6);
322
323        Vector tmpVector;
324        for(float f = 0.0; f < 1.0; f+=dt)
325          {
326            //PRINTF(0)("drawing",this->calcPos().x, this->calcPos().y, this->calcPos().z);
327            tmpVector = this->curve->calcPos(f);
328            glVertex3f(tmpVector.x, tmpVector.y, tmpVector.z);
329          }
330      glEnd();
331
332      glPopAttrib();
333
334    glPopMatrix();
335}
336
337void Track::drawBox() const
338{
339    glMatrixMode(GL_MODELVIEW);
340    glPushMatrix();
341
342    glPushAttrib(GL_ENABLE_BIT);
343
344    glDisable(GL_LIGHTING);
345    glDisable(GL_TEXTURE_2D);
346    glDisable(GL_BLEND);
347    glLineWidth(2.0);
348
349    glTranslatef (trackNode->getAbsCoor ().x,
350                  trackNode->getAbsCoor ().y,
351                  trackNode->getAbsCoor ().z);
352    Vector tmpRot = trackNode->getAbsDir().getSpacialAxis();
353    glRotatef (trackNode->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
354
355
356    glColor3f(1.0, 1.0, 0.0);
357    glBegin(GL_LINE_STRIP);
358      glVertex3f(0, height, width);
359      glVertex3f(0, -height, width);
360      glVertex3f(0, -height, -width);
361      glVertex3f(0, height, -width);
362      glVertex3f(0, height, width);
363    glEnd();
364
365    glBegin(GL_LINE_STRIP);
366      glVertex3f(depth, height * stretch, width * stretch);
367      glVertex3f(depth, -height * stretch, width * stretch);
368      glVertex3f(depth, -height * stretch, -width * stretch);
369      glVertex3f(depth, height * stretch, -width * stretch);
370      glVertex3f(depth, height * stretch, width * stretch);
371    glEnd();
372
373    glBegin(GL_LINE_STRIP);
374      glVertex3f(depth, height * stretch, width * stretch);
375      glVertex3f(0, height, width);
376      glVertex3f(0, -height, width);
377      glVertex3f(depth, -height * stretch, width * stretch);
378    glEnd();
379
380    glBegin(GL_LINE_STRIP);
381      glVertex3f(depth, height * stretch, -width * stretch);
382      glVertex3f(0, height, -width);
383      glVertex3f(0, -height, -width);
384      glVertex3f(depth, -height * stretch, -width * stretch);
385    glEnd();
386
387    glPopMatrix();
388}
Note: See TracBrowser for help on using the repository browser.