Creating springs
In this recipe, we will learn how we can create springs.
Springs are objects that connect two particles and force them to be at a defined rest distance.
In this example, we will create random particles, and whenever the user presses a mouse button, two random particles will be connected by a new spring with a random rest distance.
Getting ready
We will be using the same particle system developed in the previous recipe, Creating a particle system in 2D. Create the Particle
and ParticleSystem
classes described in that recipe and include the ParticleSystem.h
file at the top of the application source file.
We will be creating a Spring
class, so it is necessary to create the following files:
Spring.h
Spring.cpp
How to do it…
We will create springs that constrain the movement of particles. Perform the following steps to do so:
In the
Spring.h
file, we will declare aSpring
class. The first thing we need to do is to add the#pragma once
macro and include the necessary files.#pragma once...