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 | |
---|
32 | ObjectListDefinition(Track); |
---|
33 | // CREATE_FACTORY(Track); |
---|
34 | |
---|
35 | |
---|
36 | /** |
---|
37 | * standard constructor |
---|
38 | */ |
---|
39 | Track::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 | */ |
---|
49 | Track::Track(const TiXmlElement* root) |
---|
50 | { |
---|
51 | this->init(); |
---|
52 | |
---|
53 | if (root != NULL) |
---|
54 | this->loadParams(root); |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | /** |
---|
59 | * initializes this class |
---|
60 | */ |
---|
61 | void Track::init() |
---|
62 | { |
---|
63 | this->curveType = CURVE_BEZIER; |
---|
64 | // this->startingTime = 0; |
---|
65 | this->duration = 10; |
---|
66 | this->endTime = 10; |
---|
67 | this->width = 10; |
---|
68 | this->curve = new BezierCurve(); |
---|
69 | this->trackNode = new PNode(PNode::getNullParent(), PNODE_ALL); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * standard destructor |
---|
74 | */ |
---|
75 | Track::~Track() |
---|
76 | { |
---|
77 | |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | void 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"); |
---|
87 | |
---|
88 | } |
---|
89 | LOAD_PARAM_END_CYCLE(element); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | |
---|
94 | /** |
---|
95 | * This function adds a point with its coordinates to the track |
---|
96 | * @param x |
---|
97 | * @param y |
---|
98 | * @param z |
---|
99 | */ |
---|
100 | void Track::addPoint(float x, float y, float z) |
---|
101 | { |
---|
102 | this->addPoint(Vector (x,y,z)); |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | /** |
---|
107 | * This function adds a point to the track as a vector |
---|
108 | * @param newPoint |
---|
109 | */ |
---|
110 | void Track::addPoint(Vector newPoint) |
---|
111 | { |
---|
112 | this->curve->addNode(newPoint); |
---|
113 | this->nodeCount++; |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * We probably doesn't even need this |
---|
118 | */ |
---|
119 | void Track::finalize() |
---|
120 | { |
---|
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 | // } |
---|
154 | |
---|
155 | |
---|
156 | |
---|
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 | |
---|
163 | Vector Track::calcPos() const |
---|
164 | { |
---|
165 | return this->curve->calcPos(this->localTime/this->duration); |
---|
166 | } |
---|
167 | |
---|
168 | Vector Track::calcDir() const |
---|
169 | { |
---|
170 | return this->curve->calcDir(this->localTime/this->duration); |
---|
171 | } |
---|
172 | |
---|
173 | void Track::tick(float dt) |
---|
174 | { |
---|
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) |
---|
179 | this->localTime += dt; |
---|
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 | // } |
---|
195 | if (this->trackNode) |
---|
196 | { |
---|
197 | Vector tmp = this->calcPos(); |
---|
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); |
---|
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 | } |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | * @returns the main TrackNode |
---|
214 | */ |
---|
215 | PNode* Track::getTrackNode() |
---|
216 | { |
---|
217 | return this->trackNode; |
---|
218 | } |
---|