Homework 4: Springs on a Grid

CS 480 2009, Dr. Lawlor

First, I'd like you to write a little mass-on-a-spring system, where the masses sit at each texture pixel, and there is one spring connecting each mass to a fixed plate.  You need to store the mass's position and velocity at each pixel; I used the red and green channels for this. 

The controlling equations are:

    elongation = my position - 0.5
    force = -k*elongation
    acceleration = force/mass
    d velocity / dt = acceleration
    d position / dt = velocity

I defined the spring's rest elongation to be 0.5; this makes the colors oscillate around medium red.   For stability, you may need to keep the timestep and k fairly small; I used dt=0.25 and k=0.1.   If dt or k are too big, in addition to instability you can get very ugly siezure-inducing fast color changes. 

Here are my initial conditions:

Without any friction or blurring, your springs should oscillate indefinitely:

Second, also tie together adjacent springs' position and velocity using a small amount of blurring; I mixed in 10% of my four neighbors' values to get this:

Turn in only your pixel shader for this assignment; I don't need the C++ code at all, and you're welcome to reuse my "480_tex_playground" for this if you like!