Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10730 was 10698, checked in by snellen, 18 years ago

merged adm, hud, vs-enhancements : beni's responsible for this commit. blame him!

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