[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; |
---|
[10091] | 67 | this->duration = 10; |
---|
[10096] | 68 | this->endTime = 10; |
---|
[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; |
---|
[10088] | 73 | } |
---|
| 74 | |
---|
| 75 | /** |
---|
[10085] | 76 | * standard destructor |
---|
| 77 | */ |
---|
| 78 | Track::~Track() |
---|
| 79 | { |
---|
[10091] | 80 | |
---|
[10085] | 81 | } |
---|
| 82 | |
---|
[10088] | 83 | |
---|
[10085] | 84 | void Track::loadParams(const TiXmlElement* root) |
---|
| 85 | { |
---|
| 86 | LOAD_PARAM_START_CYCLE(root, element); |
---|
| 87 | { |
---|
[10284] | 88 | LoadParam_CYCLE(element, "addPoint", this, Track, addPoint) |
---|
[10085] | 89 | .describe("Adds a new Point to the currently selected TrackElement"); |
---|
[10088] | 90 | |
---|
[10085] | 91 | } |
---|
| 92 | LOAD_PARAM_END_CYCLE(element); |
---|
| 93 | } |
---|
| 94 | |
---|
[10088] | 95 | |
---|
| 96 | |
---|
| 97 | /** |
---|
[10096] | 98 | * This function adds a point with its coordinates to the track |
---|
[10088] | 99 | * @param x |
---|
| 100 | * @param y |
---|
| 101 | * @param z |
---|
| 102 | */ |
---|
[10085] | 103 | void Track::addPoint(float x, float y, float z) |
---|
| 104 | { |
---|
[10284] | 105 | this->addPointV(Vector (x,y,z)); |
---|
[10085] | 106 | } |
---|
| 107 | |
---|
[10088] | 108 | |
---|
| 109 | /** |
---|
[10096] | 110 | * This function adds a point to the track as a vector |
---|
[10088] | 111 | * @param newPoint |
---|
| 112 | */ |
---|
[10284] | 113 | void Track::addPointV(Vector newPoint) |
---|
[10085] | 114 | { |
---|
[10091] | 115 | this->curve->addNode(newPoint); |
---|
[10284] | 116 | if( this->nodeCount == 0) this->trackNode->setAbsCoor(newPoint); |
---|
[10091] | 117 | this->nodeCount++; |
---|
[10284] | 118 | PRINTF(4)("Point added to curve %d\n"); |
---|
[10085] | 119 | } |
---|
| 120 | |
---|
[10088] | 121 | /** |
---|
[10096] | 122 | * We probably doesn't even need this |
---|
[10088] | 123 | */ |
---|
[10284] | 124 | //void Track::finalize() |
---|
| 125 | //{ |
---|
[10088] | 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 | // } |
---|
[10085] | 159 | |
---|
| 160 | |
---|
[10088] | 161 | |
---|
[10085] | 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 | */ |
---|
[10284] | 166 | //} |
---|
[10085] | 167 | |
---|
[10091] | 168 | Vector Track::calcPos() const |
---|
[10085] | 169 | { |
---|
[10096] | 170 | return this->curve->calcPos(this->localTime/this->duration); |
---|
[10085] | 171 | } |
---|
| 172 | |
---|
[10091] | 173 | Vector Track::calcDir() const |
---|
[10085] | 174 | { |
---|
[10096] | 175 | return this->curve->calcDir(this->localTime/this->duration); |
---|
[10085] | 176 | } |
---|
| 177 | |
---|
| 178 | void Track::tick(float dt) |
---|
| 179 | { |
---|
[10088] | 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) |
---|
[10096] | 184 | this->localTime += dt; |
---|
[10088] | 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 | // } |
---|
[10096] | 200 | if (this->trackNode) |
---|
| 201 | { |
---|
| 202 | Vector tmp = this->calcPos(); |
---|
[10209] | 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); |
---|
[10096] | 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 |
---|
[10284] | 211 | this->trackNode->shiftCoor(tmp - this->trackNode->getAbsCoor()); |
---|
[10096] | 212 | // set direction and roll angle of trackNode |
---|
[10284] | 213 | // this->trackNode->setAbsDir(quat); |
---|
[10096] | 214 | } |
---|
[10085] | 215 | } |
---|
| 216 | |
---|
| 217 | /** |
---|
| 218 | * @returns the main TrackNode |
---|
| 219 | */ |
---|
[10091] | 220 | PNode* Track::getTrackNode() |
---|
[10085] | 221 | { |
---|
| 222 | return this->trackNode; |
---|
| 223 | } |
---|
[10284] | 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 | } |
---|