Due by email before class, Monday, Feb. 14.
We’ve had movers pulling on each other following the laws of gravity. In this assignment, you are going to swap out gravity for springs.
Replacing gravity with springs.
Grab the code we did in class on Feb. 11. This is your starting point.
Focus in and make sure you understand the part of the code where gravity was implemented. Your most significant changes are going to be here:
for (int i = 0; i < movers.length; ++i) {
for (int j = 0; j < movers.length; ++j) {
// force of mover j on mover i
if (j != i) {
PVector gravity_of_j_on_i = PVector.sub(movers[j].location, movers[i].location);
float separation = gravity_of_j_on_i.mag();
// TWEAK 2: We were getting close to division by zero when movers got very close.
// Make sure the denominator doesn't get too small (causing a huge acceleration).
if (separation < 10) {
separation = 10;
}
gravity_of_j_on_i.mult(200 * movers[i].mass * movers[j].mass / pow(separation, 3));
movers[i].applyForce(gravity_of_j_on_i);
}
}
}
Gravity goes as 1 / separation2. It gets stronger and stronger as particles get closer and closer. In the code, there is a cube in the denominator — not a square, as it is usually written, because we also normalized the difference vector at the same time.
There was a risk of division by a small number. We won’t have that risk with springs. So this code can be deleted completely:
// TWEAK 2: We were getting close to division by zero when movers got very close.
// Make sure the denominator doesn't get too small (causing a huge acceleration).
if (separation < 10) {
separation = 10;
}
Now we are down to this:
for (int i = 0; i < movers.length; ++i) {
for (int j = 0; j < movers.length; ++j) {
// force of mover j on mover i
if (j != i) {
PVector gravity_of_j_on_i = PVector.sub(movers[j].location, movers[i].location);
float separation = gravity_of_j_on_i.mag();
gravity_of_j_on_i.mult(200 * movers[i].mass * movers[j].mass / pow(separation, 3));
movers[i].applyForce(gravity_of_j_on_i);
}
}
}
Since Shiffman doesn’t give a proper explanation of springs until Section 3.10, I have prepared a one-page compare-and-contrast of gravity-vs-springs to get you up to speed much more quickly:
You need to understand the equation for the spring force and replace the gravity force with the spring force. Let’s make the equilibrium length of our springs d=300, and all the spring constants k=0.005.
Once your code is working, it should behave like a four-atom molecule. The molecule will start off excited, and then settle down into a shape where the four atoms are about 300 pixels apart from each other.
Part I was quite cookie-cutter. You were only making changes to a few lines of code, and everybody should end up with the same simulation. Part II is open-ended and people will end up with completely different results.
Instead of having every spring have an equilibrium length of 300 and a spring constant of 0.005, figure out a way to have different spring constants and different equilibrium spring lengths. In fact, you don’t even have to have every particle connected to every other particle by a spring. One way to implement this would be to have some of the spring constants be 0. Keep playing with your simulation parameters until you get something that neither (a) goes crazy, nor (b) settles down too quickly.
Figure out a way to render the springs as well as the movers. Maybe make the strong springs thick and the weak springs thin.
Embellish in any additional ways you can come up with. I haven’t done my Part II yet. I think I will add some interactivity. We can all share on Monday.