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