Changeset 3746 in orxonox.OLD for orxonox/branches/levelloader/src/simple_animation.cc
- Timestamp:
- Apr 7, 2005, 3:54:49 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/branches/levelloader/src/simple_animation.cc
r3605 r3746 1 1 2 2 3 … … 19 20 #include "simple_animation.h" 20 21 #include "stdincl.h" 21 #include "p_node.h" 22 #include "vector.h" 23 #include "world_entity.h" 22 24 23 25 using namespace std; 24 26 27 28 29 SimpleAnimation* SimpleAnimation::singletonRef = 0; 30 /** 31 \brief gets the singleton instance 32 \returns singleton instance 33 */ 34 SimpleAnimation* SimpleAnimation::getInstance() 35 { 36 if( singletonRef == NULL) 37 singletonRef = new SimpleAnimation(); 38 return singletonRef; 39 } 40 25 41 /** 26 42 \brief standard constructor 27 \param the point of the object 28 \param and the orientation of it 29 \param at this time 30 */ 31 KeyFrame::KeyFrame(Vector* point, Quaternion* orientation, float time) 32 { 33 this->setRelCoor(point); 34 this->setRelDir(orientation); 35 this->time = time; 36 } 37 38 39 /** 40 \brief standard constructor 41 \param the point of the object 42 \param and the orientation of it 43 \param at this time 44 \param function of the velocity of the movement 45 */ 46 KeyFrame::KeyFrame(Vector* point, Quaternion* orientation, float time, movementMode mode) 47 { 48 this->setRelCoor(point); 49 this->setRelDir(orientation); 50 this->time = time; 51 this->mode = mode; 52 } 53 54 55 /** 56 \brief standard deconstructor 57 */ 58 KeyFrame::~KeyFrame() 59 { 60 } 61 62 63 /** 64 \brief sets the important properties of a Keyframe 65 \param the point of the object 66 \param and the orientation of it 67 \param at this time 68 */ 69 void KeyFrame::set(Vector* point, Quaternion* orientation, float time) 70 { 71 this->setRelCoor(point); 72 this->setRelDir(orientation); 73 this->time = time; 74 } 75 76 77 /** 78 \brief sets the important properties of a Keyframe 79 \param the point of the object 80 \param and the orientation of it 81 \param at this time 82 \param function of the velocity of the movement 83 */ 84 void KeyFrame::set(Vector* point, Quaternion* orientation, float time, movementMode mode) 85 { 86 this->setRelCoor(point); 87 this->setRelDir(orientation); 88 this->time = time; 89 this->mode = mode; 90 } 91 92 93 94 /** 95 \brief standard constructor 96 */ 97 SimpleAnimation::SimpleAnimation (PNode* parent) 43 */ 44 SimpleAnimation::SimpleAnimation () 98 45 { 99 46 this->setClassName ("SimpleAnimation"); 100 47 this->frames = new tList<KeyFrame>(); 48 this->animators = new tList<Animation>(); 101 49 this->localTime = 0; 102 this->bPause = false; 103 this->parent = parent; 50 this->bRunning = false; 51 this->currentFrame = NULL; 52 this->lastFrame = NULL; 53 54 this->tmpVect = new Vector(); 55 this->lastPosition = new Vector(); 56 this->deltaT = 0.2; 104 57 } 105 58 … … 111 64 SimpleAnimation::~SimpleAnimation () 112 65 { 113 KeyFrame* frame = this->frames->enumerate(); 66 tIterator<KeyFrame>* iterator = this->frames->getIterator(); 67 KeyFrame* frame = iterator->nextElement(); 114 68 while( frame != NULL) 115 69 { 116 70 delete frame; 117 frame = frames->nextElement(); 118 } 71 frame = iterator->nextElement(); 72 } 73 delete iterator; 119 74 delete this->frames; 75 } 76 77 78 /** 79 \brief this determines the start of an Animator Describtion 80 81 this can then be followed by different commands like addKeyFrame(..) etc. and 82 will be closed with AnimatiorEnd() 83 */ 84 void SimpleAnimation::animatorBegin() 85 { 86 this->bDescriptive = true; 87 } 88 89 90 /** 91 \brief this determines the end of an Animator Describtion 92 93 this can then be followed by different commands like addKeyFrame(..) etc. and 94 will be closed with AnimatiorEnd() 95 */ 96 void SimpleAnimation::animatorEnd() 97 { 98 this->workingObject = NULL; 99 this->bDescriptive = false; 100 } 101 102 103 /* 104 Vector* lastPosition; 105 Vector* tmpVect; 106 tList<KeyFrame>* frames; 107 animationMode animMode; 108 movementMode movMode; 109 bool bRunning; 110 float deltaT; 111 */ 112 113 /** 114 \brief select an object to work on by using this function 115 \param object wo work on 116 */ 117 void SimpleAnimation::selectObject(WorldEntity* entity) 118 { 119 Animation* anim = getAnimationFromWorldEntity(entity); 120 if( anim == NULL) 121 { 122 printf("SimpleAnimation::selectObject() - object not found, creating one\n"); 123 anim = new Animation; 124 anim->object = entity; 125 anim->lastPosition = new Vector(); 126 anim->tmpVect = new Vector(); 127 frames = new tList<KeyFrame>(); 128 bRunning = false; 129 deltaT = 0.0; 130 this->animators->add(anim); 131 } 132 this->workingAnimator = anim; 120 133 } 121 134 … … 125 138 \brief adds a keyframe with properties 126 139 \param the point of the object 127 \param and the orientation of it140 \param and the direction of it 128 141 \param at this time 129 142 */ 130 void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* orientation, float time) 131 { 132 KeyFrame* frame = new KeyFrame(point, orientation, time); 143 void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time) 144 { 145 if( !this->bDescriptive) 146 { 147 PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n"); 148 return; 149 } 150 KeyFrame* frame = new KeyFrame; 151 frame->position = point; 152 frame->direction = direction; 153 frame->time = time; 154 frame->mode = DEFAULT_ANIMATION_MODE; 155 frame->object = this->workingObject; 133 156 this->frames->add(frame); 134 157 } … … 138 161 \brief adds a keyframe with properties 139 162 \param the point of the object 140 \param and the orientation of it163 \param and the direction of it 141 164 \param at this time 142 165 \param function of the velocity of the movement 143 166 */ 144 void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* orientation, float time, movementMode mode) 145 { 146 KeyFrame* frame = new KeyFrame(point, orientation, time, mode); 167 void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time, movementMode mode) 168 { 169 if( !this->bDescriptive) 170 { 171 PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n"); 172 return; 173 } 174 KeyFrame* frame = new KeyFrame; 175 frame->position = point; 176 frame->direction = direction; 177 frame->time = time; 178 frame->mode = mode; 179 frame->object = this->workingObject; 147 180 this->frames->add(frame); 148 181 } … … 154 187 void SimpleAnimation::addKeyFrame(KeyFrame* frame) 155 188 { 189 if( !this->bDescriptive) 190 { 191 PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n"); 192 return; 193 } 194 frame->object = this->workingObject; 156 195 this->frames->add(frame); 157 196 } … … 163 202 void SimpleAnimation::reset() 164 203 { 165 KeyFrame* frame = this->frames->enumerate(); 204 tIterator<KeyFrame>* iterator = this->frames->getIterator(); 205 KeyFrame* frame = iterator->nextElement(); 166 206 while( frame != NULL) 167 207 { 168 208 delete frame; 169 frame = frames->nextElement(); 170 } 209 frame = iterator->nextElement(); 210 } 211 delete iterator; 171 212 delete this->frames; 172 213 173 214 this->frames = new tList<KeyFrame>(); 174 215 this->localTime = 0; 175 this->bPause = false; 176 216 this->bRunning = false; 217 218 this->currentFrame = NULL; 219 this->lastFrame = NULL; 177 220 } 178 221 … … 181 224 */ 182 225 void SimpleAnimation::start() 183 {} 226 { 227 if( this->bRunning) 228 { 229 PRINTF(2)("SimpleAnimatin is already running. You are trying to start it again.\n"); 230 return; 231 } 232 233 this->localTime = 0; 234 this->lastFrame = this->frames->firstElement(); 235 this->currentFrame = this->frames->nextElement(this->currentFrame); 236 this->bRunning = true; 237 } 184 238 185 239 … … 188 242 */ 189 243 void SimpleAnimation::stop() 190 {} 244 { 245 this->bRunning = false; 246 } 191 247 192 248 /** … … 196 252 { 197 253 this->localTime = 0; 198 this->bPause = false; 254 this->lastFrame = this->frames->firstElement(); 255 this->currentFrame = this->frames->nextElement(this->currentFrame); 256 this->bRunning = true; 199 257 } 200 258 … … 204 262 void SimpleAnimation::pause() 205 263 { 206 this->b Pause = true;264 this->bRunning = false; 207 265 } 208 266 … … 212 270 void SimpleAnimation::resume() 213 271 { 214 this->b Pause = false;272 this->bRunning = true; 215 273 } 216 274 … … 221 279 void SimpleAnimation::tick(float time) 222 280 { 223 if(!this->bPause) 224 { 225 226 227 228 } 229 } 281 this->localTime += time; 282 tIterator<Animation>* iterator = this->animators->getIterator(); 283 Animation* anim = iterator->nextElement(); 284 while( anim != NULL) 285 { 286 if( anim->bRunning) 287 { 288 289 290 /* first get the current frame via time-stamps */ 291 while( this->localTime > anim->currentFrame->time) 292 { 293 printf("SimpleAnimation::tick(...) - changing Frame\n"); 294 295 //this->currentFrame->object->setRelCoor(*this->currentFrame->position); 296 *anim->lastPosition = *anim->currentFrame->position; 297 298 anim->lastFrame = anim->currentFrame; 299 anim->currentFrame = anim->frames->nextElement(anim->currentFrame); 300 anim->movMode = anim->currentFrame->mode; 301 if( anim->movMode == NEG_EXP) 302 { 303 *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position; 304 anim->deltaT = 1/anim->currentFrame->time * logf(1.0 + 600.0/anim->tmpVect->len()); 305 } 306 } 307 308 /* now animate it */ 309 switch( anim->movMode) 310 { 311 case LINEAR: 312 313 *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position; 314 *anim->tmpVect = *anim->tmpVect * this->localTime / anim->currentFrame->time; 315 anim->currentFrame->object->setRelCoor(*anim->lastFrame->position + *anim->tmpVect); 316 *anim->lastPosition = *anim->tmpVect; 317 break; 318 case EXP: 319 320 break; 321 case NEG_EXP: 322 *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position; 323 *anim->tmpVect = *anim->tmpVect * (1 - exp(- this->localTime * anim->deltaT)); 324 anim->currentFrame->object->setRelCoor(*anim->lastFrame->position + *anim->tmpVect); 325 *anim->lastPosition = *anim->tmpVect; 326 break; 327 case SIN: 328 *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position; 329 *anim->tmpVect = *anim->tmpVect * 0.5*(1 - cos(M_PI * this->localTime / anim->currentFrame->time)); 330 anim->currentFrame->object->setRelCoor(*anim->lastFrame->position + *anim->tmpVect); 331 *anim->lastPosition = *anim->tmpVect; 332 break; 333 case COS: 334 335 break; 336 case QUADRATIC: 337 *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position; 338 *anim->tmpVect = *anim->tmpVect * 1/3 * ldexpf(this->localTime, 3); 339 break; 340 default: 341 break; 342 } 343 } 344 anim = iterator->nextElement(); 345 } 346 delete anim; 347 } 348 349 350 351 Animation* SimpleAnimation::getAnimationFromWorldEntity(WorldEntity* entity) 352 { 353 tIterator<Animation>* iterator = this->animators->getIterator(); 354 Animation* anim = iterator->nextElement(); 355 while( anim != NULL) 356 { 357 if( anim->object == entity) 358 return anim; 359 anim = iterator->nextElement(); 360 } 361 delete iterator; 362 return NULL; 363 }
Note: See TracChangeset
for help on using the changeset viewer.