[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" |
---|
[10618] | 27 | #include "glincl.h" |
---|
[10088] | 28 | #include "p_node.h" |
---|
[10085] | 29 | |
---|
[10088] | 30 | #include "debug.h" |
---|
| 31 | |
---|
[10085] | 32 | ObjectListDefinition(Track); |
---|
[10088] | 33 | // CREATE_FACTORY(Track); |
---|
[10085] | 34 | |
---|
[10088] | 35 | |
---|
[10084] | 36 | /** |
---|
| 37 | * standard constructor |
---|
| 38 | */ |
---|
| 39 | Track::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 | */ |
---|
| 49 | Track::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 | */ |
---|
| 61 | void Track::init() |
---|
| 62 | { |
---|
| 63 | this->curveType = CURVE_BEZIER; |
---|
[10096] | 64 | // this->startingTime = 0; |
---|
[10297] | 65 | this->duration = 20; |
---|
| 66 | this->endTime = 20; |
---|
[10640] | 67 | this->width = 24; |
---|
| 68 | this->height = 18; |
---|
| 69 | this->depth = 200; |
---|
| 70 | this->stretch = 4; |
---|
[10091] | 71 | this->curve = new BezierCurve(); |
---|
[10088] | 72 | this->trackNode = new PNode(PNode::getNullParent(), PNODE_ALL); |
---|
[10284] | 73 | this->nodeCount = 0; |
---|
[10335] | 74 | this->localTime = 0; |
---|
[10500] | 75 | this->pause = false; |
---|
[10088] | 76 | } |
---|
| 77 | |
---|
| 78 | /** |
---|
[10085] | 79 | * standard destructor |
---|
| 80 | */ |
---|
| 81 | Track::~Track() |
---|
| 82 | { |
---|
[10091] | 83 | |
---|
[10085] | 84 | } |
---|
| 85 | |
---|
[10088] | 86 | |
---|
[10085] | 87 | void Track::loadParams(const TiXmlElement* root) |
---|
| 88 | { |
---|
| 89 | LOAD_PARAM_START_CYCLE(root, element); |
---|
| 90 | { |
---|
[10284] | 91 | LoadParam_CYCLE(element, "addPoint", this, Track, addPoint) |
---|
[10085] | 92 | .describe("Adds a new Point to the currently selected TrackElement"); |
---|
[10297] | 93 | LoadParam_CYCLE(element, "speed", this, Track, setSpeed) |
---|
| 94 | .describe("Sets speed of traveling"); |
---|
[10385] | 95 | LoadParam_CYCLE(element, "mode", this, Track, setMode) |
---|
| 96 | .describe("Sets mode of track behavior"); |
---|
[10085] | 97 | } |
---|
| 98 | LOAD_PARAM_END_CYCLE(element); |
---|
| 99 | } |
---|
| 100 | |
---|
[10088] | 101 | |
---|
| 102 | |
---|
| 103 | /** |
---|
[10096] | 104 | * This function adds a point with its coordinates to the track |
---|
[10088] | 105 | * @param x |
---|
| 106 | * @param y |
---|
| 107 | * @param z |
---|
| 108 | */ |
---|
[10085] | 109 | void Track::addPoint(float x, float y, float z) |
---|
| 110 | { |
---|
[10284] | 111 | this->addPointV(Vector (x,y,z)); |
---|
[10085] | 112 | } |
---|
| 113 | |
---|
[10088] | 114 | |
---|
| 115 | /** |
---|
[10096] | 116 | * This function adds a point to the track as a vector |
---|
[10088] | 117 | * @param newPoint |
---|
| 118 | */ |
---|
[10284] | 119 | void Track::addPointV(Vector newPoint) |
---|
[10085] | 120 | { |
---|
[10091] | 121 | this->curve->addNode(newPoint); |
---|
[10284] | 122 | if( this->nodeCount == 0) this->trackNode->setAbsCoor(newPoint); |
---|
[10091] | 123 | this->nodeCount++; |
---|
[10297] | 124 | // PRINTF(0)("Point added to curve\n"); |
---|
[10085] | 125 | } |
---|
| 126 | |
---|
[10088] | 127 | /** |
---|
[10297] | 128 | * This function sets the speed of the trackNode by altering the duration |
---|
| 129 | * of the time the trackNode travels on the whole track. This is bad because |
---|
| 130 | * the speed depends on the length of the curve. (by getting the curve's length |
---|
| 131 | * this function will make a lot more sense) |
---|
| 132 | */ |
---|
| 133 | void Track::setSpeed(float speed) |
---|
| 134 | { |
---|
| 135 | this->duration = this->duration/speed; |
---|
[10454] | 136 | this->speed = speed; |
---|
[10297] | 137 | } |
---|
| 138 | |
---|
| 139 | /** |
---|
[10385] | 140 | * Sets the mode of the track. 0 means wait at the end. 1 means rerun track |
---|
| 141 | */ |
---|
| 142 | void Track::setMode(int newMode) |
---|
| 143 | { |
---|
| 144 | this->mode = newMode; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | /** |
---|
[10498] | 148 | * Sets the bool if the track runs (false) or not (true) |
---|
| 149 | */ |
---|
| 150 | void Track::pauseTrack(bool stop) |
---|
| 151 | { |
---|
| 152 | this->pause = stop; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | |
---|
| 156 | /** |
---|
[10096] | 157 | * We probably doesn't even need this |
---|
[10088] | 158 | */ |
---|
[10284] | 159 | //void Track::finalize() |
---|
| 160 | //{ |
---|
[10088] | 161 | // for (int i = 1; i<= trackElemCount ;i++) |
---|
| 162 | // { |
---|
| 163 | // TrackElement* tmpElem = this->firstTrackElem->findByID(i); |
---|
| 164 | // if( tmpElem->childCount > 0) |
---|
| 165 | // { |
---|
| 166 | // tIterator<TrackElement>* iterator = tmpElem->children->getIterator(); |
---|
| 167 | // TrackElement* enumElem = iterator->firstElement(); |
---|
| 168 | // //TrackElement* enumElem = tmpElem->children->enumerate(); |
---|
| 169 | // while (enumElem) |
---|
| 170 | // { |
---|
| 171 | // |
---|
| 172 | // // c1-continuity |
---|
| 173 | // enumElem->curve->addNode(enumElem->curve->getNode(0) + |
---|
| 174 | // ((enumElem->curve->getNode(0) - |
---|
| 175 | // tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) |
---|
| 176 | // ),2); |
---|
| 177 | // enumElem->nodeCount++; |
---|
| 178 | // // c2-continuity |
---|
| 179 | // enumElem->curve->addNode((tmpElem->curve->getNode(tmpElem->curve->getNodeCount())- |
---|
| 180 | // tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) * 4 + |
---|
| 181 | // tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-2), 3); |
---|
| 182 | // enumElem->nodeCount++; |
---|
| 183 | // PRINTF(5)("accelerations: %d-in: count: %d, %f, %f, %f\n %d-out: count: %d %f, %f, %f\n", |
---|
| 184 | // tmpElem->ID, tmpElem->nodeCount, |
---|
| 185 | // tmpElem->curve->calcAcc(0.999).x, tmpElem->curve->calcAcc(0.999).y, tmpElem->curve->calcAcc(0.999).z, |
---|
| 186 | // enumElem->ID, enumElem->nodeCount, |
---|
| 187 | // enumElem->curve->calcAcc(0).x, enumElem->curve->calcAcc(0).y, enumElem->curve->calcAcc(0).z); |
---|
| 188 | // |
---|
| 189 | // enumElem = iterator->nextElement(); |
---|
| 190 | // } |
---|
| 191 | // delete iterator; |
---|
| 192 | // } |
---|
| 193 | // } |
---|
[10085] | 194 | |
---|
| 195 | |
---|
[10088] | 196 | |
---|
[10085] | 197 | /*for (int i = 1; i <= trackElemCount;i++) |
---|
| 198 | if (this->firstTrackElem->findByID(i)->endTime > this->maxTime) |
---|
| 199 | this->maxTime = this->firstTrackElem->findByID(i)->endTime; // very bad implemented :/ |
---|
| 200 | */ |
---|
[10284] | 201 | //} |
---|
[10085] | 202 | |
---|
[10091] | 203 | Vector Track::calcPos() const |
---|
[10085] | 204 | { |
---|
[10096] | 205 | return this->curve->calcPos(this->localTime/this->duration); |
---|
[10085] | 206 | } |
---|
| 207 | |
---|
[10091] | 208 | Vector Track::calcDir() const |
---|
[10085] | 209 | { |
---|
[10096] | 210 | return this->curve->calcDir(this->localTime/this->duration); |
---|
[10085] | 211 | } |
---|
| 212 | |
---|
| 213 | void Track::tick(float dt) |
---|
| 214 | { |
---|
[10088] | 215 | // PRINTF(4)("CurrentTrackID: %d, LocalTime is: %f, timestep is: %f\n", this->currentTrackElem->ID, this->localTime, dt); |
---|
| 216 | // if (this->localTime <= this->firstTrackElem->duration) |
---|
| 217 | // this->jumpTo(this->localTime); |
---|
| 218 | // if (this->localTime <= this->maxTime) |
---|
[10410] | 219 | |
---|
[10454] | 220 | |
---|
[10088] | 221 | // if (this->localTime > this->currentTrackElem->endTime |
---|
| 222 | // && this->currentTrackElem->children) |
---|
| 223 | // { |
---|
| 224 | // if (this->currentTrackElem->jumpTime != 0.0) |
---|
| 225 | // this->jumpTo(this->localTime + this->currentTrackElem->jumpTime); |
---|
| 226 | // // jump to the next TrackElement and also set the history of the new Element to the old one. |
---|
| 227 | // TrackElement* tmpHistoryElem = this->currentTrackElem; |
---|
| 228 | // this->currentTrackElem = this->currentTrackElem->getChild(this->choosePath(this->currentTrackElem)); |
---|
| 229 | // this->currentTrackElem->history = tmpHistoryElem; |
---|
| 230 | // if (this->currentTrackElem->getName()) |
---|
| 231 | // { |
---|
| 232 | // this->trackText->setText(this->currentTrackElem->getName()); |
---|
| 233 | // this->textAnimation->replay(); |
---|
| 234 | // } |
---|
| 235 | // } |
---|
[10512] | 236 | if (this->trackNode && !this->pause) |
---|
| 237 | { |
---|
[10454] | 238 | // tmp save |
---|
| 239 | float oldTime = this->localTime; |
---|
| 240 | |
---|
| 241 | if(this->mode == 0) |
---|
| 242 | { |
---|
[10512] | 243 | // PRINTF(0)("localTime %f and duration %f", this->localTime, this->duration); |
---|
| 244 | if(this->localTime + dt < this->duration) |
---|
[10454] | 245 | this->localTime += dt; |
---|
| 246 | } |
---|
| 247 | else |
---|
| 248 | { |
---|
| 249 | this->localTime += dt; |
---|
| 250 | if(this->localTime >= this->duration) |
---|
| 251 | this->localTime = 0; |
---|
| 252 | } |
---|
| 253 | |
---|
[10512] | 254 | if(oldTime != this->localTime) |
---|
[10454] | 255 | { |
---|
[10512] | 256 | Vector tmp = this->calcPos(); |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | Vector dV = tmp - this->trackNode->getAbsCoor(); |
---|
| 260 | float dx = speed * dt; |
---|
| 261 | float ratio = dx / dV.len(); |
---|
| 262 | |
---|
| 263 | if( dt > 0.0f) |
---|
| 264 | { |
---|
| 265 | float newDt = dt * ratio; |
---|
| 266 | this->localTime = oldTime + newDt; |
---|
| 267 | } |
---|
| 268 | tmp = this->calcPos(); |
---|
| 269 | |
---|
| 270 | Vector v(0.0, 1.0, 0.0); |
---|
| 271 | Quaternion quat = Quaternion(this->calcDir(), v); |
---|
| 272 | Quaternion q(-PI/2, v); |
---|
| 273 | quat = quat * q; |
---|
| 274 | |
---|
| 275 | // move trackNode of the track |
---|
| 276 | this->trackNode->shiftCoor(tmp - this->trackNode->getAbsCoor()); |
---|
| 277 | // set direction and roll angle of trackNode |
---|
| 278 | this->trackNode->setAbsDir(quat); |
---|
[10454] | 279 | } |
---|
[10512] | 280 | } |
---|
[10085] | 281 | } |
---|
| 282 | |
---|
| 283 | /** |
---|
| 284 | * @returns the main TrackNode |
---|
| 285 | */ |
---|
[10091] | 286 | PNode* Track::getTrackNode() |
---|
[10085] | 287 | { |
---|
| 288 | return this->trackNode; |
---|
| 289 | } |
---|
[10284] | 290 | |
---|
| 291 | /** |
---|
| 292 | * Imports a model of the Graph into the OpenGL-environment. |
---|
| 293 | * @param dt The Iterator used in seconds for Painting the Graph. |
---|
| 294 | |
---|
| 295 | This is for testing facility only. Do this if you want to see the Path inside the Level. |
---|
| 296 | eventually this will all be packed into a gl-list. |
---|
| 297 | */ |
---|
[10410] | 298 | void Track::drawGraph(float dt) const |
---|
[10284] | 299 | { |
---|
[10410] | 300 | glMatrixMode(GL_MODELVIEW); |
---|
| 301 | glPushMatrix(); |
---|
| 302 | |
---|
| 303 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 304 | |
---|
| 305 | glDisable(GL_LIGHTING); |
---|
| 306 | glDisable(GL_TEXTURE_2D); |
---|
| 307 | glDisable(GL_BLEND); |
---|
| 308 | glLineWidth(2.0); |
---|
| 309 | |
---|
| 310 | |
---|
| 311 | |
---|
| 312 | PNode* node = PNode::getNullParent(); |
---|
| 313 | glTranslatef (node->getAbsCoor ().x, |
---|
| 314 | node->getAbsCoor ().y, |
---|
| 315 | node->getAbsCoor ().z); |
---|
| 316 | Vector tmpRot = node->getAbsDir().getSpacialAxis(); |
---|
| 317 | glRotatef (node->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
| 318 | |
---|
[10412] | 319 | |
---|
[10284] | 320 | glBegin(GL_LINE_STRIP); |
---|
[10410] | 321 | glColor3f(1.0, 1.0, 0.6); |
---|
| 322 | |
---|
[10412] | 323 | Vector tmpVector; |
---|
[10284] | 324 | for(float f = 0.0; f < 1.0; f+=dt) |
---|
| 325 | { |
---|
[10410] | 326 | //PRINTF(0)("drawing",this->calcPos().x, this->calcPos().y, this->calcPos().z); |
---|
[10412] | 327 | tmpVector = this->curve->calcPos(f); |
---|
[10284] | 328 | glVertex3f(tmpVector.x, tmpVector.y, tmpVector.z); |
---|
| 329 | } |
---|
| 330 | glEnd(); |
---|
[10410] | 331 | |
---|
| 332 | glPopAttrib(); |
---|
| 333 | |
---|
| 334 | glPopMatrix(); |
---|
| 335 | } |
---|
[10640] | 336 | |
---|
| 337 | void Track::drawBox() const |
---|
| 338 | { |
---|
| 339 | glMatrixMode(GL_MODELVIEW); |
---|
| 340 | glPushMatrix(); |
---|
| 341 | |
---|
| 342 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 343 | |
---|
| 344 | glDisable(GL_LIGHTING); |
---|
| 345 | glDisable(GL_TEXTURE_2D); |
---|
| 346 | glDisable(GL_BLEND); |
---|
| 347 | glLineWidth(2.0); |
---|
| 348 | |
---|
| 349 | glTranslatef (trackNode->getAbsCoor ().x, |
---|
| 350 | trackNode->getAbsCoor ().y, |
---|
| 351 | trackNode->getAbsCoor ().z); |
---|
| 352 | Vector tmpRot = trackNode->getAbsDir().getSpacialAxis(); |
---|
| 353 | glRotatef (trackNode->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
| 354 | |
---|
| 355 | |
---|
| 356 | glColor3f(1.0, 1.0, 0.0); |
---|
| 357 | glBegin(GL_LINE_STRIP); |
---|
| 358 | glVertex3f(0, height, width); |
---|
| 359 | glVertex3f(0, -height, width); |
---|
| 360 | glVertex3f(0, -height, -width); |
---|
| 361 | glVertex3f(0, height, -width); |
---|
| 362 | glVertex3f(0, height, width); |
---|
| 363 | glEnd(); |
---|
| 364 | |
---|
| 365 | glBegin(GL_LINE_STRIP); |
---|
| 366 | glVertex3f(depth, height * stretch, width * stretch); |
---|
| 367 | glVertex3f(depth, -height * stretch, width * stretch); |
---|
| 368 | glVertex3f(depth, -height * stretch, -width * stretch); |
---|
| 369 | glVertex3f(depth, height * stretch, -width * stretch); |
---|
| 370 | glVertex3f(depth, height * stretch, width * stretch); |
---|
| 371 | glEnd(); |
---|
| 372 | |
---|
| 373 | glBegin(GL_LINE_STRIP); |
---|
| 374 | glVertex3f(depth, height * stretch, width * stretch); |
---|
| 375 | glVertex3f(0, height, width); |
---|
| 376 | glVertex3f(0, -height, width); |
---|
| 377 | glVertex3f(depth, -height * stretch, width * stretch); |
---|
| 378 | glEnd(); |
---|
| 379 | |
---|
| 380 | glBegin(GL_LINE_STRIP); |
---|
| 381 | glVertex3f(depth, height * stretch, -width * stretch); |
---|
| 382 | glVertex3f(0, height, -width); |
---|
| 383 | glVertex3f(0, -height, -width); |
---|
| 384 | glVertex3f(depth, -height * stretch, -width * stretch); |
---|
| 385 | glEnd(); |
---|
| 386 | |
---|
| 387 | glPopMatrix(); |
---|
| 388 | } |
---|