// Julien Pilet, Sept. 2008 #include #include "Meddle/elasticchainondefm.h" namespace Meddle { /* ctor */ ElasticChain::ElasticChain(unsigned int npts, float x, float y, Model::Animation::Handler *animation): m_npts(npts), m_first(true), m_animation(animation) { m_elastics = new t_elastic[m_npts - 1]; m_pts = new t_point[m_npts]; memset(m_pts, 0, m_npts * sizeof(t_point)); // orientation et force de la gravite gravity[0] = 0; gravity[1] = -800; float dy = 2 / m_npts; // 2/m_npts espace entre les points du spaghetti for (int i = 0; i < m_npts; i++) { m_pts[i].mass = .002; // poids des maillons intermediaires m_pts[i].pos[0] = x; m_pts[i].pos[1] = y + i*dy; } m_pts[0].flags = 10; m_pts[m_npts - 1].mass = .02; for (int i = 0;i < m_npts - 1; i++) { m_elastics[i].p1 = m_pts + i; m_elastics[i].p2 = m_pts + i + 1; m_elastics[i].factor = 2; //2 ressort plus ou moins rigide m_elastics[i].len = dy; } m_last.start(); } /* dtor */ ElasticChain::~ElasticChain() { delete[] (m_pts); delete[] (m_elastics); } void ElasticChain::draw(float x, float y) { if (m_first) { m_first = false; return; } float dtime = m_last.elapsed(); m_pts[0].pos[0] = x; m_pts[0].pos[1] = y; dtime = dtime / 32; for (int i = 0;i < 32; i++) { zero_forces(m_pts, m_npts); calc_forces(m_elastics, m_npts - 1); advance(m_pts, m_npts, dtime); } m_animation->texturize(); m_animation->texture.enable(); double width = 20; //20 LARGEUR de la chaine dessine glBegin(GL_TRIANGLES); glColor4f(1, 1, 1, 1); int ndiv = 1; int tot = (m_npts - 1) * ndiv; int k = 0; for (int i = 0;i < m_npts - 1; i++) { for (int j = 0;j < ndiv; j++, k++) { float t = j / (float)ndiv; float r[2]; float dt[2]; float nr[2], ndt[2]; splineCompute(t,2, (i>0? m_pts[i-1].pos:0), m_pts[i].pos, m_pts[i+1].pos, (i0? m_pts[i-1].pos:0), m_pts[i].pos, m_pts[i+1].pos, (itexture.disable(); m_last.restart(); } } // namespace Meddle