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).
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:
Params::setup()
, add the following:eRad = 100; velRad = 0; lifeTime = 2.0; rotate = 0; force = 1000; spinning = 1000; friction = 0.05;
testApp::setup()
, add the following:history = 0.9; bornRate = 1500;
Params::setup()
, add the following:eRad = 300; velRad = 0; lifeTime = 3.0; rotate = 500; force = -1000; spinning = 1000; friction = 0.05;
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: