![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsDynamicParticle.hpp 00003 ******************************************************************************/ 00007 /****************************************************************************** 00008 SUKITTI PUNAK (11/24/2008) 00009 UPDATE (08/06/2010) 00010 ******************************************************************************/ 00011 #include "TAPsDynamicParticle.hpp" 00012 // Using Inclusion Model (i.e. definitions are included in declarations) 00013 // (this name.cpp is included in name.hpp) 00014 // Each friend is defined directly inside its declaration. 00015 00016 BEGIN_NAMESPACE_TAPs 00017 //============================================================================= 00018 // Constructors and Destructor 00019 //----------------------------------------------------------------------------- 00020 template <typename T> 00021 DynamicParticle<T>::DynamicParticle ( bool bRandomValue ) 00022 : Particle<T>() 00023 { bRandomValue ? RandomValues() : DefaultValues(); } 00024 //----------------------------------------------------------------------------- 00025 template <typename T> 00026 DynamicParticle<T>::DynamicParticle 00027 ( 00028 T mass, 00029 Vector3<T> const & vPosition, 00030 Vector3<T> const & vVelocity, 00031 Vector3<T> const & vAcceleration, 00032 Vector3<T> const & vForce, 00033 bool fixed 00034 ) : Particle<T>( mass, vPosition, vVelocity, vAcceleration, vForce, fixed ) 00035 { 00036 // Size 00037 m_tStartSize = 1; 00038 m_tCurrentSize = 1; 00039 m_tEndSize = 0; 00040 // Color 00041 m_v4StartColor.SetXYZW( 1, 1, 1, 1 ); 00042 m_v4CurrentColor.SetXYZW( 1, 1, 1, 1 ); 00043 m_v4EndColor.SetXYZW( 0, 0, 0, 0 ); 00044 // Life Time 00045 m_tLifeTimeElapsed = 0; 00046 m_tLifeTimeSpan = 60; 00047 } 00048 //----------------------------------------------------------------------------- 00049 template <typename T> 00050 DynamicParticle<T>::DynamicParticle ( DynamicParticle<T> const & P ) 00051 : Particle<T>( P ), 00052 // Size 00053 m_tStartSize( P.m_tStartSize ), 00054 m_tCurrentSize( P.m_tCurrentSize ), 00055 m_tEndSize( P.m_tEndSize ), 00056 // Color 00057 m_v4StartColor( P.m_v4StartColor ), 00058 m_v4CurrentColor( P.m_v4CurrentColor ), 00059 m_v4EndColor( P.m_v4EndColor ), 00060 // Life Time 00061 m_tLifeTimeElapsed( P.m_tLifeTimeElapsed ), 00062 m_tLifeTimeSpan( P.m_tLifeTimeSpan ) 00063 {} 00064 //----------------------------------------------------------------------------- 00065 // DynamicParticle Destructor 00066 template <typename T> 00067 DynamicParticle<T>::~DynamicParticle () 00068 {} 00069 //----------------------------------------------------------------------------- 00070 //============================================================================= 00071 // DynamicParticle Assignment Operator 00072 //----------------------------------------------------------------------------- 00073 template <typename T> 00074 DynamicParticle<T> const & DynamicParticle<T>::operator= ( DynamicParticle<T> const & P ) 00075 { 00076 if ( this != &P ) 00077 { 00078 *(dynamic_cast< Particle<T> * >( this )) = *(dynamic_cast< Particle<T> const * >( &P )); 00079 // Size 00080 m_tStartSize = P.m_tStartSize; 00081 m_tCurrentSize = P.m_tCurrentSize; 00082 m_tEndSize = P.m_tEndSize; 00083 // Color 00084 m_v4StartColor = P.m_v4StartColor; 00085 m_v4CurrentColor = P.m_v4CurrentColor; 00086 m_v4EndColor = P.m_v4EndColor; 00087 // Life Time 00088 m_tLifeTimeElapsed = P.m_tLifeTimeElapsed; 00089 m_tLifeTimeSpan = P.m_tLifeTimeSpan; 00090 } 00091 return *this; 00092 } 00093 //----------------------------------------------------------------------------- 00094 00095 // Return this object info as a string 00096 template <typename T> 00097 std::string DynamicParticle<T>::StrInfo () const 00098 { 00099 std::stringstream ss; 00100 ss << "DynamicParticle<" << typeid(T).name() << "> ==> " 00101 << "\n Mass: " << GetMass() 00102 << "\n Normal: " << GetNormal() 00103 << "\n Position: " << GetPosition() 00104 << "\n Velocity: " << GetVelocity() 00105 << "\n Acceleration: " << GetAcceleration() 00106 << "\n Force: " << GetForce() 00107 << "\n Size( start, current, end ): " << m_tStartSize << ", " << m_tCurrentSize << ", " << m_tEndSize 00108 << "\n Fix Status: " << ( GetFixStatus() == true ? "true" : "false" ) 00109 << "\n"; 00110 return ss.str(); 00111 } 00112 00113 //============================================================================= 00114 // Get/Set Functions 00115 //----------------------------------------------------------------------------- 00116 template <typename T> 00117 inline T DynamicParticle<T>::GetStartSize () const 00118 { return m_tStartSize; } 00119 //----------------------------------------------------------------------------- 00120 template <typename T> 00121 inline void DynamicParticle<T>::SetStartSize ( T t ) 00122 { m_tStartSize = t; } 00123 //----------------------------------------------------------------------------- 00124 template <typename T> 00125 inline T DynamicParticle<T>::GetCurrentSize () const 00126 { return m_tCurrentSize; } 00127 //----------------------------------------------------------------------------- 00128 template <typename T> 00129 inline void DynamicParticle<T>::SetCurrentSize ( T t ) 00130 { m_tCurrentSize = t; } 00131 //----------------------------------------------------------------------------- 00132 template <typename T> 00133 inline T DynamicParticle<T>::GetEndSize () const 00134 { return m_tEndSize; } 00135 //----------------------------------------------------------------------------- 00136 template <typename T> 00137 inline void DynamicParticle<T>::SetEndSize ( T t ) 00138 { m_tEndSize = t; } 00139 //----------------------------------------------------------------------------- 00140 template <typename T> 00141 inline Vector4<T> const & DynamicParticle<T>::GetStartColor () const 00142 { return m_v4StartColor; } 00143 //----------------------------------------------------------------------------- 00144 template <typename T> 00145 inline void DynamicParticle<T>::SetStartColor ( Vector4<T> const & v4 ) 00146 { m_v4StartColor = v3; } 00147 //----------------------------------------------------------------------------- 00148 template <typename T> 00149 inline void DynamicParticle<T>::SetStartColor ( T r, T g, T b, T a ) 00150 { m_v4StartColor.SetXYZW( r, g, b, a ); } 00151 //----------------------------------------------------------------------------- 00152 template <typename T> 00153 inline T DynamicParticle<T>::GetLifeTimeElapsed () const 00154 { return m_tLifeTimeElapsed; } 00155 //----------------------------------------------------------------------------- 00156 template <typename T> 00157 inline T DynamicParticle<T>::GetLifeTimeElapsedInUnit () const 00158 { return m_tLifeTimeElapsed / (m_tLifeTimeSpan + std::numeric_limits<T>::min()); } 00159 //----------------------------------------------------------------------------- 00160 template <typename T> 00161 inline void DynamicParticle<T>::SetLifeTimeElapsed ( T t ) 00162 { m_tLifeTimeElapsed = t; } 00163 //----------------------------------------------------------------------------- 00164 template <typename T> 00165 inline T DynamicParticle<T>::GetLifeTimeSpan () const 00166 { return m_tLifeTimeSpan; } 00167 //----------------------------------------------------------------------------- 00168 template <typename T> 00169 inline void DynamicParticle<T>::SetLifeTimeSpan ( T t ) 00170 { m_tLifeTimeSpan = t; } 00171 //----------------------------------------------------------------------------- 00172 template <typename T> 00173 inline bool DynamicParticle<T>::IsDead () const 00174 { return m_tLifeTimeElapsed >= m_tLifeTimeSpan ; } 00175 //----------------------------------------------------------------------------- 00176 00177 //============================================================================= 00178 // Simulation Functions 00179 //----------------------------------------------------------------------------- 00180 template <typename T> 00181 bool DynamicParticle<T>::IsDead() 00182 { return m_tLifeTimeElapsed > m_tLifeTimeSpan; } 00183 //----------------------------------------------------------------------------- 00184 template <typename T> 00185 void DynamicParticle<T>::Update ( 00186 T worldTimeDuration, 00187 T tIntegrationTimeDelta, 00188 T tGravity 00189 ) 00190 { 00191 m_tLifeTimeElapsed += worldTimeDuration; 00192 // Adding 0.000001f for protecting division by zero 00193 T tComplete = m_tLifeTimeElapsed / (m_tLifeTimeSpan + std::numeric_limits<T>::min()); 00194 00195 // Update Rendering Properties 00196 m_v4CurrentColor = CGMath<T>::LinearInterpolation( m_v4StartColor, m_v4EndColor, tComplete ); 00197 m_tCurrentSize = CGMath<T>::LinearInterpolation( m_tStartSize, m_tEndSize, tComplete ); 00198 00199 // Update Physical Properties 00200 if ( !GetFixStatus() ) { 00201 RefToAcceleration()[1] += tGravity /* * tIntegrationTimeDelta * tIntegrationTimeDelta*/ / m_tMass; 00202 RefToVelocity() += RefToAcceleration() * tIntegrationTimeDelta; 00203 RefToPosition() += RefToVelocity() * tIntegrationTimeDelta; 00204 } 00205 } 00206 //----------------------------------------------------------------------------- 00207 00208 //============================================================================= 00209 // Helper Functions 00210 //----------------------------------------------------------------------------- 00211 template <typename T> 00212 inline void DynamicParticle<T>::DefaultValues () 00213 { 00214 // Rendering ---------------------------------------------------- 00215 RefToNormal().SetXYZ( 0, 0, 1 ); 00216 // Size 00217 m_tStartSize = 1; 00218 m_tCurrentSize = 1; 00219 m_tEndSize = 0; 00220 // Color 00221 m_v4StartColor.SetXYZW( 1, 1, 1, 1 ); 00222 m_v4CurrentColor.SetXYZW( 1, 1, 1, 1 ); 00223 m_v4EndColor.SetXYZW( 0, 0, 0, 0 ); 00224 // Physics Properties ------------------------------------------- 00225 m_tMass = 1.0; 00226 RefToPosition().SetXYZ( 0, 0, 0 ); 00227 RefToVelocity().SetXYZ( 0, 0, 0 ); 00228 RefToAcceleration().SetXYZ( 0, 0, 0 ); 00229 RefToForce().SetXYZ( 0, 0, 0 ); 00230 // Constraints -------------------------------------------------- 00231 m_bFixed = false; 00232 // Life Time 00233 m_tLifeTimeElapsed = 0; 00234 m_tLifeTimeSpan = 60; 00235 } 00236 //----------------------------------------------------------------------------- 00237 template <typename T> 00238 inline void DynamicParticle<T>::RandomValues () 00239 { 00240 // Rendering ---------------------------------------------------- 00241 // between -1.0 to 1.0 00242 RefToNormal().SetXYZ( (rand()%201-100)/100.0, (rand()%201-100)/100.0, (rand()%201-100)/100.0 ); 00243 // Size 00244 // between 0.1 to 2.0 00245 m_tStartSize = (rand()%20 + 1)/10.0; 00246 m_tCurrentSize = m_tStartSize; 00247 m_tEndSize = 0; 00248 // Color 00249 // between 0.0 to 1.0 00250 m_v4StartColor.SetXYZW( (rand()%1001)/1000.0, (rand()%1001)/1000.0, (rand()%1001)/1000.0, (rand()%1001)/1000.0 ); 00251 m_v4CurrentColor = m_v4StartColor; 00252 m_v4EndColor.SetXYZW( (rand()%1001)/1000.0, (rand()%1001)/1000.0, (rand()%1001)/1000.0, (rand()%1001)/1000.0 ); 00253 // Physics Properties ------------------------------------------- 00254 // between 0.01 to 10.0 00255 m_tMass = (rand()%1000 + 1)/100.0; 00256 // between -1.0 to 1.0 00257 RefToPosition().SetXYZ( (rand()%201-100)/100.0, (rand()%201-100)/100.0, (rand()%201-100)/100.0 ); 00258 RefToVelocity().SetXYZ( (rand()%201-100)/100.0, (rand()%201-100)/100.0, (rand()%201-100)/100.0 ); 00259 RefToAcceleration().SetXYZ( (rand()%201-100)/100.0, (rand()%201-100)/100.0, (rand()%201-100)/100.0 ); 00260 RefToForce().SetXYZ( (rand()%201-100)/100.0, (rand()%201-100)/100.0, (rand()%201-100)/100.0 ); 00261 // Constraints -------------------------------------------------- 00262 m_bFixed = false; 00263 // Life Time 00264 m_tLifeTimeElapsed = 0; 00265 // between 0.01 to 120.0 00266 m_tLifeTimeSpan = (rand()%2000 + 1)/100.0; 00267 00268 //m_tCurrentSize = m_tStartSize = 10.0; 00269 //m_tLifeTimeSpan = 5.0; 00270 } 00271 //----------------------------------------------------------------------------- 00272 00273 //============================================================================= 00274 #if defined(__gl_h_) || defined(__GL_H__) 00275 template <typename T> 00276 void DynamicParticle<T>::Draw () 00277 { 00278 if ( IsDead() ) return; 00279 00280 glPushMatrix(); 00281 glPushAttrib( GL_ALL_ATTRIB_BITS ); 00282 //glTranslatef( 0, m_v3Position[1], 0 ); 00283 glTranslatef( GetPosition()[0], GetPosition()[1], GetPosition()[2] ); 00284 glEnable( GL_COLOR_MATERIAL ); 00285 glColor4fv( m_v4CurrentColor.GetDataFloat() ); 00286 glutSolidSphere( m_tCurrentSize, 10, 10 ); 00287 //glPointSize( m_tCurrentSize*100 ); 00288 //glBegin( GL_POINTS ); 00289 //glVertex3f( m_v3Position[0], m_v3Position[1], m_v3Position[2] ); 00290 // glVertex3f( 0, 0, 0 ); 00291 //glEnd(); 00292 glPopAttrib(); 00293 glPopMatrix(); 00294 } 00295 #endif // #if defined(__gl_h_) || defined(__GL_H__) 00296 //============================================================================= 00297 END_NAMESPACE_TAPs 00298 //34567890123456789012345678901234567890123456789012345678901234567890123456789 00299 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----