Automatically moving particles

Hi all!

I have a very simple question. I am working with the particles example of openframeworks. I am editing the void demoParticle::update() function and in “PARTICLE_MODE_ATTRACT” mode I want to set a random value every 5 seconds, instead of using the mouseX and mouseY position. The goal is to have automatically moving particles.

I have tried the following, but this does not work. The particles are moving to a certain point and then they won’t move any longer. The position is not updated.

Maybe someone has an idea?

Thanks & all the best
Patrick

CODE:

void demoParticle::update(){
if( mode == PARTICLE_MODE_ATTRACT ){

    int frame_num = ofGetFrameNum();
    int elapsedTime = ofGetElapsedTimef();
    
    random_one_neu = random_one;
    random_two_neu = random_two;
    
    if(elapsedTime%5 == 0){
        random_one_neu = ofRandom(1000);
        random_two_neu = ofRandom(1000);
        ofPoint attractPt(random_one_neu, random_two_neu);
    } else{
        random_one_neu = random_one;
        random_two_neu = random_two;
        ofPoint attractPt(random_one_neu, random_two_neu);
    }
    ofPoint attractPt(random_one_neu, random_two_neu);
    frc = attractPt-pos; // we get the attraction force/vector by looking at the mouse pos relative to our pos
    frc.normalize(); //by normalizing we disregard how close the particle is to the attraction point
    
    vel *= drag; //apply drag
    vel += frc * 0.6; //apply force
}

Hi

 if(elapsedTime%5 == 0)

Are you sure this test get true ? I’m not sure, try to put a breakpoint

 random_one_neu = ofRandom(1000);
 random_two_neu = ofRandom(1000);

You try to change the attraction point here, but it’s getting reset each time update() is called with this:

 random_one_neu = random_one;
 random_two_neu = random_two;

Try something like

ofPoint attractPt(ofRandom(1000), ofRandom(1000));

void demoParticle::update(){
  if( mode == PARTICLE_MODE_ATTRACT ){
    float elapsedTime = ofGetElapsedTimef();
    flot alarm = elapsedTime + 5.;
    if (elapsedTime > alarm)
    {
      alarm = elapsedTime + 5.;
      attractPt.set(ofRandom(1000), ofRandom(1000));
    }

    frc = attractPt-pos; // we get the attraction force/vector by looking at the mouse pos relative to our pos
    frc.normalize(); //by normalizing we disregard how close the particle is to the attraction point

    vel *= drag; //apply drag
    vel += frc * 0.6; //apply force
   }
}

and don’t forget to update your particle pos:

 pos += vel;
2 Likes

Thanks talaron for the quick answer! That really fixed the issue! Appreciate dude!