The attraction, repulsion, and spinning forces

Let's extend our particle's model with three new control parameters—attraction/repulsion, spinning forces inside the emitter (the force and spinning parameters), and friction that freezes the motion (the friction parameter).

Note

An example of this is 03-Particles/03-ParticlesForces.

The example is based on the 03-Particles/02-ParticlesEmitter project, implemented in the previous section. Add a declaration of the new parameters to the Params class declaration:

float force;       //Attraction/repulsion force inside emitter
float spinning;    //Spinning force inside emitter
float friction;    //Friction, in the range [0, 1]

Then add their initialization in Params::setup():

force = 0;
spinning = 0;
friction = 0;

Finally, implement these parameters by inserting the following code in the Particle::update() function after the vel.rotate(...) line:

ofPoint acc;         //Acceleration
ofPoint delta = pos - param.eCenter;
float len = delta.length();
if ( ofInRange( len, 0, param.eRad ) ) {
  delta.normalize();

  //Attraction/repulsion force 
  acc += delta * param.force;

  //Spinning force
  acc.x += -delta.y * param.spinning;
  acc.y += delta.x * param.spinning;
}
vel += acc * dt;            //Euler method
vel *= ( 1 - param.friction );  //Friction

If you run the project and notice nothing changing in the picture, it's because all the new parameters are initialized with zeros. Now try the following sets of parameters:

  • The first set is as follows:
    • In Params::setup(), add the following:
      eRad = 100;
      velRad = 0;
      lifeTime = 2.0;
      rotate = 0;
      force = 1000;
      spinning = 1000;
      friction = 0.05;
    • In testApp::setup(), add the following:
      history = 0.9;
      bornRate = 1500;
  • The second set is as follows:
    • In Params::setup(), add the following:
      eRad = 300;
      velRad = 0;
      lifeTime = 3.0;
      rotate = 500;
      force = -1000;
      spinning = 1000;
      friction = 0.05;
    • In testApp::setup(), add the following:
      history = 0.9;
      bornRate = 2500;

By running the project with these parameters, you obtain the following pictures respectively, as shown in the following screenshot:

The attraction, repulsion, and spinning forces
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset