void Cloth::createShearLinks(float shearStiffness, float shearDamping) { numShear = (rows - 1) * (columns - 1) * 2; shearSprings = new Spring[numShear]; int index = 0; for (int i = 0; i < rows - 1; i++) { for (int j = 0; j < columns - 1; j++) { shearSprings[index++] = Spring(shearStiffness, shearDamping, &particles[(i * columns) + j], &particles[((i + 1) * columns) + j + 1]); shearSprings[index++] = Spring(shearStiffness, shearDamping, &particles[(i * columns) + j + 1], &particles[((i + 1) * columns) + j]); } } }
void Cloth::createFlexionLinks(float flexionStiffness, float flexionDamping) { numFlexion = ((rows - 2) * (columns - 2) * 2) + ((rows - 2) * 2) + ((columns - 2) * 2); flexionSprings = new Spring[numFlexion]; int index = 0; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { if (i < rows - 2 && j < columns - 2) { // horizontal spring flexionSprings[index++] = Spring(flexionStiffness, flexionDamping, &particles[(i * columns) + j], &particles[(i * columns) + j + 2]); // vertical spring flexionSprings[index++] = Spring(flexionStiffness, flexionDamping, &particles[(i * columns) + j], &particles[((i + 2) * columns) + j]); } else if (i < rows && j < columns - 2) { flexionSprings[index++] = Spring(flexionStiffness, flexionDamping, &particles[(i * columns) + j], &particles[(i * columns) + j + 2]); } else if (j < columns && i < rows - 2) { flexionSprings[index++] = Spring(flexionStiffness, flexionDamping, &particles[(i * columns) + j], &particles[((i + 2) * columns) + j]); } } } }
void Cloth::createStructuralLinks(float structuralStiffness, float structuralDamping) { numStructural = ((rows - 1) * (columns - 1) * 2) + (rows - 1) + (columns - 1); structuralSprings = new Spring[numStructural]; int index = 0; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { if (i < rows - 1 && j < columns - 1) { // horizontal spring structuralSprings[index++] = Spring(structuralStiffness, structuralDamping, &particles[(i * columns) + j], &particles[(i * columns) + j + 1]); // vertical spring structuralSprings[index++] = Spring(structuralStiffness, structuralDamping, &particles[(i * columns) + j], &particles[((i + 1) * columns) + j]); } else if (i == rows - 1 && j < columns - 1) { // only create horizontal springs for the bottom edge of the mesh structuralSprings[index++] = Spring(structuralStiffness, structuralDamping, &particles[(i * columns) + j], &particles[(i * columns) + j + 1]); } else if (j == columns - 1 && i < rows - 1) { // only create vertical springs for the right edge of the mesh structuralSprings[index++] = Spring(structuralStiffness, structuralDamping, &particles[(i * columns) + j], &particles[((i + 1) * columns) + j]); } } } }
/** * Create or load a particle system and simulate a number of time steps. */ int main(int argc, char *argv[]) { // Create particles and springs. particles.reserve(dimx * dimy * dimz); for (size_t z = 0; z < dimz; ++z) { for (size_t y = 0; y < dimy; ++y) { for (size_t x = 0; x < dimx; ++x) { Length3D p; p[0] = (x / float(dimx > 1 ? dimx - 1 : 1) - 0.5) * m - 1 * m;// - 6 * m; p[1] = (y / float(dimy > 1 ? dimy - 1 : 1) - 0.5) * 0.005 * m - 1.7 * m;// + 5 * m; p[2] = (z / float(dimz > 1 ? dimz - 1 : 1) - 0.5) * m; particles.push_back(Particle(mass, p, Velocity3D())); // create connections to all existing neighbors std::vector<size_t> other_xs, other_ys, other_zs; if (z > 0) other_zs.push_back(z - 1); if (y > 0) other_ys.push_back(y - 1); if (x > 0) other_xs.push_back(x - 1); other_zs.push_back(z); other_ys.push_back(y); other_xs.push_back(x); if (z + 1 < dimz) other_zs.push_back(z + 1); if (y + 1 < dimy) other_ys.push_back(y + 1); if (x + 1 < dimx) other_xs.push_back(x + 1); for (std::vector<size_t>::const_iterator oz = other_zs.begin(); oz != other_zs.end(); ++oz) { for (std::vector<size_t>::const_iterator oy = other_ys.begin(); oy != other_ys.end(); ++oy) { for (std::vector<size_t>::const_iterator ox = other_xs.begin(); ox != other_xs.end(); ++ox) { size_t idx = *oz * dimy * dimx + *oy * dimx + *ox; if (idx < particles.size() - 1) { springs.push_back(Spring(particles[idx], particles.back(), stiffness, springDamping, shrinkage)); } } } } } } } // Create obstacles. /*Number3D plane_normal; plane_normal[0] = 0.5; plane_normal[1] = 1.0; plane_normal[2] = 0.0; obstacles.push_back(new Plane(plane_normal / norm(plane_normal), -1.0 * m, bounciness, friction)); plane_normal[0] = 0.0; plane_normal[1] = 1.0; plane_normal[2] = 0.0; obstacles.push_back(new Plane(plane_normal / norm(plane_normal), -1.5 * m, bounciness, friction)); plane_normal[0] = -1.0; plane_normal[1] = 0.0; plane_normal[2] = 0.0; obstacles.push_back(new Plane(plane_normal / norm(plane_normal), -5.0 * m, bounciness, friction)); */ Number3D table_normal; table_normal[0] = 0.0; table_normal[1] = 1.0; table_normal[2] = 0.0; Length table_radius = 0.35 * m; Length3D table_origin; table_origin[0] = -1.0 * m; table_origin[1] = -2.0 * m; table_origin[2] = 0.0 * m; obstacles.push_back(new Table(table_origin, table_normal, table_radius, bounciness, friction)); table_origin[0] = -1.15 * m; table_origin[1] = -1.9 * m; obstacles.push_back(new Table(table_origin, table_normal, table_radius, bounciness, friction)); table_origin[0] = -0.85 * m; table_origin[1] = -2.1 * m; obstacles.push_back(new Table(table_origin, table_normal, table_radius, bounciness, friction)); // Create a particle system. particle_system = new MassSpringSystem(particles, springs, obstacles, particleDamping); // Create a solver. solver = new RungeKuttaSolver(particle_system); quadObj = gluNewQuadric(); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowPosition(0, 0); glutInitWindowSize(windowWidth, windowHeight); glutCreateWindow("Particle System"); glutIdleFunc(idle); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutMotionFunc(motion); glutKeyboardFunc(keyboard); glEnable(GL_DEPTH_TEST); glutMainLoop(); return 0; }
void JelloMesh::AddShearSpring(JelloMesh::Particle& p1, JelloMesh::Particle& p2) { double restLen = (p1.position - p2.position).Length(); m_vsprings.push_back(Spring(SHEAR, p1.index, p2.index, g_shearKs, g_shearKd, restLen)); }
void ClothSystem::setSpring(int start, int end, float springConst, float restLength) { std::vector<Spring> vec = springTies[start/2]; vec.push_back(Spring(start, end, springConst, restLength)); springTies[start/2] = vec; }
void JelloMesh::AddBendSpring(JelloMesh::Particle& p1, JelloMesh::Particle& p2) { double restLen = (p1.position - p2.position).Length(); m_vsprings.push_back(Spring(BEND, p1.index, p2.index, g_bendKs, g_bendKd, restLen)); }
void JelloMesh::AddStructuralSpring(Particle& p1, Particle& p2) { double restLen = (p1.position - p2.position).Length(); m_vsprings.push_back(Spring(STRUCTURAL, p1.index, p2.index, g_structuralKs, g_structuralKd, restLen)); }
void ParticleSystem::constructSprings( double inK, double inB, double shearK, double shearB, double flexK, double flexB ) { //set stiffest spring constant mStiffestSpring = max( inK, max(shearK, flexK) ); //useful macro for calculating indexes #define IDX(u,v) ( (u)+((v)*mWidth) ) //----- add 4-connectivity springs ---- for( idx_t y = 0; y < mHeight; y++ ) for( idx_t x = 0; x < mWidth; x++ ) { //connect spring to the right on all except for rightmost particle if( x!= mWidth-1) { idx_t a = IDX(x,y); idx_t b = IDX(x+1,y); Vector3d &p1V = mParticlePos[a]; Vector3d &p2V = mParticlePos[b]; double dist = (p2V-p1V).length(); if (dist < 0) dist *= -1; //double dist = 0.5; mSprings.push_back( Spring( a, b, dist, inK, inB, 0 ) ); } //connect springs down for all except bottom row if( y != mHeight-1 ) { idx_t a = IDX(x,y); idx_t b = IDX(x,y+1); Vector3d p1V = mParticlePos[a]; Vector3d p2V = mParticlePos[b]; double dist = (p2V-p1V).length(); if (dist < 0) dist *= -1; //double dist = 0.5; mSprings.push_back( Spring( a, b, dist, inK, inB, 0 ) ); } //Add backslashed Shear springs for all but rightmost particles and bottom row if( x!= mWidth-1 && y != mHeight-1 ) { idx_t a = IDX(x,y); idx_t b = IDX(x+1,y+1); Vector3d &p1V = mParticlePos[a]; Vector3d &p2V = mParticlePos[b]; //double dist = abs((p2V-p1V).length()); double dist = (p1V - p2V).length(); //Set rest distance as current distance mSprings.push_back( Spring( a, b, dist, shearK, shearB, 1 ) ); } //Add slashed Shear springs for all but leftmost particles and bottom row if( x != 0 && y != mHeight-1 ) { idx_t a = IDX(x,y); idx_t b = IDX(x-1,y+1); Vector3d &p1V = mParticlePos[a]; Vector3d &p2V = mParticlePos[b]; //double dist = abs((p2V-p1V).length()); double dist = (p1V - p2V).length(); //Set rest distance as current distance mSprings.push_back( Spring( a, b, dist, shearK, shearB, 1 ) ); } //Add flexion springs for all but 2 rightmost particles if( x < mWidth-2 ) { idx_t a = IDX(x,y); idx_t b = IDX(x+2,y); Vector3d &p1V = mParticlePos[a]; Vector3d &p2V = mParticlePos[b]; //double dist = abs((p2V-p1V).length()); double dist = (p1V - p2V).length(); //Set rest distance as current distance mSprings.push_back( Spring( a, b, dist, flexK, flexB, 2 ) ); } //Add flexion springs for all but 2 bottommost rows if( y < mHeight-2 ) { idx_t a = IDX(x,y); idx_t b = IDX(x,y+2); Vector3d &p1V = mParticlePos[a]; Vector3d &p2V = mParticlePos[b]; //double dist = abs((p2V-p1V).length()); double dist = (p1V - p2V).length(); //Set rest distance as current distance mSprings.push_back( Spring( a, b, dist, flexK, flexB, 2 ) ); } } #undef IDX }
//============================================================================== JuceDemoPluginAudioProcessor::JuceDemoPluginAudioProcessor() : delayBuffer (2, 12000) { // Set up our parameters. The base class will delete them for us. addParameter (offset = new FloatParameter (defaultGain, "Gain")); addParameter (springConstant = new FloatParameter (defaultDelay, "Delay")); addParameter (velocityFactor = new FloatParameter (defaultDelay, "Velocity")); lastUIWidth = 1000; lastUIHeight = 400; lastPosInfo.resetToDefault(); delayPosition = 0; // Initialise the synth... for (int i = 4; --i >= 0;) synth.addVoice (new SineWaveVoice()); // These voices will play our custom sine-wave sounds.. synth.addSound (new SineWaveSound()); const int n = m_particleCount; for(int i = 0; i < 2; i++) { m_particles.push_back(std::deque<Particle>()); m_springs.push_back(std::deque<Spring>()); std::deque<Particle>& particles = m_particles.back(); std::deque<Spring>& springs = m_springs.back(); // particles.resize(n); // springs.resize(n*2); Particle *p = &particles.back(); Particle *pprev = &particles.back(); particles.push_back(Particle(Vector3D(0.0, 0.0, 0.0))); Particle* fixedParticle = &particles.back(); m_fixedParticle.push_back(fixedParticle); particles.push_back(Particle(Vector3D(1.0, 0.0, 0.0))); Particle* outputParticle = &particles.back(); m_outputParticle.push_back(outputParticle); pprev = fixedParticle; p = outputParticle; springs.push_back(Spring(pprev, p, 1.0, 1.0)); pprev = p; for(int j = 2; j < n; j++) { particles.push_back(Particle(Vector3D(j, 0.0, 0.0))); p = &particles.back(); springs.push_back(Spring(pprev, p, 1.0, 1.0)); pprev = p; } particles.push_back(Particle(Vector3D(n, 0.0, 0.0))); Particle* pn = &particles.back(); Particle* inputParticle = pn; m_inputParticle.push_back(inputParticle); springs.push_back(Spring(pprev, pn, 1.0, 1.0)); // Spring &sn = springs.back(); } }
void Corpse::ApplyConstraint(float dt) { SPADES_MARK_FUNCTION(); AngularMomentum(0, Torso1, Torso2); AngularMomentum(1, Torso2, Torso3); AngularMomentum(2, Torso3, Torso4); AngularMomentum(3, Torso4, Torso1); AngularMomentum(4, Torso1, Arm1); AngularMomentum(5, Torso2, Arm2); AngularMomentum(6, Torso3, Leg1); AngularMomentum(7, Torso4, Leg2); Spring(Torso1, Torso2, 0.8f, dt); Spring(Torso3, Torso4, 0.8f, dt); Spring(Torso1, Torso4, 0.9f, dt); Spring(Torso2, Torso3, 0.9f, dt); Spring(Torso1, Torso3, 1.204f, dt); Spring(Torso2, Torso4, 1.204f, dt); Spring(Arm1, Torso1, 1.f, dt); Spring(Arm2, Torso2, 1.f, dt); Spring(Leg1, Torso3, 1.f, dt); Spring(Leg2, Torso4, 1.f, dt); AngleSpring(Torso1, Arm1, Torso3, -1.f, 0.6f, dt); AngleSpring(Torso2, Arm2, Torso4, -1.f, 0.6f, dt); AngleSpring(Torso3, Leg1, Torso2, -1.f, -0.2f, dt); AngleSpring(Torso4, Leg2, Torso1, -1.f, -0.2f, dt); Spring(Torso1, Torso2, Head, .6f, dt); /* AngleSpring(Torso1, Torso2, Head, 0.5f, 1.f, dt); AngleSpring(Torso2, Torso1, Head, 0.5f, 1.f, dt); */ LineCollision(Torso1, Torso2, dt); LineCollision(Torso2, Torso3, dt); LineCollision(Torso3, Torso4, dt); LineCollision(Torso4, Torso1, dt); LineCollision(Torso1, Torso3, dt); LineCollision(Torso2, Torso4, dt); LineCollision(Torso1, Arm1, dt); LineCollision(Torso2, Arm2, dt); LineCollision(Torso3, Leg1, dt); LineCollision(Torso4, Leg2, dt); return; AngleSpring(Torso4, Torso1, Head, 0.5f, 1.f, dt); AngleSpring(Torso3, Torso2, Head, 0.5f, 1.f, dt); AngleSpring(Torso4, Torso2, Head, 0.5f, 1.f, dt); AngleSpring(Torso3, Torso1, Head, 0.5f, 1.f, dt); }