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

RigidBody.cpp

00001 /*************************************************************************** 00002 * This file is part of OpenCAL: Open Computer Animation Library * 00003 * I created OpenCAL as my master's thesis Computer Science (multimedia) * 00004 * at the tUL university in Diepenbeek, Belgium * 00005 * * 00006 * Copyright (C) 2003-2004 by Jeroen Dierckx * 00007 * jeroen.dierckx@student.luc.ac.be * 00008 * * 00009 ***************************************************************************/ 00010 00011 // Includes 00012 #include "RigidBody.h" 00013 #include <OpenCAL/PhaseSpace.h> 00014 using namespace OpenCAL; 00015 00016 using namespace std; 00017 00018 00019 /****************************** 00020 * Constructors and destructor * 00021 ******************************/ 00022 00023 RigidBody::RigidBody(System *parent, float mass) 00024 : PhysicalObject(parent, mass), Rigid() 00025 { 00026 #ifdef VERBOSE 00027 Debug::print("RigidBody constructor", 2); 00028 #endif // VERBOSE 00029 //cout << "mass = " << mass << endl; 00030 } 00031 00032 RigidBody::~RigidBody() 00033 { 00034 #ifdef VERBOSE 00035 Debug::print("RigidBody destructor", 2); 00036 #endif // VERBOSE 00037 } 00038 00039 00040 /****************** 00041 * Force functions * 00042 ******************/ 00043 00044 void RigidBody::addForce(const Vector3 &force, const Vector3 &position) 00045 { 00046 m_force += force; 00047 //m_torque += (position - m_position).crossProduct(force); 00048 m_torque += position.crossProduct(force); 00049 } 00050 00051 00052 /****************** 00053 * Other functions * 00054 ******************/ 00055 00056 Vector3 RigidBody::getAngularVelocity() const 00057 { 00058 Matrix33 rot = m_rotation.toMatrix33(); // Rotation matrix 00059 Matrix33 invInertia = rot * m_invInertia * rot.transposed(); // Inversed inertia tensor (in world coördinates) 00060 return invInertia * m_angularMomentum; 00061 } 00062 00063 void RigidBody::setAngularVelocity(const Vector3 &velocity) 00064 { 00066 00067 cout << "InvInertia:" << endl; 00068 m_invInertia.print(); 00069 00070 Matrix33 inertia = m_invInertia; 00071 inertia.set(0,0, 1.0f / inertia.get(0,0)); 00072 inertia.set(1,1, 1.0f / inertia.get(1,1)); 00073 inertia.set(2,2, 1.0f / inertia.get(2,2)); 00074 cout << "Inertia:" << endl; 00075 inertia.print(); 00076 00077 Matrix33 rot = m_rotation.toMatrix33(); // Rotation matrix 00078 inertia = rot * inertia * rot.transposed(); // Inertia tensor (in world coördinates) 00079 cout << "Inertia world:" << endl; 00080 inertia.print(); 00081 00082 m_angularMomentum = inertia * velocity; 00083 } 00084 00085 void RigidBody::initialize() 00086 { 00087 /* 00088 #ifdef USE_OPENGL 00089 initializeGL(); 00090 #endif // USE_OPENGL 00091 */ 00092 00093 PhysicalObject::initialize(); 00094 00095 cout << "Initializing rigid body!" << endl; 00096 00097 calculateVolume(); 00098 calculateInversedInertia(); 00099 } 00100 00101 void RigidBody::addToState(PhaseSpace *state) 00102 { 00103 state->concat(m_position); // Position (of center of mass) 00104 state->concat(m_rotation); // Rotation 00105 state->concat(m_velocity); // Linear velocity 00106 state->concat(m_angularMomentum); // Angular momentum 00107 } 00108 00109 void RigidBody::addToDerivative(PhaseSpace *deriv) 00110 { 00111 if(isFixed()) 00112 deriv->concatZeros(13); 00113 else 00114 { 00115 // Calculate auxilary variables 00116 //m_rotation.normalize(); 00118 Matrix33 rot = m_rotation.toMatrix33(); // Rotation matrix 00119 Matrix33 invInertia = rot * m_invInertia * rot.transposed(); // Inversed inertia tensor (in world coördinates) 00120 Vector3 omega = invInertia * m_angularMomentum; 00121 Quaternion qdot = 0.5f * (omega * m_rotation); 00122 //qdot.normalize(); //! @warning this gives very strange results! 00123 00124 // Fill in the state vector 00125 deriv->concat(m_velocity); // Linear velocity 00126 deriv->concat(qdot); // Rotation derived to time 00127 deriv->concat(getAccelleration()); // Linear acceleration 00128 deriv->concat(m_torque); // Torque 00129 00130 /* 00131 cout << "Rotation: " << m_rotation << endl; 00132 cout << "Rotation matrix: " << endl; 00133 rot.print(); 00134 cout << "Inversed inertia tensor (body co): " << endl; 00135 m_invInertia.print(); 00136 cout << "Inversed inertia tensor (world co): " << endl; 00137 invInertia.print(); 00138 cout << "Omega: " << omega << endl; 00139 cout << endl; 00140 cout << "Linear velocity: " << m_velocity << endl; 00141 cout << "Qdot: " << qdot << endl; 00142 cout << "Linear acceleration: " << getAccelleration() << endl; 00143 cout << "Torque: " << m_torque << endl; 00144 */ 00145 } 00146 } 00147 00148 void RigidBody::fromState(PhaseSpace *state) 00149 { 00150 if(isFixed()) 00151 state->skip(13); 00152 else 00153 { 00154 m_position.setX(state->getNext()); 00155 m_position.setY(state->getNext()); 00156 m_position.setZ(state->getNext()); 00157 00158 m_rotation.setScalar(state->getNext()); 00159 m_rotation.setVector(Vector3(state->getNext(), state->getNext(), state->getNext())); 00160 00161 m_velocity.setX(state->getNext()); 00162 m_velocity.setY(state->getNext()); 00163 m_velocity.setZ(state->getNext()); 00164 00165 m_angularMomentum.setX(state->getNext()); 00166 m_angularMomentum.setY(state->getNext()); 00167 m_angularMomentum.setZ(state->getNext()); 00168 } 00169 00171 m_force = Vector3::zero; 00172 m_torque = Vector3::zero; 00173 } 00174 00175 void RigidBody::disable() 00176 { 00177 m_force = m_torque = Vector3::zero; 00178 m_velocity = m_angularMomentum = Vector3::zero; 00179 m_enabled = false; 00180 } 00181 00182 /* 00183 #ifdef USE_OPENGL 00184 void RigidBody::drawGL() 00185 { 00186 #ifdef VERBOSE 00187 Debug::print("RigidBody drawGL", Debug::maxLevel); 00188 #endif // VERBOSE 00189 00190 //if(!m_draw) return; 00191 00192 glPushMatrix(); 00193 00194 translateGL(); 00195 rotateGL(); 00196 renderGL(); 00197 00198 glPopMatrix(); 00199 } 00200 #endif // USE_OPENGL 00201 */ 00202 00203 00204 /********************** 00205 * Protected functions * 00206 **********************/

Generated on Sun Aug 15 19:19:23 2004 for OpenCAL: Open Computer Animation Library by doxygen 1.3.8