For my graphics final project, I created a flag that responds to wind and gravity as well as drag. The flag was created using a mass spring system. There are thirty points connected with springs in a grid format. These springs keep the grid together while responding to a variety of forces.
In this demo I show how the flag reacts to just the force of the fixed points, then add in gravity and wind. I then show the wireframe option and decrease the wind speed before resetting the corners of the flag.
The wireframe option shows off the dynamically rendered triangles that make up the flag. Each of these triangles is based on an index into the point array that is updated each pass of render.
There is a constant downward force that is applied to all particles when gravity is turned on.
There is a variable strength force that pushes the particles back and forth. It pushes them in one direction for a period, then switches directions to cause the rippling effect.
I used a particle system connected with springs to create the points of the flag. Each particle in the system has a position and velocity as well as a force accumulator and various state variables. They also contained functions for either fixing the position and velocity or altering them by applying forces. The forces act on the velocities which then act on the positions. There is also special logic for a spring as the spring acts on two particles at once. Each particle has a list of springs connected to it and when forces are being calculated, the particle alters the force both on itself and on all the particles it is attached to based on Hook's law.
These particles were then connected with triangles using a GL_ARRAY_ELEMENT_BUFFER. This structure allowed me to pass an array of indices, then alter the values at these indices each pass of render. I added shadows based on the distance from the camera, so you can see the depth of the flag.
I got help from the following sources throughout my project: