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

MassSpringSystem.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 "MassSpringSystem.h" 00013 #include <OpenCAL/PointMass.h> 00014 #include <OpenCAL/Spring.h> 00015 #include <OpenCAL/Renderer.h> 00016 #include <OpenCAL/Picker.h> 00017 using namespace OpenCAL; 00018 00019 #include <OpenCAL/Vector3.h> 00020 using namespace OpenCAL::Utils; 00021 00022 #include <map> 00023 using namespace std; 00024 00025 00026 /****************************** 00027 * Constructors and destructor * 00028 ******************************/ 00029 00030 MassSpringSystem::MassSpringSystem(System *parent) 00031 : PhysicsSystem(parent) 00032 { 00033 m_maxStepSize = 0.005f; 00034 } 00035 00036 00037 MassSpringSystem::~MassSpringSystem() 00038 { 00039 } 00040 00041 00042 /************************ 00043 * Get and set functions * 00044 ************************/ 00045 00046 void MassSpringSystem::setMass(float mass) 00047 { 00048 if(m_objects.empty()) return; 00049 00050 float massEach = mass / m_objects.size(); 00051 for(unsigned int i = 0; i < m_objects.size(); ++i) 00052 ((PointMass *) m_objects[i])->setMass(massEach); 00053 } 00054 00055 void MassSpringSystem::setMassEach(float mass) 00056 { 00057 for(unsigned int i = 0; i < m_objects.size(); ++i) 00058 ((PointMass *) m_objects[i])->setMass(mass); 00059 } 00060 00061 void MassSpringSystem::setStiffness(float stiffness) 00062 { 00063 for(unsigned int i = 0; i < m_actors.size(); ++i) 00064 ((Spring *) m_actors[i])->setStiffness(stiffness); 00065 } 00066 00067 void MassSpringSystem::setDamping(float damping) 00068 { 00069 for(unsigned int i = 0; i < m_actors.size(); ++i) 00070 ((Spring *) m_actors[i])->setDamping(damping); 00071 } 00072 00073 00074 /****************** 00075 * Other functions * 00076 ******************/ 00077 00078 void MassSpringSystem::step(float deltaSeconds) 00079 { 00080 //if(!m_enabled) return; 00081 00082 // Don't use PhysicsSystem::step, cause we use a special integrator here. 00083 //PhysicsSystem::step(deltaSeconds); 00084 00085 unsigned int steps = (unsigned int) (deltaSeconds / m_maxStepSize) + 1; 00086 deltaSeconds /= steps; 00087 00088 for(unsigned int i = 0; i < steps; ++i) 00089 { 00090 System::step(deltaSeconds); 00091 integrate(deltaSeconds); 00092 } 00093 } 00094 00095 void MassSpringSystem::render() 00096 { 00097 Renderer *renderer = m_parent->getRenderer(); 00098 if(!renderer) return; 00099 00100 m_surface.invalidateTriangleNormals(); 00101 m_surface.calculateVertexNormals(); 00102 renderer->renderMesh(&m_surface); 00103 } 00104 00105 void MassSpringSystem::renderPicking() 00106 { 00107 Renderer *renderer = m_parent->getRenderer(); 00108 if(!renderer) return; 00109 00110 Picker *picker = m_parent->getPicker(); 00111 if(!picker) return; 00112 00113 picker->pushName(0); 00114 00115 PointMass *point; 00116 for(unsigned int i = 0; i < m_objects.size(); ++i) 00117 { 00118 point = getPoint(i); 00119 picker->loadName(i); 00120 renderer->renderPoint(point->getPosition(), 3.0f); 00121 } 00122 00123 picker->popName(); 00124 } 00125 00126 /* 00127 #ifdef USE_OPENGL 00128 void MassSpringSystem::drawPickGL() 00129 { 00130 map<PointMass *, bool> drawMap; 00131 00132 PointMass *point; 00133 for(unsigned int i = 0; i < m_objects.size(); ++i) 00134 { 00135 point = getPoint(i); 00136 drawMap[point] = point->getDraw(); 00137 point->enableDraw(); 00138 } 00139 00140 PhysicsSystem::drawPickGL(); 00141 00142 map<PointMass *, bool>::iterator it; 00143 for(it = drawMap.begin(); it != drawMap.end(); ++it) 00144 it->first->setDraw(it->second); 00145 } 00146 #endif // USE_OPENGL 00147 */ 00148 00149 void MassSpringSystem::initialize() 00150 { 00151 PhysicsSystem::initialize(); 00152 00153 Renderer *renderer = m_parent->getRenderer(); 00154 if(!renderer) return; 00155 00156 renderer->initializeMesh(&m_surface); 00157 } 00158 00159 00160 /********************** 00161 * Protected functions * 00162 **********************/ 00163 00172 void MassSpringSystem::integrate(float deltaSeconds) 00173 { 00174 for(unsigned int i = 0; i < m_objects.size(); ++i) 00175 { 00176 PointMass *point = getPoint(i); 00177 Vector3 &pos = *(point->getPositionP()); 00178 Vector3 &vel = *(point->getVelocityP()); 00179 Vector3 acc = point->getAccelleration(); 00180 00181 if(!point->isFixed()) 00182 { 00183 vel += deltaSeconds * acc; 00184 pos += deltaSeconds * vel; 00185 } 00186 point->resetForce(); 00187 } 00188 }

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