![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsPointMassDynamic.cpp 00003 00004 PointMassDynamic class is a class for dynamic particle in 3D. 00005 00006 SUKITTI PUNAK (07/12/2009) 00007 UPDATE (09/21/2010) 00008 ******************************************************************************/ 00009 #include "TAPsPointMassDynamic.hpp" 00010 // Using Inclusion Model (i.e. definitions are included in declarations) 00011 // (this name.cpp is included in name.hpp) 00012 // Each friend is defined directly inside its declaration. 00013 00014 BEGIN_NAMESPACE_TAPs 00015 //============================================================================= 00016 // Constructors and Destructor 00017 //----------------------------------------------------------------------------- 00018 template <typename T> 00019 PointMassDynamic<T>::PointMassDynamic ( bool bRandomValue ) 00020 { 00021 bRandomValue ? RandomValues() : DefaultValues(); 00022 m_Type = TYPE_DYNAMIC; 00023 } 00024 //----------------------------------------------------------------------------- 00025 template <typename T> 00026 PointMassDynamic<T>::PointMassDynamic 00027 ( 00028 T mass, 00029 T size, 00030 Vector3<T> const & vPosition, 00031 Vector3<T> const & vVelocity, 00032 Vector3<T> const & vAcceleration, 00033 Vector3<T> const & vForce, 00034 DS::SimulationFlags const & flags 00035 ) : PointMass( mass, size, vPosition, flags ) 00036 , m_v3Velocity( vVelocity ) 00037 , m_v3Acceleration( vAcceleration ) 00038 , m_v3Force( vForce ) 00039 { 00040 m_Type = TYPE_DYNAMIC; 00041 } 00042 //----------------------------------------------------------------------------- 00043 template <typename T> 00044 PointMassDynamic<T>::PointMassDynamic ( PointMassDynamic<T> const & P ) 00045 : PointMass( P ) 00046 , m_v3Velocity( P.m_v3Velocity ) 00047 , m_v3Acceleration( P.m_v3Acceleration ) 00048 , m_v3Force( P.m_v3Force ) 00049 { 00050 m_Type = TYPE_DYNAMIC; 00051 } 00052 //----------------------------------------------------------------------------- 00053 template <typename T> 00054 PointMassDynamic<T>::PointMassDynamic ( Vector3<T> const & location ) 00055 : PointMass( location ) 00056 , m_v3Velocity() 00057 , m_v3Acceleration() 00058 , m_v3Force() 00059 { 00060 m_Type = TYPE_DYNAMIC; 00061 } 00062 //----------------------------------------------------------------------------- 00063 template <typename T> 00064 PointMassDynamic<T>::PointMassDynamic ( T const location[3] ) 00065 : PointMass( location ) 00066 , m_v3Velocity() 00067 , m_v3Acceleration() 00068 , m_v3Force() 00069 { 00070 m_Type = TYPE_DYNAMIC; 00071 } 00072 //----------------------------------------------------------------------------- 00073 template <typename T> 00074 PointMassDynamic<T>::PointMassDynamic ( T locX, T locY, T locZ ) 00075 : PointMass( locX, locY, locZ ) 00076 , m_v3Velocity() 00077 , m_v3Acceleration() 00078 , m_v3Force() 00079 { 00080 m_Type = TYPE_DYNAMIC; 00081 } 00082 //----------------------------------------------------------------------------- 00083 // PointMassDynamic Destructor 00084 template <typename T> 00085 PointMassDynamic<T>::~PointMassDynamic () 00086 {} 00087 //----------------------------------------------------------------------------- 00088 template <typename T> 00089 std::string PointMassDynamic<T>::StrInfo () const 00090 { 00091 std::stringstream ss; 00092 ss << "PointMassDynamic<" << typeid(T).name() << "> ==> " 00093 << "\n Mass: " << GetMass() 00094 << "\n Position: " << GetPosition() 00095 << "\n Velocity: " << GetVelocity() 00096 << "\n Acceleration: " << GetAcceleration() 00097 << "\n Force: " << GetForce() 00098 << "\n Simulation Flags: " << GetFlags() 00099 << "\n"; 00100 return ss.str(); 00101 } 00102 //----------------------------------------------------------------------------- 00103 //============================================================================= 00104 // PointMassDynamic Assignment Operator 00105 //----------------------------------------------------------------------------- 00106 template <typename T> 00107 PointMassDynamic<T> const & PointMassDynamic<T>::operator= ( PointMassDynamic<T> const & P ) 00108 { 00109 if ( this != &P ) 00110 { 00111 *(dynamic_cast< PointMass<T> * >(this)) = *(dynamic_cast< PointMass<T> const * >(&P)); 00112 // Physics Properties --------------------------------------- 00113 m_v3Velocity = P.m_v3Velocity; 00114 m_v3Acceleration = P.m_v3Acceleration; 00115 m_v3Force = P.m_v3Force; 00116 } 00117 return *this; 00118 } 00119 //----------------------------------------------------------------------------- 00120 00121 //============================================================================= 00122 // Get/Set Functions 00123 //----------------------------------------------------------------------------- 00124 00125 //------------------------------------------------------------------- 00126 // Physics Properties 00127 //------------------------------------------------------------------- 00128 00129 // Velocity 00130 template <typename T> 00131 Vector3<T> const & PointMassDynamic<T>::GetVelocity () const { return m_v3Velocity; } 00132 template <typename T> 00133 Vector3<T> & PointMassDynamic<T>::GetVelocity () { return m_v3Velocity; } 00134 template <typename T> 00135 void PointMassDynamic<T>::SetVelocity ( Vector3<T> const & v3 ) { m_v3Velocity = v3; } 00136 template <typename T> 00137 void PointMassDynamic<T>::SetVelocity ( T x, T y, T z ) { m_v3Velocity.SetXYZ( x, y, z ); } 00138 00139 // Acceleration 00140 template <typename T> 00141 Vector3<T> const & PointMassDynamic<T>::GetAcceleration () const { return m_v3Acceleration; } 00142 template <typename T> 00143 Vector3<T> & PointMassDynamic<T>::GetAcceleration () { return m_v3Acceleration; } 00144 template <typename T> 00145 void PointMassDynamic<T>::SetAcceleration ( Vector3<T> const & v3 ) { m_v3Acceleration = v3; } 00146 template <typename T> 00147 void PointMassDynamic<T>::SetAcceleration ( T x, T y, T z ) { m_v3Acceleration.SetXYZ( x, y, z ); } 00148 00149 // Force 00150 template <typename T> 00151 Vector3<T> const & PointMassDynamic<T>::GetForce () const { return m_v3Force; } 00152 template <typename T> 00153 Vector3<T> & PointMassDynamic<T>::GetForce () { return m_v3Force; } 00154 template <typename T> 00155 void PointMassDynamic<T>::SetForce ( Vector3<T> const & v3 ) { m_v3Force = v3; } 00156 template <typename T> 00157 void PointMassDynamic<T>::SetForce ( T x, T y, T z ) { m_v3Force.SetXYZ( x, y, z ); } 00158 00159 00160 //============================================================================= 00161 // Simulation Functions 00162 //----------------------------------------------------------------------------- 00163 /* 00164 template <typename T> 00165 void PointMassDynamic<T>::Update( 00166 T worldTimeDuration, //!< duration time (sec) in the virtual world after the last update 00167 T tIntegrationTimeDelta //!< integration time (sec) 00168 ) 00169 { 00170 static T tPrecentComplete; 00171 static T tGravity = -9.81E1; 00172 00173 m_tLifeTimeElapsed += worldTimeDuration; 00174 // Adding 0.000001f for protecting division by zero 00175 tPrecentComplete = m_tLifeTimeElapsed / (m_tLifeTimeSpan + 0.000001f); 00176 00177 // Update Rendering Properties 00178 m_v4CurrentColor = CGMath<T>::LinearInterpolation( m_v4StartColor, m_v4EndColor, tPrecentComplete ); 00179 m_tCurrentSize = CGMath<T>::LinearInterpolation( m_tStartSize, m_tEndSize, tPrecentComplete ); 00180 00181 // Update Physical Properties 00182 if ( !m_bFixed ) { 00183 m_v3Acceleration[1] += tGravity * worldTimeDuration * worldTimeDuration * m_tMass; 00184 m_v3Velocity += m_v3Acceleration * tIntegrationTimeDelta; 00185 m_v3Position += m_v3Velocity * tIntegrationTimeDelta; 00186 } 00187 } 00188 //*/ 00189 //----------------------------------------------------------------------------- 00190 00191 00192 //============================================================================= 00193 //----------------------------------------------------------------------------- 00194 template <typename T> 00195 void PointMassDynamic<T>::DefaultValues () 00196 { 00197 PointMass<T>::DefaultValues(); 00198 // Physics Properties ------------------------------------------- 00199 m_v3Velocity.SetXYZ( 0, 0, 0 ); 00200 m_v3Acceleration.SetXYZ( 0, 0, 0 ); 00201 m_v3Force.SetXYZ( 0, 0, 0 ); 00202 } 00203 //----------------------------------------------------------------------------- 00204 template <typename T> 00205 void PointMassDynamic<T>::RandomValues () 00206 { 00207 PointMass<T>::DefaultValues(); 00208 // Physics Properties ------------------------------------------- 00209 // between -1.0 to 1.0 00210 m_v3Velocity.SetXYZ( (rand()%201-100)/100.0, (rand()%201-100)/100.0, (rand()%201-100)/100.0 ); 00211 m_v3Acceleration.SetXYZ( (rand()%201-100)/100.0, (rand()%201-100)/100.0, (rand()%201-100)/100.0 ); 00212 m_v3Force.SetXYZ( (rand()%201-100)/100.0, (rand()%201-100)/100.0, (rand()%201-100)/100.0 ); 00213 } 00214 //----------------------------------------------------------------------------- 00215 00216 //============================================================================= 00217 #if defined(__gl_h_) || defined(__GL_H__) 00218 template <typename T> 00219 void PointMassDynamic<T>::Draw () const 00220 { 00221 glPushMatrix(); 00222 glPushAttrib( GL_ALL_ATTRIB_BITS ); 00223 glTranslatef( static_cast<float>(m_v3Position[0]), static_cast<float>(m_v3Position[1]), static_cast<float>(m_v3Position[2]) ); 00224 glEnable( GL_COLOR_MATERIAL ); 00225 glColor4f( 0.5, 0.5, 0.5, 0.5 ); 00226 glutSolidSphere( m_tSize, 10, 10 ); 00227 glPopAttrib(); 00228 glPopMatrix(); 00229 } 00230 00231 template <typename T> 00232 void PointMassDynamic<T>::Draw ( T scale ) const 00233 { 00234 glPushMatrix(); 00235 glPushAttrib( GL_ALL_ATTRIB_BITS ); 00236 glTranslatef( static_cast<float>(m_v3Position[0]), static_cast<float>(m_v3Position[1]), static_cast<float>(m_v3Position[2]) ); 00237 glEnable( GL_COLOR_MATERIAL ); 00238 glColor4f( 0.5, 0.5, 0.5, 0.5 ); 00239 glutSolidSphere( m_tSize*scale, 10, 10 ); 00240 glPopAttrib(); 00241 glPopMatrix(); 00242 } 00243 #endif // #if defined(__gl_h_) || defined(__GL_H__) 00244 //============================================================================= 00245 END_NAMESPACE_TAPs 00246 //34567890123456789012345678901234567890123456789012345678901234567890123456789 00247 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----