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

Rope.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 "Rope.h" 00013 #include <OpenCAL/PointMass.h> 00014 #include <OpenCAL/Spring.h> 00015 #include <OpenCAL/System.h> 00016 #include <OpenCAL/Renderer.h> 00017 using namespace OpenCAL; 00018 00019 #include <OpenCAL/Vector3.h> 00020 #include <OpenCAL/Material.h> 00021 using namespace Utils; 00022 00023 using namespace std; 00024 00025 00026 /****************************** 00027 * Constructors and destructor * 00028 ******************************/ 00029 00030 Rope::Rope(System *parent, const Vector3 &from, const Vector3 &to) 00031 : MassSpringSystem(parent) 00032 { 00033 constructRope(from, to); 00034 } 00035 00036 Rope::~Rope() 00037 { 00038 } 00039 00040 00041 /************************ 00042 * Get and set functions * 00043 ************************/ 00044 00045 00046 /****************** 00047 * Other functions * 00048 ******************/ 00049 00050 void Rope::render() 00051 { 00052 if(m_parent->numObjects() == 0) return; 00053 00054 Renderer *renderer = m_parent->getRenderer(); 00055 if(!renderer) return; 00056 00057 for(unsigned int i = 1; i < m_parent->numObjects(); ++i) 00058 renderer->renderLine(getPoint(i - 1)->getPosition(), getPoint(i)->getPosition()); 00059 } 00060 00061 /* 00062 #ifdef USE_OPENGL 00063 void Rope::drawGL() 00064 { 00065 MassSpringSystem::drawGL(); 00066 00067 return; // TEMP 00069 00070 //if(!m_draw) return; 00071 00072 Utils::Material mat; 00073 mat.setAmbient(Color::red); 00074 mat.setAmbient(Color::red); 00075 mat.applyGL(); 00076 00077 // Draw the rope 00078 glBegin(GL_LINE_STRIP); 00079 for(unsigned int i = 0; i < m_parent->numObjects(); ++i) 00080 glVertex3fv(getPoint(i)->getPosition()); 00081 glEnd(); 00082 } 00083 #endif // USE_OPENGL 00084 */ 00085 00086 00087 /********************** 00088 * Protected functions * 00089 **********************/ 00090 00091 void Rope::constructRope(const Vector3 &from, const Vector3 &to) 00092 { 00093 // For now, we use the same parameters as in the nehe.gamedev.net rope tutorial 00094 const int precision = 20; // Particles per unit length 00095 const float stiffness = 100.0f; 00096 const float damping = 0.2f; 00097 const float mass = 1.0f; // Mass per unit length 00098 00099 float distance = from.distance(to); 00100 int particles = (int) (distance * precision); 00101 float massEach = distance * mass / (float) particles; 00102 00103 PointMass *prevMass = new PointMass(this, massEach); 00104 //prevMass->disableDraw(); 00105 prevMass->setPosition(from); 00106 //prevMass->setInfiniteMass(); 00107 00108 for(int i = 1; i < particles; ++i) 00109 { 00110 PointMass *mass = new PointMass(this, massEach); 00111 //mass->disableDraw(); 00112 mass->setPosition(from.blended((float) (i + 1) / (float) particles, to)); 00113 //if(i == (particles - 1)) 00114 // mass->setInfiniteMass(); 00115 00116 Spring *spring = new Spring(this, prevMass, mass); 00117 spring->setStiffness(stiffness); 00118 spring->setDamping(damping); 00119 spring->setDraw(true); 00120 00121 prevMass = mass; 00122 } 00123 }

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