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:
Add properties to your application's main class.
Vec2f attrPosition; float attrFactor, repulsionFactor, repulsionRadius;
Set the default value inside the
setup
method.attrPosition = getWindowCenter(); attrFactor = 0.05f; repulsionRadius = 100.f; repulsionFactor = -5.f;
Implement the
mouseMove
andmouseDown
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...