Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

some changes so the vertical scroller will work better

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