Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

c:/home/kevn/src/animaniac/ani/Dynamics/systems/TriSpring.h

Go to the documentation of this file.
00001 
00002 // i use a lot of operators...
00003 #include "ani/Dynamics/operators/Gravity.h"
00004 #include "ani/Dynamics/operators/ViscousDragOperator.h"
00005 #include "ani/Dynamics/operators/GrimReaperOperator.h"
00006 #include "ani/Dynamics/operators/ColorWithAgeOperator.h"
00007 #include "ani/Dynamics/operators/SpringForceOperator.h"
00008 
00010 class TriSpring : public ParticleSystem
00011 {
00012 public:
00013       TriSpring() : ParticleSystem(), a(NULL), b(NULL), c(NULL), velocitizer(0.0f)
00014       {
00015          this->init();
00016       }
00017       
00018       virtual void clear()
00019       {
00020          ParticleSystem::clear();
00021          
00022          if (a != NULL)
00023          {
00024             a->deref(); a = NULL;
00025          }
00026          if (b != NULL)
00027          {
00028             b->deref(); b = NULL;
00029          }
00030          if (c != NULL)
00031          {
00032             c->deref(); c = NULL;
00033          }
00034       }
00035             
00036       void init()
00037       {
00038          this->clear();
00039 
00040          //ODEsolver* solver = new EulerODEsolver;         // 1st order
00041          //ODEsolver* solver = new ModifiedEulerODEsolver; // 2nd order
00042          //ODEsolver* solver = new HeunODEsolver;          // 2nd order
00043          ODEsolver* solver = new RungeKuttaODEsolver;    // 4th order
00044          this->setSolver( solver );
00045          solver->deref();
00046 
00047          // operations
00048          Gravity* gravity = new Gravity;
00049          this->add( gravity );
00050          GrimReaper* reaper = new GrimReaper;
00051          this->add( reaper );
00052          ColorWithAge* cwa = new ColorWithAge;
00053          this->add( cwa );
00054          ViscousDrag* drag = new ViscousDrag;
00055          drag->setDrag( 0.39f ); // ??
00056          this->add( drag );
00057 
00058          setupSpring( *this, gravity );
00059          
00060          drag->deref();
00061          gravity->deref();
00062          reaper->deref();
00063          cwa->deref();
00064       }
00065 
00066       void setupSpring( ParticleSystem& s, GlobalOperator* ignore )
00067       {
00068          a = new Particle;
00069          a->setPosition( gmtl::Vec3f(6, 3, 0) );
00070          a->setMass( 2 );
00071 
00072          b = new Particle;
00073          b->setPosition( gmtl::Vec3f(-6, 3, 0) );
00074          b->setMass( 1 );
00075 
00076          c = new Particle;
00077          c->setPosition( gmtl::Vec3f(-6, 3, -6) );
00078          c->setMass( 1.5 );
00079 
00080          Spring* spring = new Spring;
00081          s.add( a );
00082          s.add( b );
00083          s.add( spring );
00084          if (ignore) ignore->ignore( a );
00085          if (ignore) ignore->ignore( b );
00086          spring->setParticles( a, b );
00087          spring->setSpringConstant( 5.0 );
00088          spring->setDampeningConstant( 0.5 );
00089          spring->setRestLength( 5 );
00090 
00091          Spring* spring2 = new Spring;
00092          s.add( c );
00093          s.add( spring2 );
00094          if (ignore) ignore->ignore( c );
00095          spring2->setParticles( a, c );
00096          spring2->setSpringConstant( 20.0 );
00097          spring2->setDampeningConstant( 2 );
00098          spring2->setRestLength( 5 );
00099 
00100          Spring* spring3 = new Spring;
00101          s.add( spring3 );
00102          spring3->setParticles( b, c );
00103          spring3->setSpringConstant( 20.0 );
00104          spring3->setDampeningConstant( 1.5 );
00105          spring3->setRestLength( 5 );
00106 
00107 
00108          spring->deref();
00109          spring2->deref();
00110          spring3->deref();
00111       }
00112       
00113       Particle* a, *b, *c;
00114       virtual void step( float timeDelta )
00115       {
00116          static int when_to_start = 0;
00117          when_to_start++;
00118          
00119          // hack for stability upon startup.
00120          if (when_to_start > 20)
00121          {
00122             const float mag = 3.0f;
00123             velocitizer += 0.75f * timeDelta;
00124             if (velocitizer > 1.0f || velocitizer <= 0)
00125                velocitizer = 0.0f;
00126             gmtl::Vec3f vel( gmtl::Math::sin( velocitizer * TWO_PI_F ) * mag, 0.0f, gmtl::Math::cos( velocitizer * TWO_PI_F ) * mag );
00127             a->setVelocity( vel );
00128          }
00129          
00130          ParticleSystem::step( timeDelta );
00131       }   
00132       float velocitizer;
00133 };

Generated on Wed Jun 12 01:54:02 2002 for Animaniac by doxygen1.2.15