We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › path optimization study, clinging hairs, particles
Page Index Toggle Pages: 1
path optimization study, clinging hairs, particles (Read 1918 times)
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=#
Re: path optimization study, clinging hairs, particles
Reply #1 - May 1st, 2009, 6:28am
 
missing some code ..
Re: path optimization study, clinging hairs, particles
Reply #2 - May 1st, 2009, 8:45am
 
quite right. they're minor things, I just ran out of space i guess.

the last draw function in the class Hair can just be closed. My version had an ellipse drawn at the midpoint of each hair, And there also needs to be a new varable declared at the top of the sketch called fixedP...

Since yesterday I realised that I could maybe add some more particles along each hair's length but that still doesn't address the issue I had which was that the force of attraction that I was using to generate the "cling" was kind of erratic and no sooner had these points been pulled together they had been driven apart again. I guess it might have to do with the momentum of the initial motion...still I wonder if there is a way to manage this behaviour so the two particles come to a settled position close to one another.

//global variable
Particle fixedP;

//to be inserted in the hair class
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);
ellipse(mid.position().x(),  mid.position().y(), 5, 5);
}
}
Re: path optimization study, clinging hairs, particles
Reply #3 - May 2nd, 2009, 1:19pm
 
made some progress, still I'd like some input on how to make it work better. I believe I might have memory problems - things are consistenly breaking down but I need many more forces and springs in the final version - I am just developing this as a prototype for now. So I need help with optimization and I also need help making it behave as I want it to. Check out this site for an example, though it was done in Maya using a hair dynamic system

www.shampooo.net click on the thrid button down from the top in the second column

to see where I'm at go here:
http://upsdn.ca/processing/hairplay02d/applet/

thanks for the help in advance!
Re: path optimization study, clinging hairs, particles
Reply #4 - May 26th, 2009, 4:18pm
 
ok. I'm back at it and struggling really badly. I need help from someone who has a really good grasp on the forces in a physics system, particularly Traer's. What I'm having trouble getting a hold on is getting my particles to 'dampen' and to 'cling' to one another. Right now, they seem to pull together only to be ripped apart again.

My hunch is that I need to develop an elaborate pattern of turning forces on or off depending on distance but I don't really know. I seem to have played around with every conceivable combination of values for attraction and spring values but I can't seem to get a good feel for how to tame these things.

Really, I could use a hand. the sketch can be seen here:
http://www.upsdn.ca/processing/hairExtensions01g_3d/applet/

thanks so much!
Re: path optimization study, clinging hairs, particles
Reply #5 - May 27th, 2009, 1:35pm
 
The link you posted freezes up my browser (tried with Safari 3.2.3 on Mac OS X 10.5.7) but I don't have time to do a line-by-line walk-through of the code anyway.

From my recollection of Traer physics, the particle system works by balancing repulsive forces (like two protons repelling each other) and attractive forces (like "springs" connecting selected particles).  As I recall, by default, all of the particles in the system repel each other and only those that are explicitly connected will attract each other.  This could, indeed, cause the "ripping apart" effect you are seeing.  That's by design because it's a good model for real physical systems: the closer the particles get, the more strongly they repel.

Turning forces on and off may get you the desired effect, but a more elegant solution might be to change the equation for the force of attraction to allow "sticking" explicitly.  I don't know if Traer's physics library allows that level of tinkering, but if the source is available then you could figure out how to do it yourself.
Re: path optimization study, clinging hairs, particles
Reply #6 - May 28th, 2009, 10:19am
 
thanks so much for the reply!

and sorry bout the browser problem. I just noticed too that it doesnt work for me either on the web...I'l try and sort that out and repost but as for your suggestion about tinkering with the forces I noticed there's an update to the library that allows for new forces to be added. What would this stickiness force look like? Any quick ideas off the top of your head?

and about the default repulsion - I'm not sure about that. What I do know, is that I was referring to particles that do have attraction between them so that they are drawn together....I would assume that the attraction would override the repulsion since it's really just accumulating or summing vectors - and the greater of the two should just cancel the smaller out, no?

thanks again
Re: path optimization study, clinging hairs, particles
Reply #7 - May 29th, 2009, 11:02am
 
i'm trying to get you the applet but I'm having problems - I wonder if it's because it's an opengl 3d applet? it's not crashing my browser but nothing ever loads in the end....
Re: path optimization study, clinging hairs, particles
Reply #8 - May 29th, 2009, 12:40pm
 
How big is your example system?  If there are a large number of particles, try reducing to a smaller size and see if that works.
Re: path optimization study, clinging hairs, particles
Reply #9 - May 29th, 2009, 1:39pm
 
it turns out I was referencing a file using a non relative path so it wasn't being included in the applet export. now it is, check it out here

http://www.upsdn.ca/processing/hairExtensions01fii_3d/applet/

give it a bit of time to load too...

thanks so much!
Re: path optimization study, clinging hairs, particles
Reply #10 - May 30th, 2009, 11:33am
 
mhuyk

i uploaded a really simple demo of the problem I'm talking about.

http://www.upsdn.ca/processing/hairplay_att_experiment/applet/

MODIFICATION: note that you have to drag the fixed endpoints in such a way that the fixed particles come within a certain proximity of each other in order to see the effect I'm talking about....you can then fix them if you hold the space bar down when you release the mouse click....

note that the particles never really fully settle together but get propelled apart as soon as they come within a certain proximtiy...

thanks for the advice in advance
Re: path optimization study, clinging hairs, particles
Reply #11 - Jun 9th, 2009, 12:01pm
 
oompa_l wrote on May 28th, 2009, 10:19am:
I would assume that the attraction would override the repulsion since it's really just accumulating or summing vectors - and the greater of the two should just cancel the smaller out, no



That may be a bad assumption to make depending on how the physics simulation accumulates forces. Depending on the method it may not keep the equations balanced well - Say it sums up all the repulsion forces then sums up all the attractions, if the system's just using floats there's a good chance you'll hit the limit of what a float can store. The system will proceed to freak out and go to a state where all the floats are NAN because infinity over a large number is still infinity.

Just something to be mindful of - often physics engines have to be custom tailored or at least tweaked & values clamped so that the forces stay in a valid range for the simulation.

Jack
Re: path optimization study, clinging hairs, particles
Reply #12 - Jun 9th, 2009, 3:36pm
 
Thanks for that Jack. I haven't actually considered what the magnitude of those forces are and, moreover, that there may be limits to the size a float can hold.

I have since been in touch with Jeff Traer and he was able to suggest a number of really minor tweaks that instantly made a difference - like the Physics time step, the drag, and the attraction forces all of which I decreased and achieved much better results.

I guess I am not mindful enough of what calculations are actually going on and that you will get drastically different behaviours depending on the proportions between them - I thought I had played around with them plenty but apprently not enough.

thanks again for the advice.
Page Index Toggle Pages: 1