| [3851] | 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 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 | main-programmer: Patrick Boenzli |
|---|
| 13 | co-programmer: Benjamin Grauer |
|---|
| 14 | |
|---|
| 15 | 2005-04-17: Benjamin Grauer |
|---|
| 16 | Rewritte all functions, so it will fit into the Animation-class |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #include "animation3d.h" |
|---|
| 21 | |
|---|
| 22 | #include "stdincl.h" |
|---|
| 23 | #include "vector.h" |
|---|
| 24 | #include "p_node.h" |
|---|
| 25 | |
|---|
| 26 | using namespace std; |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | Animation3D::Animation3D(void) |
|---|
| 30 | { |
|---|
| 31 | // create a new List |
|---|
| 32 | this->keyFrameList = new tList<KeyFrame3D>(); |
|---|
| 33 | KeyFrame3D* tmpKeyFrame = new KeyFrame3D; |
|---|
| 34 | tmpKeyFrame->position = Vector(); |
|---|
| 35 | tmpKeyFrame->direction = Quaternion(); |
|---|
| 36 | keyFrameList->add(tmpKeyFrame); |
|---|
| 37 | |
|---|
| 38 | this->currentKeyFrame = tmpKeyFrame; |
|---|
| 39 | this->nextKeyFrame = tmpKeyFrame; |
|---|
| 40 | |
|---|
| 41 | this->animFunc = &Animation3D::linear; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | Animation3D::~Animation3D(void) |
|---|
| 45 | { |
|---|
| 46 | // delete all the KeyFrames |
|---|
| 47 | tIterator<KeyFrame3D>* itKF = keyFrameList->getIterator(); |
|---|
| 48 | KeyFrame3D* enumKF = itKF->nextElement(); |
|---|
| 49 | while (enumKF) |
|---|
| 50 | { |
|---|
| 51 | delete enumKF; |
|---|
| 52 | enumKF = itKF->nextElement(); |
|---|
| 53 | } |
|---|
| 54 | delete itKF; |
|---|
| 55 | delete this->keyFrameList; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | void Animation3D::rewind(void) |
|---|
| 60 | { |
|---|
| 61 | this->currentKeyFrame = keyFrameList->firstElement(); |
|---|
| 62 | this->nextKeyFrame = keyFrameList->nextElement(keyFrameList->firstElement()); |
|---|
| 63 | this->localTime = 0.0; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | void Animation3D::addKeyFrame(Vector position, Quaternion direction, float duration = 1.0, ANIM_FUNCTION animFunc) |
|---|
| 68 | { |
|---|
| 69 | // some small check |
|---|
| 70 | if (duration <= 0.0) |
|---|
| 71 | duration = 1.0; |
|---|
| 72 | |
|---|
| 73 | KeyFrame3D* tmpKeyFrame; |
|---|
| 74 | |
|---|
| 75 | if (bHasKeys) |
|---|
| 76 | { |
|---|
| 77 | tmpKeyFrame = new KeyFrame3D; |
|---|
| 78 | if (this->currentKeyFrame == this->nextKeyFrame) |
|---|
| 79 | this->nextKeyFrame = tmpKeyFrame; |
|---|
| 80 | this->keyFrameList->add(tmpKeyFrame); |
|---|
| 81 | |
|---|
| 82 | } |
|---|
| 83 | else |
|---|
| 84 | { |
|---|
| 85 | tmpKeyFrame = this->keyFrameList->firstElement(); |
|---|
| 86 | bHasKeys = true; |
|---|
| 87 | this->setAnimFunc(animFunc); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | tmpKeyFrame->position = position; |
|---|
| 91 | tmpKeyFrame->direction = direction; |
|---|
| 92 | tmpKeyFrame->duration = duration; |
|---|
| 93 | tmpKeyFrame->animFunc = animFunc; |
|---|
| 94 | |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | void Animation3D::tick(float timePassed) |
|---|
| 98 | { |
|---|
| 99 | if (this->bRunning) |
|---|
| 100 | { |
|---|
| 101 | this->localTime += timePassed; |
|---|
| 102 | if (localTime >= this->currentKeyFrame->duration) |
|---|
| 103 | { |
|---|
| 104 | // switching to the next Key-Frame |
|---|
| 105 | this->localTime -= this->currentKeyFrame->duration; |
|---|
| 106 | this->currentKeyFrame = this->nextKeyFrame; |
|---|
| 107 | // checking, if we should still Play the animation |
|---|
| 108 | if (this->currentKeyFrame == this->keyFrameList->lastElement()) |
|---|
| 109 | { |
|---|
| 110 | switch (this->postInfinity) |
|---|
| 111 | { |
|---|
| 112 | case ANIM_INF_CONSTANT: |
|---|
| 113 | this->bRunning = false; |
|---|
| 114 | break; |
|---|
| 115 | case ANIM_INF_REWIND: |
|---|
| 116 | break; |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | this->nextKeyFrame = this->keyFrameList->nextElement(this->nextKeyFrame); |
|---|
| 120 | this->setAnimFunc(this->currentKeyFrame->animFunc); |
|---|
| 121 | |
|---|
| 122 | if( this->currentKeyFrame->animFunc == ANIM_NEG_EXP) |
|---|
| 123 | { |
|---|
| 124 | this->tmpVect = this->nextKeyFrame->position - this->currentKeyFrame->position; |
|---|
| 125 | this->deltaT = 1/this->currentKeyFrame->duration * logf(1.0 + 600.0/this->tmpVect.len()); |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | /* now animate it */ |
|---|
| 130 | (this->*animFunc)(this->localTime); |
|---|
| 131 | /* |
|---|
| 132 | switch( this->movMode) |
|---|
| 133 | { |
|---|
| 134 | case LINEAR: |
|---|
| 135 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
|---|
| 136 | *this->tmpVect = *this->tmpVect * this->localTime / this->currentFrame->time; |
|---|
| 137 | this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect); |
|---|
| 138 | *this->lastPosition = *this->tmpVect; |
|---|
| 139 | break; |
|---|
| 140 | case EXP: |
|---|
| 141 | |
|---|
| 142 | break; |
|---|
| 143 | case NEG_EXP: |
|---|
| 144 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
|---|
| 145 | *this->tmpVect = *this->tmpVect * (1 - expf(- this->localTime * this->deltaT)); |
|---|
| 146 | this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect); |
|---|
| 147 | *this->lastPosition = *this->tmpVect; |
|---|
| 148 | break; |
|---|
| 149 | case SIN: |
|---|
| 150 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
|---|
| 151 | *this->tmpVect = *this->tmpVect * 0.5*(1 - cos(M_PI * this->localTime / this->currentFrame->time)); |
|---|
| 152 | this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect); |
|---|
| 153 | *this->lastPosition = *this->tmpVect; |
|---|
| 154 | break; |
|---|
| 155 | case COS: |
|---|
| 156 | |
|---|
| 157 | break; |
|---|
| 158 | case QUADRATIC: |
|---|
| 159 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
|---|
| 160 | *this->tmpVect = *this->tmpVect * 1/3 * ldexpf(this->localTime, 3); |
|---|
| 161 | break; |
|---|
| 162 | default: |
|---|
| 163 | break; |
|---|
| 164 | } |
|---|
| 165 | */ |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | void Animation3D::setAnimFunc(ANIM_FUNCTION animFunc) |
|---|
| 172 | { |
|---|
| 173 | switch (animFunc) |
|---|
| 174 | { |
|---|
| 175 | default: |
|---|
| 176 | case ANIM_CONSTANT: |
|---|
| 177 | this->animFunc = &Animation3D::constant; |
|---|
| 178 | break; |
|---|
| 179 | case ANIM_LINEAR: |
|---|
| 180 | this->animFunc = &Animation3D::linear; |
|---|
| 181 | break; |
|---|
| 182 | case ANIM_SINE: |
|---|
| 183 | this->animFunc = &Animation3D::sine; |
|---|
| 184 | break; |
|---|
| 185 | case ANIM_COSINE: |
|---|
| 186 | this->animFunc = &Animation3D::cosine; |
|---|
| 187 | break; |
|---|
| 188 | case ANIM_EXP: |
|---|
| 189 | this->animFunc = &Animation3D::exp; |
|---|
| 190 | break; |
|---|
| 191 | case ANIM_NEG_EXP: |
|---|
| 192 | this->animFunc = &Animation3D::negExp; |
|---|
| 193 | break; |
|---|
| 194 | case ANIM_QUADRATIC: |
|---|
| 195 | this->animFunc = &Animation3D::quadratic; |
|---|
| 196 | break; |
|---|
| 197 | case ANIM_RANDOM: |
|---|
| 198 | this->animFunc = &Animation3D::random; |
|---|
| 199 | break; |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | float Animation3D::constant(float timePassed) const |
|---|
| 204 | { |
|---|
| 205 | |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | float Animation3D::linear(float timePassed) const |
|---|
| 209 | { |
|---|
| 210 | |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | float Animation3D::sine(float timePassed) const |
|---|
| 214 | { |
|---|
| 215 | |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | float Animation3D::cosine(float timePassed) const |
|---|
| 219 | { |
|---|
| 220 | |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | float Animation3D::exp(float timePassed) const |
|---|
| 224 | { |
|---|
| 225 | |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | float Animation3D::negExp(float timePassed) const |
|---|
| 229 | { |
|---|
| 230 | |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | float Animation3D::quadratic(float timePassed) const |
|---|
| 234 | { |
|---|
| 235 | |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | float Animation3D::random(float timePassed) const |
|---|
| 239 | { |
|---|
| 240 | |
|---|
| 241 | } |
|---|