[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 "track/track_manager.h" |
---|
| 29 | #include "p_node.h" |
---|
[10085] | 30 | |
---|
[10088] | 31 | #include "debug.h" |
---|
| 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(); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * initializes this class |
---|
| 58 | */ |
---|
| 59 | void Track::init() |
---|
| 60 | { |
---|
| 61 | // resetting all elements |
---|
| 62 | this->firstTrackElem = NULL; |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | // make a debug track |
---|
| 66 | this->firstTrackElem = new TrackElement(); |
---|
| 67 | this->firstTrackElem->ID = 1; |
---|
| 68 | this->firstTrackElem->setName("root"); |
---|
| 69 | |
---|
| 70 | this->currentTrackElem = firstTrackElem; |
---|
| 71 | |
---|
| 72 | this->curveType = CURVE_BEZIER; |
---|
| 73 | this->trackElemCount = 1; |
---|
| 74 | |
---|
| 75 | this->trackNode = new PNode(PNode::getNullParent(), PNODE_ALL); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | /** |
---|
[10085] | 79 | * standard destructor |
---|
| 80 | */ |
---|
| 81 | Track::~Track() |
---|
| 82 | { |
---|
[10088] | 83 | if( this->firstTrackElem) |
---|
| 84 | delete this->firstTrackElem; |
---|
[10085] | 85 | } |
---|
| 86 | |
---|
[10088] | 87 | |
---|
[10085] | 88 | void Track::loadParams(const TiXmlElement* root) |
---|
| 89 | { |
---|
| 90 | LOAD_PARAM_START_CYCLE(root, element); |
---|
| 91 | { |
---|
| 92 | LoadParam_CYCLE(element, "Point", this, Track, addPoint) |
---|
| 93 | .describe("Adds a new Point to the currently selected TrackElement"); |
---|
[10088] | 94 | |
---|
[10085] | 95 | } |
---|
| 96 | LOAD_PARAM_END_CYCLE(element); |
---|
| 97 | } |
---|
| 98 | |
---|
[10088] | 99 | |
---|
| 100 | |
---|
| 101 | /** |
---|
| 102 | * |
---|
| 103 | * @param x |
---|
| 104 | * @param y |
---|
| 105 | * @param z |
---|
| 106 | */ |
---|
[10085] | 107 | void Track::addPoint(float x, float y, float z) |
---|
| 108 | { |
---|
| 109 | this->addPoint(Vector (x,y,z)); |
---|
| 110 | } |
---|
| 111 | |
---|
[10088] | 112 | |
---|
| 113 | /** |
---|
| 114 | * |
---|
| 115 | * @param newPoint |
---|
| 116 | */ |
---|
[10085] | 117 | void Track::addPoint(Vector newPoint) |
---|
| 118 | { |
---|
[10088] | 119 | // if (this->currentTrackElem->isFresh) |
---|
| 120 | // { |
---|
| 121 | // this->setCurveType(CURVE_BEZIER, this->currentTrackElem); |
---|
| 122 | // this->currentTrackElem->isFresh = false; |
---|
| 123 | // } |
---|
| 124 | // trackElem->curve->addNode(newPoint); |
---|
| 125 | // trackElem->nodeCount++; |
---|
[10085] | 126 | } |
---|
| 127 | |
---|
[10088] | 128 | /** |
---|
| 129 | * |
---|
| 130 | */ |
---|
[10085] | 131 | void Track::finalize() |
---|
| 132 | { |
---|
[10088] | 133 | // for (int i = 1; i<= trackElemCount ;i++) |
---|
| 134 | // { |
---|
| 135 | // TrackElement* tmpElem = this->firstTrackElem->findByID(i); |
---|
| 136 | // if( tmpElem->childCount > 0) |
---|
| 137 | // { |
---|
| 138 | // tIterator<TrackElement>* iterator = tmpElem->children->getIterator(); |
---|
| 139 | // TrackElement* enumElem = iterator->firstElement(); |
---|
| 140 | // //TrackElement* enumElem = tmpElem->children->enumerate(); |
---|
| 141 | // while (enumElem) |
---|
| 142 | // { |
---|
| 143 | // |
---|
| 144 | // // c1-continuity |
---|
| 145 | // enumElem->curve->addNode(enumElem->curve->getNode(0) + |
---|
| 146 | // ((enumElem->curve->getNode(0) - |
---|
| 147 | // tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) |
---|
| 148 | // ),2); |
---|
| 149 | // enumElem->nodeCount++; |
---|
| 150 | // // c2-continuity |
---|
| 151 | // enumElem->curve->addNode((tmpElem->curve->getNode(tmpElem->curve->getNodeCount())- |
---|
| 152 | // tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) * 4 + |
---|
| 153 | // tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-2), 3); |
---|
| 154 | // enumElem->nodeCount++; |
---|
| 155 | // PRINTF(5)("accelerations: %d-in: count: %d, %f, %f, %f\n %d-out: count: %d %f, %f, %f\n", |
---|
| 156 | // tmpElem->ID, tmpElem->nodeCount, |
---|
| 157 | // tmpElem->curve->calcAcc(0.999).x, tmpElem->curve->calcAcc(0.999).y, tmpElem->curve->calcAcc(0.999).z, |
---|
| 158 | // enumElem->ID, enumElem->nodeCount, |
---|
| 159 | // enumElem->curve->calcAcc(0).x, enumElem->curve->calcAcc(0).y, enumElem->curve->calcAcc(0).z); |
---|
| 160 | // |
---|
| 161 | // enumElem = iterator->nextElement(); |
---|
| 162 | // } |
---|
| 163 | // delete iterator; |
---|
| 164 | // } |
---|
| 165 | // } |
---|
[10085] | 166 | |
---|
| 167 | |
---|
[10088] | 168 | |
---|
[10085] | 169 | /*for (int i = 1; i <= trackElemCount;i++) |
---|
| 170 | if (this->firstTrackElem->findByID(i)->endTime > this->maxTime) |
---|
| 171 | this->maxTime = this->firstTrackElem->findByID(i)->endTime; // very bad implemented :/ |
---|
| 172 | */ |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | Vector TrackManager::calcPos() const |
---|
| 176 | { |
---|
| 177 | return this->currentTrackElem->curve->calcPos((this->localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | Vector TrackManager::calcDir() const |
---|
| 181 | { |
---|
| 182 | return this->currentTrackElem->curve->calcDir((this->localTime - this->currentTrackElem->startingTime)/this->currentTrackElem->duration); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | void Track::tick(float dt) |
---|
| 186 | { |
---|
[10088] | 187 | // PRINTF(4)("CurrentTrackID: %d, LocalTime is: %f, timestep is: %f\n", this->currentTrackElem->ID, this->localTime, dt); |
---|
| 188 | // if (this->localTime <= this->firstTrackElem->duration) |
---|
| 189 | // this->jumpTo(this->localTime); |
---|
| 190 | // if (this->localTime <= this->maxTime) |
---|
| 191 | // this->localTime += dt; |
---|
| 192 | // if (this->localTime > this->currentTrackElem->endTime |
---|
| 193 | // && this->currentTrackElem->children) |
---|
| 194 | // { |
---|
| 195 | // if (this->currentTrackElem->jumpTime != 0.0) |
---|
| 196 | // this->jumpTo(this->localTime + this->currentTrackElem->jumpTime); |
---|
| 197 | // // jump to the next TrackElement and also set the history of the new Element to the old one. |
---|
| 198 | // TrackElement* tmpHistoryElem = this->currentTrackElem; |
---|
| 199 | // this->currentTrackElem = this->currentTrackElem->getChild(this->choosePath(this->currentTrackElem)); |
---|
| 200 | // this->currentTrackElem->history = tmpHistoryElem; |
---|
| 201 | // if (this->currentTrackElem->getName()) |
---|
| 202 | // { |
---|
| 203 | // this->trackText->setText(this->currentTrackElem->getName()); |
---|
| 204 | // this->textAnimation->replay(); |
---|
| 205 | // } |
---|
| 206 | // } |
---|
| 207 | // if (this->bindSlave) |
---|
| 208 | // { |
---|
| 209 | // Vector tmp = this->calcPos(); |
---|
| 210 | // Quaternion quat = Quaternion(this->calcDir(), Vector(this->currentTrackElem->curve->calcAcc((localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration).x,1,this->currentTrackElem->curve->calcAcc((localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration).z)); |
---|
| 211 | // |
---|
| 212 | // Vector v(0.0, 1.0, 0.0); |
---|
| 213 | // Quaternion q(-PI/2, v); |
---|
| 214 | // quat = quat * q; |
---|
| 215 | // |
---|
| 216 | // this->bindSlave->setAbsCoor(tmp); |
---|
| 217 | // this->bindSlave->setAbsDir(quat); |
---|
| 218 | // } |
---|
[10085] | 219 | } |
---|
| 220 | |
---|
| 221 | /** |
---|
| 222 | * @returns the main TrackNode |
---|
| 223 | */ |
---|
| 224 | PNode* TrackManager::getTrackNode() |
---|
| 225 | { |
---|
| 226 | return this->trackNode; |
---|
| 227 | } |
---|