This page was last updated: September 5, 2022
You get to create a chain spring mesh (like the example in the notes), where all nodes can move in 2D. Its motion will be like a Slinky dangling from your hand.
I have a wrapper program that will give you the basic program, including sliders to control the X and Y locations of the top node. Other sliders will let you control the spring constant, gravity, mass, and damping. Here it is: Coming soon.
struct state { float x; float vx; float y; float vy; }; struct derivatives { float vx; float ax; float vy; float ay; };
In the GetDerivs( ) function, fill in the equation for the force on each node, taking into account weights, springs, and damping.
glutSetWindow( MainWindow ); glutPostRedisplay();
stay at the end of Animate( ). They are what force the graphics system to redraw your scene with the newly-computed values.
glBegin( GL_LINE_STRIP ); glVertex2f( X0, Y0 ); for( int node = 0; node < NUMNODES; node++ ) { glVertex2f( State[i].x, State[i].y ); } glEnd( );
glLineWidth( 2. );
before the call to glBegin( ). size is a floating point number. A size of 1.0 is a single pixel-wide line.
void GetDerivs( struct state state[NUMNODES], struct derivatives derivs[NUMNODES] ) { for( int node = 0; node < NUMNODES; node++ ) { float xm, ym; // vector from bottom of node #node to preious node float xp, yp; // vector from bottom of node #node to next node float sumfx = 0.; float sumfy = Mass*Gravity; if( node == 0 ) // node #0 is attached to node Y0 by a spring { xm = ????? ym = ????? } else { xm = ????? ym = ????? } float length = sqrtf( xm*xm + ym*ym ); // to normailze the vector float stretch = length - LENGTH0; // amount spring is stretched float force = K * stretch; sumfx += ????? sumfy += ????? if( node < NUMNODES-1 ) { xp = ????? yp = ????? length = sqrtf( xp*xp + yp*yp ); stretch = length - LENGTH0; force = K * stretch; sumfx += ????? sumfy += ????? } float vx = state[node].vx; float vy = state[node].vy; float v = sqrtf(vx*vx + vy*vy); if( v > 0. ) { force = Cd * v; sumfx -= ????? sumfy -= ????? } derivs[node].vx = vx; derivs[node].ax = sumfx / Mass; derivs[node].vy = vy; derivs[node].ay = sumfy / Mass; } }
You will definitely need to see if your program is working. Here is the file: Chain2019Wrapper.zip, a graphics wrapper program and all the Visual Studio stuff that goes with it. Just unzip all of this into a separate folder and double click on the .sln file.
Warning #1: This will not compile as-is. There are question marks indicating things you need to fill in.
Warning #2: You might need to tune the key parameters (Mass, K, Cd, Dt) for the speed of the system you are on. (The given values are tuned for my laptop.)
Your electronic turnin will be done at
http://engr.oregonstate.edu/teach
and will consist of:
This is due at 23:59:59 on the listed due date.
Item | Points |
Motion behaves correctly when X0,Y0 is moved | 20 |
Motion behaves correctly when the Mass (Mass) is changed | 20 |
Motion behaves correctly when the Spring Constant (K) is changed | 20 |
Motion behaves correctly when the Damping Coefficient (Cd) is changed | 20 |
Motion behaves correctly when Gravity is changed | 20 |
Potential Total | 100 |