Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/playability/src/util/track/track.cc @ 10297

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

track now works a bit can be partly used now

File size: 7.8 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
28#include "p_node.h"
29
30#include "stdincl.h"
31
32#include "debug.h"
33
34ObjectListDefinition(Track);
35// CREATE_FACTORY(Track);
36
37
38/**
39 *  standard constructor
40*/
41Track::Track()
42{
43  this->init();
44}
45
46
47/**
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();
54
55  if (root != NULL)
56    this->loadParams(root);
57}
58
59
60/**
61 * initializes this class
62 */
63void Track::init()
64{
65  this->curveType = CURVE_BEZIER;
66//  this->startingTime = 0;
67  this->duration = 20;
68  this->endTime = 20;
69  this->width = 10;
70  this->curve = new BezierCurve();
71  this->trackNode = new PNode(PNode::getNullParent(), PNODE_ALL);
72  this->nodeCount = 0;
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
93     }
94     LOAD_PARAM_END_CYCLE(element);
95}
96
97
98
99/**
100 * This function adds a point with its coordinates to the track
101 * @param x
102 * @param y
103 * @param z
104 */
105void Track::addPoint(float x, float y, float z)
106{
107     this->addPointV(Vector (x,y,z));
108}
109
110
111/**
112 * This function adds a point to the track as a vector
113 * @param newPoint
114 */
115void Track::addPointV(Vector newPoint)
116{
117   this->curve->addNode(newPoint);
118   if( this->nodeCount == 0) this->trackNode->setAbsCoor(newPoint);
119   this->nodeCount++;
120   // PRINTF(0)("Point added to curve\n");
121}
122
123/**
124 * This function sets the speed of the trackNode by altering the duration
125 * of the time the trackNode travels on the whole track. This is bad because
126 * the speed depends on the length of the curve. (by getting the curve's length
127 * this function will make a lot more sense)
128 */
129void Track::setSpeed(float speed)
130{
131     this->duration = this->duration/speed;
132     
133}
134
135/**
136 * We probably doesn't even need this
137 */
138//void Track::finalize()
139//{
140//   for (int i = 1; i<= trackElemCount ;i++)
141//     {
142//       TrackElement* tmpElem = this->firstTrackElem->findByID(i);
143//       if( tmpElem->childCount > 0)
144//         {
145//           tIterator<TrackElement>* iterator = tmpElem->children->getIterator();
146//           TrackElement* enumElem = iterator->firstElement();
147//           //TrackElement* enumElem = tmpElem->children->enumerate();
148//           while (enumElem)
149//             {
150//
151//               // c1-continuity
152//               enumElem->curve->addNode(enumElem->curve->getNode(0) +
153//                                                    ((enumElem->curve->getNode(0) -
154//                                                     tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1))
155//                                                     ),2);
156//               enumElem->nodeCount++;
157//               // c2-continuity
158//               enumElem->curve->addNode((tmpElem->curve->getNode(tmpElem->curve->getNodeCount())-
159//                                                     tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) * 4 +
160//                                                    tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-2), 3);
161//               enumElem->nodeCount++;
162//               PRINTF(5)("accelerations: %d-in: count: %d, %f, %f, %f\n                  %d-out: count: %d %f, %f, %f\n",
163//                      tmpElem->ID, tmpElem->nodeCount,
164//                      tmpElem->curve->calcAcc(0.999).x, tmpElem->curve->calcAcc(0.999).y, tmpElem->curve->calcAcc(0.999).z,
165//                      enumElem->ID, enumElem->nodeCount,
166//                      enumElem->curve->calcAcc(0).x, enumElem->curve->calcAcc(0).y, enumElem->curve->calcAcc(0).z);
167//
168//               enumElem = iterator->nextElement();
169//             }
170//           delete iterator;
171//         }
172//     }
173
174
175
176  /*for (int i = 1; i <= trackElemCount;i++)
177    if (this->firstTrackElem->findByID(i)->endTime > this->maxTime)
178      this->maxTime = this->firstTrackElem->findByID(i)->endTime; // very bad implemented :/
179      */
180//}
181
182Vector Track::calcPos() const
183{
184  return this->curve->calcPos(this->localTime/this->duration);
185}
186
187Vector Track::calcDir() const
188{
189  return this->curve->calcDir(this->localTime/this->duration);
190}
191
192void Track::tick(float dt)
193{
194//   PRINTF(4)("CurrentTrackID: %d, LocalTime is: %f, timestep is: %f\n", this->currentTrackElem->ID, this->localTime, dt);
195//   if (this->localTime <= this->firstTrackElem->duration)
196//     this->jumpTo(this->localTime);
197//   if (this->localTime <= this->maxTime)
198     this->localTime += dt;
199//   if (this->localTime > this->currentTrackElem->endTime
200//       && this->currentTrackElem->children)
201//     {
202//       if (this->currentTrackElem->jumpTime != 0.0)
203//         this->jumpTo(this->localTime + this->currentTrackElem->jumpTime);
204//       // jump to the next TrackElement and also set the history of the new Element to the old one.
205//       TrackElement* tmpHistoryElem = this->currentTrackElem;
206//       this->currentTrackElem = this->currentTrackElem->getChild(this->choosePath(this->currentTrackElem));
207//       this->currentTrackElem->history = tmpHistoryElem;
208//       if (this->currentTrackElem->getName())
209//         {
210//           this->trackText->setText(this->currentTrackElem->getName());
211//           this->textAnimation->replay();
212//         }
213//     }
214   if (this->trackNode)
215     {
216       Vector tmp = this->calcPos();
217       //Quaternion quat = Quaternion(this->calcDir(), Vector(this->curve->calcAcc(this->localTime/this->duration).x,1,this->curve->calcAcc(this->localTime/this->duration).z));
218       Quaternion quat = Quaternion(this->calcDir(), 0);
219
220       Vector v(0.0, 1.0, 0.0);
221       Quaternion q(-PI/2, v);
222       quat = quat * q;
223
224       // move trackNode of the track
225       this->trackNode->shiftCoor(tmp - this->trackNode->getAbsCoor());
226       // set direction and roll angle of trackNode
227       this->trackNode->setAbsDir(quat);
228     }
229}
230
231/**
232 * @returns the main TrackNode
233*/
234PNode* Track::getTrackNode()
235{
236  return this->trackNode;
237}
238
239/**
240 *  Imports a model of the Graph into the OpenGL-environment.
241 * @param dt The Iterator used in seconds for Painting the Graph.
242
243   This is for testing facility only. Do this if you want to see the Path inside the Level.
244   eventually this will all be packed into a gl-list.
245*/
246/*void Track::drawGraph(float dt) const
247{
248      glBegin(GL_LINE_STRIP);
249        for(float f = 0.0; f < 1.0; f+=dt)
250          {
251            // PRINTF(4)("drawing",this->calcPos().x, this->calcPos().y, this->calcPos().z);
252            Vector tmpVector = this->curve->calcPos(f);
253            glVertex3f(tmpVector.x, tmpVector.y, tmpVector.z);
254          }
255      glEnd();
256}*/
Note: See TracBrowser for help on using the repository browser.