Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

track updates

File size: 6.5 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
[10088]30#include "debug.h"
31
[10085]32ObjectListDefinition(Track);
[10088]33// CREATE_FACTORY(Track);
[10085]34
[10088]35
[10084]36/**
37 *  standard constructor
38*/
39Track::Track()
40{
[10088]41  this->init();
[10084]42}
[10085]43
[10088]44
[10085]45/**
[10088]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();
[10097]52
53  if (root != NULL)
54    this->loadParams(root);
[10088]55}
56
57
58/**
59 * initializes this class
60 */
61void Track::init()
62{
63  this->curveType = CURVE_BEZIER;
[10096]64//  this->startingTime = 0;
[10091]65  this->duration = 10;
[10096]66  this->endTime = 10;
[10091]67  this->width = 10;
68  this->curve = new BezierCurve();
[10088]69  this->trackNode = new PNode(PNode::getNullParent(), PNODE_ALL);
70}
71
72/**
[10085]73 *  standard destructor
74*/
75Track::~Track()
76{
[10091]77
[10085]78}
79
[10088]80
[10085]81void Track::loadParams(const TiXmlElement* root)
82{
83     LOAD_PARAM_START_CYCLE(root, element);
84     {
85           LoadParam_CYCLE(element, "Point", this, Track, addPoint)
86             .describe("Adds a new Point to the currently selected TrackElement");
[10088]87
[10085]88     }
89     LOAD_PARAM_END_CYCLE(element);
90}
91
[10088]92
93
94/**
[10096]95 * This function adds a point with its coordinates to the track
[10088]96 * @param x
97 * @param y
98 * @param z
99 */
[10085]100void Track::addPoint(float x, float y, float z)
101{
102     this->addPoint(Vector (x,y,z));
103}
104
[10088]105
106/**
[10096]107 * This function adds a point to the track as a vector
[10088]108 * @param newPoint
109 */
[10085]110void Track::addPoint(Vector newPoint)
111{
[10091]112   this->curve->addNode(newPoint);
113   this->nodeCount++;
[10085]114}
115
[10088]116/**
[10096]117 * We probably doesn't even need this
[10088]118 */
[10085]119void Track::finalize()
120{
[10088]121//   for (int i = 1; i<= trackElemCount ;i++)
122//     {
123//       TrackElement* tmpElem = this->firstTrackElem->findByID(i);
124//       if( tmpElem->childCount > 0)
125//         {
126//           tIterator<TrackElement>* iterator = tmpElem->children->getIterator();
127//           TrackElement* enumElem = iterator->firstElement();
128//           //TrackElement* enumElem = tmpElem->children->enumerate();
129//           while (enumElem)
130//             {
131//
132//               // c1-continuity
133//               enumElem->curve->addNode(enumElem->curve->getNode(0) +
134//                                                    ((enumElem->curve->getNode(0) -
135//                                                     tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1))
136//                                                     ),2);
137//               enumElem->nodeCount++;
138//               // c2-continuity
139//               enumElem->curve->addNode((tmpElem->curve->getNode(tmpElem->curve->getNodeCount())-
140//                                                     tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) * 4 +
141//                                                    tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-2), 3);
142//               enumElem->nodeCount++;
143//               PRINTF(5)("accelerations: %d-in: count: %d, %f, %f, %f\n                  %d-out: count: %d %f, %f, %f\n",
144//                      tmpElem->ID, tmpElem->nodeCount,
145//                      tmpElem->curve->calcAcc(0.999).x, tmpElem->curve->calcAcc(0.999).y, tmpElem->curve->calcAcc(0.999).z,
146//                      enumElem->ID, enumElem->nodeCount,
147//                      enumElem->curve->calcAcc(0).x, enumElem->curve->calcAcc(0).y, enumElem->curve->calcAcc(0).z);
148//
149//               enumElem = iterator->nextElement();
150//             }
151//           delete iterator;
152//         }
153//     }
[10085]154
155
[10088]156
[10085]157  /*for (int i = 1; i <= trackElemCount;i++)
158    if (this->firstTrackElem->findByID(i)->endTime > this->maxTime)
159      this->maxTime = this->firstTrackElem->findByID(i)->endTime; // very bad implemented :/
160      */
161}
162
[10091]163Vector Track::calcPos() const
[10085]164{
[10096]165  return this->curve->calcPos(this->localTime/this->duration);
[10085]166}
167
[10091]168Vector Track::calcDir() const
[10085]169{
[10096]170  return this->curve->calcDir(this->localTime/this->duration);
[10085]171}
172
173void Track::tick(float dt)
174{
[10088]175//   PRINTF(4)("CurrentTrackID: %d, LocalTime is: %f, timestep is: %f\n", this->currentTrackElem->ID, this->localTime, dt);
176//   if (this->localTime <= this->firstTrackElem->duration)
177//     this->jumpTo(this->localTime);
178//   if (this->localTime <= this->maxTime)
[10096]179     this->localTime += dt;
[10088]180//   if (this->localTime > this->currentTrackElem->endTime
181//       && this->currentTrackElem->children)
182//     {
183//       if (this->currentTrackElem->jumpTime != 0.0)
184//         this->jumpTo(this->localTime + this->currentTrackElem->jumpTime);
185//       // jump to the next TrackElement and also set the history of the new Element to the old one.
186//       TrackElement* tmpHistoryElem = this->currentTrackElem;
187//       this->currentTrackElem = this->currentTrackElem->getChild(this->choosePath(this->currentTrackElem));
188//       this->currentTrackElem->history = tmpHistoryElem;
189//       if (this->currentTrackElem->getName())
190//         {
191//           this->trackText->setText(this->currentTrackElem->getName());
192//           this->textAnimation->replay();
193//         }
194//     }
[10096]195   if (this->trackNode)
196     {
197       Vector tmp = this->calcPos();
[10209]198       //Quaternion quat = Quaternion(this->calcDir(), Vector(this->curve->calcAcc(this->localTime/this->duration).x,1,this->curve->calcAcc(this->localTime/this->duration).z));
199       Quaternion quat = Quaternion(this->calcDir(), 0);
[10096]200
201       Vector v(0.0, 1.0, 0.0);
202       Quaternion q(-PI/2, v);
203       quat = quat * q;
204
205       // move trackNode of the track
206       this->trackNode->setAbsCoor(tmp);
207       // set direction and roll angle of trackNode
208       this->trackNode->setAbsDir(quat);
209     }
[10085]210}
211
212/**
213 * @returns the main TrackNode
214*/
[10091]215PNode* Track::getTrackNode()
[10085]216{
217  return this->trackNode;
218}
Note: See TracBrowser for help on using the repository browser.