oompa_l
Full Member
Offline
Posts: 212
path optimization study, clinging hairs, particles
Apr 30th , 2009, 4:39pm
Hi I am trying to simulate the behaviour of hairs clinging to each other...but I need some advice. I have a first stab but it's not quite working - I think the issue is with the particle system, but I am open to the possibility I am thinking about the whole system all wrong... I appreciate any advice [quote]import traer.animation.*;import traer.physics.*; ParticleSystem ps;ArrayList fixed = new ArrayList ();//, free; ArrayList hairs = new ArrayList ();void setup () { size (1026,830); ps = new ParticleSystem( 0, 0.25 ); for (int i=0; i<5; i++){ Particle tempP = ps.makeParticle(1, random (0,width ),random (0,height ), 0); tempP.makeFixed(); fixed.add (tempP); } for (int i = 0; i < fixed.size (); i++) { Particle p1 = (Particle)fixed.get (i); for (int j = i+1; j < fixed.size (); j++) { Particle p2 = (Particle)fixed.get (j); PVector pPosA = new PVector (p1.position().x(), p1.position().y()); PVector pPosB = new PVector (p2.position().x(), p2.position().y()); float tempDist = PVector .dist (pPosA, pPosB); if (tempDist>400){ Hair tempStrand = new Hair(ps.getParticle(i), ps.getParticle(j)); hairs.add (tempStrand); } } } for (int i = 0; i < hairs.size (); i++) { Hair h1 = (Hair)hairs.get (i); Particle tempA = h1.mid; for (int j = i+1; j < hairs.size (); j++) { Hair h2 = (Hair)hairs.get (j); Particle tempB = h2.mid; Attraction tempAtt = ps.makeAttraction(tempA, tempB, 10000,15); } } smooth (); }void draw () { background (0); ps.tick(); if (mousePressed ) { fixedP.moveTo(mouseX , mouseY , 0); fixedP.velocity().clear(); } strokeWeight (4); stroke (100); for (int i = 0; i < fixed.size (); i++) { Particle p = (Particle)fixed.get (i); ellipse (p.position().x(),p.position().y(), 10, 10); } strokeWeight (0.5); stroke (100); noFill (); for (int i = 0; i < hairs.size (); i++) { Hair h = (Hair)hairs.get (i); h.draw (); } }void mousePressed () { int mx = mouseX ; int my = mouseY ; float d = -1.0; PVector mousePos = new PVector (mx, my, 0); for (int i=0; i<ps.numberOfParticles(); i++) { Particle pTemp = ps.getParticle(i); PVector particlePos = new PVector (pTemp.position().x(),pTemp.position().y() ); float dTemp = PVector .dist (particlePos, mousePos); if (dTemp < d || d < 0) { d = dTemp; fixedP = pTemp; } } }void mouseReleased () { if (keyPressed ) { if (key == ' ' ) { fixedP.makeFixed(); } } else { fixedP.makeFree(); } fixedP = null ; } [quote]class Hair{ Particle a; Particle b; Particle mid; Hair(Particle a, Particle b){ this .a = a; this .b = b; mid = ps.makeParticle(1, (a.position().x()+b.position().x())/2, (a.position( ).y()+b.position().y())/2, 0); Spring tempSpringA = ps.makeSpring(a, mid,1,1,100); Spring tempSpringB = ps.makeSpring(b, mid,1,1,100); } void draw (){ noFill (); beginShape (); stroke (100); curveVertex (a.position().x(), a.position().y()); curveVertex (a.position().x(), a.position().y()); curveVertex (mid.position().x(), mid.position().y()); curveVertex (b.position().x(), b.position().y()); curveVertex (b.position().x(), b.position().y()); endShape (); fill (255); [quote] [color=#