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