In this recipe, we will show you how to add a tail to the particle animation.
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.
We will add a tail to the particles using different techniques.
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(); }
We will add a tail constructed from several lines.
Particle
class inside the Particle.h
header file:std::vector<ci::Vec2f> positionHistory; int tailLength;
Particle
constructor, inside the Particle.cpp
source file, set the default value to the tailLength
property:tailLength = 10;
update
method of the Particle
class add the following code:position History.push_back(position); if(positionHistory.size() >tailLength) { positionHistory.erase( positionHistory.begin() ); }
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 ); }
3.145.36.221