[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" |
---|
[10085] | 27 | |
---|
[10088] | 28 | #include "p_node.h" |
---|
[10085] | 29 | |
---|
[10284] | 30 | #include "stdincl.h" |
---|
| 31 | |
---|
[10088] | 32 | #include "debug.h" |
---|
| 33 | |
---|
[10085] | 34 | ObjectListDefinition(Track); |
---|
[10088] | 35 | // CREATE_FACTORY(Track); |
---|
[10085] | 36 | |
---|
[10088] | 37 | |
---|
[10084] | 38 | /** |
---|
| 39 | * standard constructor |
---|
| 40 | */ |
---|
| 41 | Track::Track() |
---|
| 42 | { |
---|
[10088] | 43 | this->init(); |
---|
[10084] | 44 | } |
---|
[10085] | 45 | |
---|
[10088] | 46 | |
---|
[10085] | 47 | /** |
---|
[10088] | 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(); |
---|
[10097] | 54 | |
---|
| 55 | if (root != NULL) |
---|
| 56 | this->loadParams(root); |
---|
[10088] | 57 | } |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * initializes this class |
---|
| 62 | */ |
---|
| 63 | void Track::init() |
---|
| 64 | { |
---|
| 65 | this->curveType = CURVE_BEZIER; |
---|
[10096] | 66 | // this->startingTime = 0; |
---|
[10297] | 67 | this->duration = 20; |
---|
| 68 | this->endTime = 20; |
---|
[10091] | 69 | this->width = 10; |
---|
| 70 | this->curve = new BezierCurve(); |
---|
[10088] | 71 | this->trackNode = new PNode(PNode::getNullParent(), PNODE_ALL); |
---|
[10284] | 72 | this->nodeCount = 0; |
---|
[10335] | 73 | this->localTime = 0; |
---|
[10088] | 74 | } |
---|
| 75 | |
---|
| 76 | /** |
---|
[10085] | 77 | * standard destructor |
---|
| 78 | */ |
---|
| 79 | Track::~Track() |
---|
| 80 | { |
---|
[10091] | 81 | |
---|
[10085] | 82 | } |
---|
| 83 | |
---|
[10088] | 84 | |
---|
[10085] | 85 | void Track::loadParams(const TiXmlElement* root) |
---|
| 86 | { |
---|
| 87 | LOAD_PARAM_START_CYCLE(root, element); |
---|
| 88 | { |
---|
[10284] | 89 | LoadParam_CYCLE(element, "addPoint", this, Track, addPoint) |
---|
[10085] | 90 | .describe("Adds a new Point to the currently selected TrackElement"); |
---|
[10297] | 91 | LoadParam_CYCLE(element, "speed", this, Track, setSpeed) |
---|
| 92 | .describe("Sets speed of traveling"); |
---|
[10088] | 93 | |
---|
[10085] | 94 | } |
---|
| 95 | LOAD_PARAM_END_CYCLE(element); |
---|
| 96 | } |
---|
| 97 | |
---|
[10088] | 98 | |
---|
| 99 | |
---|
| 100 | /** |
---|
[10096] | 101 | * This function adds a point with its coordinates to the track |
---|
[10088] | 102 | * @param x |
---|
| 103 | * @param y |
---|
| 104 | * @param z |
---|
| 105 | */ |
---|
[10085] | 106 | void Track::addPoint(float x, float y, float z) |
---|
| 107 | { |
---|
[10284] | 108 | this->addPointV(Vector (x,y,z)); |
---|
[10085] | 109 | } |
---|
| 110 | |
---|
[10088] | 111 | |
---|
| 112 | /** |
---|
[10096] | 113 | * This function adds a point to the track as a vector |
---|
[10088] | 114 | * @param newPoint |
---|
| 115 | */ |
---|
[10284] | 116 | void Track::addPointV(Vector newPoint) |
---|
[10085] | 117 | { |
---|
[10091] | 118 | this->curve->addNode(newPoint); |
---|
[10284] | 119 | if( this->nodeCount == 0) this->trackNode->setAbsCoor(newPoint); |
---|
[10091] | 120 | this->nodeCount++; |
---|
[10297] | 121 | // PRINTF(0)("Point added to curve\n"); |
---|
[10085] | 122 | } |
---|
| 123 | |
---|
[10088] | 124 | /** |
---|
[10297] | 125 | * This function sets the speed of the trackNode by altering the duration |
---|
| 126 | * of the time the trackNode travels on the whole track. This is bad because |
---|
| 127 | * the speed depends on the length of the curve. (by getting the curve's length |
---|
| 128 | * this function will make a lot more sense) |
---|
| 129 | */ |
---|
| 130 | void Track::setSpeed(float speed) |
---|
| 131 | { |
---|
| 132 | this->duration = this->duration/speed; |
---|
| 133 | |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | /** |
---|
[10096] | 137 | * We probably doesn't even need this |
---|
[10088] | 138 | */ |
---|
[10284] | 139 | //void Track::finalize() |
---|
| 140 | //{ |
---|
[10088] | 141 | // for (int i = 1; i<= trackElemCount ;i++) |
---|
| 142 | // { |
---|
| 143 | // TrackElement* tmpElem = this->firstTrackElem->findByID(i); |
---|
| 144 | // if( tmpElem->childCount > 0) |
---|
| 145 | // { |
---|
| 146 | // tIterator<TrackElement>* iterator = tmpElem->children->getIterator(); |
---|
| 147 | // TrackElement* enumElem = iterator->firstElement(); |
---|
| 148 | // //TrackElement* enumElem = tmpElem->children->enumerate(); |
---|
| 149 | // while (enumElem) |
---|
| 150 | // { |
---|
| 151 | // |
---|
| 152 | // // c1-continuity |
---|
| 153 | // enumElem->curve->addNode(enumElem->curve->getNode(0) + |
---|
| 154 | // ((enumElem->curve->getNode(0) - |
---|
| 155 | // tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) |
---|
| 156 | // ),2); |
---|
| 157 | // enumElem->nodeCount++; |
---|
| 158 | // // c2-continuity |
---|
| 159 | // enumElem->curve->addNode((tmpElem->curve->getNode(tmpElem->curve->getNodeCount())- |
---|
| 160 | // tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) * 4 + |
---|
| 161 | // tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-2), 3); |
---|
| 162 | // enumElem->nodeCount++; |
---|
| 163 | // PRINTF(5)("accelerations: %d-in: count: %d, %f, %f, %f\n %d-out: count: %d %f, %f, %f\n", |
---|
| 164 | // tmpElem->ID, tmpElem->nodeCount, |
---|
| 165 | // tmpElem->curve->calcAcc(0.999).x, tmpElem->curve->calcAcc(0.999).y, tmpElem->curve->calcAcc(0.999).z, |
---|
| 166 | // enumElem->ID, enumElem->nodeCount, |
---|
| 167 | // enumElem->curve->calcAcc(0).x, enumElem->curve->calcAcc(0).y, enumElem->curve->calcAcc(0).z); |
---|
| 168 | // |
---|
| 169 | // enumElem = iterator->nextElement(); |
---|
| 170 | // } |
---|
| 171 | // delete iterator; |
---|
| 172 | // } |
---|
| 173 | // } |
---|
[10085] | 174 | |
---|
| 175 | |
---|
[10088] | 176 | |
---|
[10085] | 177 | /*for (int i = 1; i <= trackElemCount;i++) |
---|
| 178 | if (this->firstTrackElem->findByID(i)->endTime > this->maxTime) |
---|
| 179 | this->maxTime = this->firstTrackElem->findByID(i)->endTime; // very bad implemented :/ |
---|
| 180 | */ |
---|
[10284] | 181 | //} |
---|
[10085] | 182 | |
---|
[10091] | 183 | Vector Track::calcPos() const |
---|
[10085] | 184 | { |
---|
[10096] | 185 | return this->curve->calcPos(this->localTime/this->duration); |
---|
[10085] | 186 | } |
---|
| 187 | |
---|
[10091] | 188 | Vector Track::calcDir() const |
---|
[10085] | 189 | { |
---|
[10096] | 190 | return this->curve->calcDir(this->localTime/this->duration); |
---|
[10085] | 191 | } |
---|
| 192 | |
---|
| 193 | void Track::tick(float dt) |
---|
| 194 | { |
---|
[10088] | 195 | // PRINTF(4)("CurrentTrackID: %d, LocalTime is: %f, timestep is: %f\n", this->currentTrackElem->ID, this->localTime, dt); |
---|
| 196 | // if (this->localTime <= this->firstTrackElem->duration) |
---|
| 197 | // this->jumpTo(this->localTime); |
---|
| 198 | // if (this->localTime <= this->maxTime) |
---|
[10096] | 199 | this->localTime += dt; |
---|
[10335] | 200 | if(this->localTime >= this->duration) |
---|
| 201 | this->localTime = 0; |
---|
[10088] | 202 | // if (this->localTime > this->currentTrackElem->endTime |
---|
| 203 | // && this->currentTrackElem->children) |
---|
| 204 | // { |
---|
| 205 | // if (this->currentTrackElem->jumpTime != 0.0) |
---|
| 206 | // this->jumpTo(this->localTime + this->currentTrackElem->jumpTime); |
---|
| 207 | // // jump to the next TrackElement and also set the history of the new Element to the old one. |
---|
| 208 | // TrackElement* tmpHistoryElem = this->currentTrackElem; |
---|
| 209 | // this->currentTrackElem = this->currentTrackElem->getChild(this->choosePath(this->currentTrackElem)); |
---|
| 210 | // this->currentTrackElem->history = tmpHistoryElem; |
---|
| 211 | // if (this->currentTrackElem->getName()) |
---|
| 212 | // { |
---|
| 213 | // this->trackText->setText(this->currentTrackElem->getName()); |
---|
| 214 | // this->textAnimation->replay(); |
---|
| 215 | // } |
---|
| 216 | // } |
---|
[10096] | 217 | if (this->trackNode) |
---|
| 218 | { |
---|
| 219 | Vector tmp = this->calcPos(); |
---|
[10209] | 220 | //Quaternion quat = Quaternion(this->calcDir(), Vector(this->curve->calcAcc(this->localTime/this->duration).x,1,this->curve->calcAcc(this->localTime/this->duration).z)); |
---|
[10335] | 221 | |
---|
[10096] | 222 | |
---|
| 223 | Vector v(0.0, 1.0, 0.0); |
---|
[10335] | 224 | Quaternion quat = Quaternion(this->calcDir(), v); |
---|
[10096] | 225 | Quaternion q(-PI/2, v); |
---|
| 226 | quat = quat * q; |
---|
| 227 | |
---|
| 228 | // move trackNode of the track |
---|
[10284] | 229 | this->trackNode->shiftCoor(tmp - this->trackNode->getAbsCoor()); |
---|
[10096] | 230 | // set direction and roll angle of trackNode |
---|
[10297] | 231 | this->trackNode->setAbsDir(quat); |
---|
[10096] | 232 | } |
---|
[10085] | 233 | } |
---|
| 234 | |
---|
| 235 | /** |
---|
| 236 | * @returns the main TrackNode |
---|
| 237 | */ |
---|
[10091] | 238 | PNode* Track::getTrackNode() |
---|
[10085] | 239 | { |
---|
| 240 | return this->trackNode; |
---|
| 241 | } |
---|
[10284] | 242 | |
---|
| 243 | /** |
---|
| 244 | * Imports a model of the Graph into the OpenGL-environment. |
---|
| 245 | * @param dt The Iterator used in seconds for Painting the Graph. |
---|
| 246 | |
---|
| 247 | This is for testing facility only. Do this if you want to see the Path inside the Level. |
---|
| 248 | eventually this will all be packed into a gl-list. |
---|
| 249 | */ |
---|
[10297] | 250 | /*void Track::drawGraph(float dt) const |
---|
[10284] | 251 | { |
---|
| 252 | glBegin(GL_LINE_STRIP); |
---|
| 253 | for(float f = 0.0; f < 1.0; f+=dt) |
---|
| 254 | { |
---|
| 255 | // PRINTF(4)("drawing",this->calcPos().x, this->calcPos().y, this->calcPos().z); |
---|
| 256 | Vector tmpVector = this->curve->calcPos(f); |
---|
| 257 | glVertex3f(tmpVector.x, tmpVector.y, tmpVector.z); |
---|
| 258 | } |
---|
| 259 | glEnd(); |
---|
[10297] | 260 | }*/ |
---|