Adding a tail to our particles

In this recipe, we will show you how to add a tail to the particle animation.

Getting started

In this recipe we are going to use the code base from the recipe Applying repulsion and attraction forces from Chapter 5, Building Particle Systems.

How to do it…

We will add a tail to the particles using different techniques.

Drawing history

Simply replace the draw method with the following code:

void MainApp::draw()
{   
gl::enableAlphaBlending();
gl::setViewport(getWindowBounds());
gl::setMatricesWindow(getWindowWidth(), getWindowHeight());

gl::color( ColorA(0.f,0.f,0.f, 0.05f) );
gl::drawSolidRect(getWindowBounds());
gl::color( ColorA(1.f,1.f,1.f, 1.f) );
mParticleSystem.draw();
}

Tail as a line

We will add a tail constructed from several lines.

  1. Add new properties to the Particle class inside the Particle.h header file:
    std::vector<ci::Vec2f> positionHistory;
    int tailLength;
  2. At the end of the Particle constructor, inside the Particle.cpp source file, set the default value to the tailLength property:
    tailLength = 10;
  3. At the end of the update method of the Particle class add the following code:
    position History.push_back(position);
    if(positionHistory.size() >tailLength) {
    positionHistory.erase( positionHistory.begin() );
    }
  4. Replace your Particle::draw method with the following code:
    void Particle::draw(){
      glBegin( GL_LINE_STRIP );
      for( int i=0; i<positionHistory.size(); i++ ){
    float alpha = (float)i/(float)positionHistory.size();
    ci::gl::color( ci::ColorA(1.f,1.f,1.f, alpha));
    ci::gl::vertex( positionHistory[i] );
      }
      glEnd();
    
    ci::gl::color( ci::ColorA(1.f,1.f,1.f, 1.f) );
    ci::gl::drawSolidCircle( position, radius );
    }
..................Content has been hidden....................

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