Applying repulsion and attraction forces

In this recipe, we will show how you can apply repulsion and attraction forces to the particle system that we have implemented in the previous recipe.

Getting ready

In this recipe, we are going to use the code from the Creating particle system in 2D recipe.

How to do it…

We will illustrate how you can apply forces to the particle system. Perform the following steps:

  1. Add properties to your application's main class.
    Vec2f attrPosition;
    float attrFactor, repulsionFactor, repulsionRadius;
  2. Set the default value inside the setup method.
    attrPosition = getWindowCenter();
    attrFactor = 0.05f;
    repulsionRadius = 100.f;
    repulsionFactor = -5.f;
  3. Implement the mouseMove and mouseDown methods, as follows:
    void MainApp::mouseMove(MouseEvent event)
    {
      attrPosition.x = event.getPos().x;
      attrPosition.y = event.getPos().y;
    }
    
    void MainApp::mouseDown(MouseEvent event)
    {
    for( std::vector<Particle*>::iterator it = mParticleSystem.particles.begin(); it != mParticleSystem.particles.end(); ++it ) {
      Vec2f repulsionForce = (*it)->position - event.getPos();
      repulsionForce = repulsionForce.normalized() * math<float>::max(0.f, repulsionRadius - repulsionForce.length());
              (*it)->forces += repulsionForce;
          }
    }
  4. At the beginning of the update method, add the following code snippet:
    for( std::vector<Particle*>::iterator it = mParticleSystem.particles.begin(); it != mParticleSystem.particles.end(); ++it ) {
      Vec2f attrForce = attrPosition - (*it)->position;
      attrForce *= attrFactor;
        (*it)->forces += attrForce;
    }

How it works…

In this example we added interaction to the particles engine introduced in the first recipe. The attraction force is pointing to your mouse cursor position but the repulsion vector points in the opposite direction. These forces were calculated and applied to each particle in steps 3 and 4, and then we made the particles follow your mouse cursor, but when you click on the left mouse button, they are suddenly moves away from the mouse cursor. This effect can be achieved with basic vector operations. Cinder lets you perform vector calculations pretty much the same way you usually do on scalars.

The repulsion force is calculated in step 3. We are using the normalized vector beginning at the mouse cursor position and the end of the particle position, multiplied by the repulsion factor, calculated on the basis of the distance between the particle and the mouse cursor position. Using the repulsionRadius value, we can limit the range of the repulsion.

We are calculating the attraction force in step 4 taking the vector beginning at the particle position and the end at the mouse cursor position. We are multiplying this vector by the attrFactor value, which controls the strength of the attraction.

How it works…
..................Content has been hidden....................

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