Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 7, 2005, 3:54:49 PM (20 years ago)
Author:
chris
Message:

orxonox/branches/levelloader: Merged trunk into branch… still not working though…

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/levelloader/src/simple_animation.cc

    r3605 r3746  
     1
    12
    23
     
    1920#include "simple_animation.h"
    2021#include "stdincl.h"
    21 #include "p_node.h"
     22#include "vector.h"
     23#include "world_entity.h"
    2224
    2325using namespace std;
    2426
     27
     28
     29SimpleAnimation* SimpleAnimation::singletonRef = 0;
     30/**
     31   \brief gets the singleton instance
     32   \returns singleton instance
     33*/
     34SimpleAnimation* SimpleAnimation::getInstance()
     35{
     36  if( singletonRef == NULL)
     37    singletonRef = new SimpleAnimation();
     38  return singletonRef;
     39}
     40
    2541/**
    2642   \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*/
     44SimpleAnimation::SimpleAnimation ()
    9845{
    9946   this->setClassName ("SimpleAnimation");
    10047   this->frames = new tList<KeyFrame>();
     48   this->animators = new tList<Animation>();
    10149   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;
    10457}
    10558
     
    11164SimpleAnimation::~SimpleAnimation ()
    11265{
    113   KeyFrame* frame = this->frames->enumerate();
     66  tIterator<KeyFrame>* iterator = this->frames->getIterator();
     67  KeyFrame* frame = iterator->nextElement();
    11468  while( frame != NULL)
    11569    {
    11670      delete frame;
    117       frame = frames->nextElement();
    118     }
     71      frame = iterator->nextElement();
     72    }
     73  delete iterator;
    11974  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*/
     84void 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*/
     96void 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*/
     117void 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;
    120133}
    121134
     
    125138   \brief adds a keyframe with properties
    126139   \param the point of the object
    127    \param and the orientation of it
     140   \param and the direction of it
    128141   \param at this time
    129142*/
    130 void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* orientation, float time)
    131 {
    132   KeyFrame* frame = new KeyFrame(point, orientation, time);
     143void 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;
    133156  this->frames->add(frame);
    134157}
     
    138161   \brief adds a keyframe with properties
    139162   \param the point of the object
    140    \param and the orientation of it
     163   \param and the direction of it
    141164   \param at this time
    142165   \param function of the velocity of the movement
    143166*/
    144 void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* orientation, float time, movementMode mode)
    145 {
    146   KeyFrame* frame = new KeyFrame(point, orientation, time, mode);
     167void 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;
    147180  this->frames->add(frame);
    148181}
     
    154187void SimpleAnimation::addKeyFrame(KeyFrame* frame)
    155188{
     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;
    156195  this->frames->add(frame);
    157196}
     
    163202void SimpleAnimation::reset()
    164203{
    165   KeyFrame* frame = this->frames->enumerate();
     204  tIterator<KeyFrame>* iterator = this->frames->getIterator();
     205  KeyFrame* frame = iterator->nextElement();
    166206  while( frame != NULL)
    167207    {
    168208      delete frame;
    169       frame = frames->nextElement();
    170     }
     209      frame = iterator->nextElement();
     210    }
     211  delete iterator;
    171212  delete this->frames;
    172213
    173214  this->frames = new tList<KeyFrame>();
    174215  this->localTime = 0;
    175   this->bPause = false;
    176 
     216  this->bRunning = false;
     217
     218  this->currentFrame = NULL;
     219  this->lastFrame = NULL;
    177220}
    178221
     
    181224*/
    182225void 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}
    184238
    185239
     
    188242*/
    189243void SimpleAnimation::stop()
    190 {}
     244{
     245  this->bRunning = false;
     246}
    191247
    192248/**
     
    196252{
    197253  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;
    199257}
    200258
     
    204262void SimpleAnimation::pause()
    205263{
    206   this->bPause = true;
     264  this->bRunning = false;
    207265}
    208266
     
    212270void SimpleAnimation::resume()
    213271{
    214   this->bPause = false;
     272  this->bRunning = true;
    215273}
    216274
     
    221279void SimpleAnimation::tick(float time)
    222280{
    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
     351Animation* 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.