Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

connected the track with the worldentities and the spaceship

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