[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
This is another in the "Physical World" (or maybe "Physics World"?) series of programs. The Bouncing Ball and Cannon programs started it. This one begins an exploration of Spring-Mass systems by modeling the actions of a coiled spring with a weight hanging from it when we give it a push or stretch and release it. The motion of the "system" is animated as the spring bounces.
Background & Techniques
I'm interested in programs that model the real world. One goal is to develop a model Physics World where we can assemble machines and watch them run. Remember "The Incredible Machine" from DOS days?
That's still a long way off, but we'll definitely need spring-mass systems as a component of that world. The current version, a single spring with a dangling weight, is about 300 lines long. But the heart of the program, the Animate routine, has only 30 or so lines of Delphi code, so why not start now.
First the physics: Thanks to Mr. Newton, we know that if we suspend a weight from a spring, it will just hang there (1st law). We'll also be needing his 2nd law (acceleration is proportional to the force applied and inversely proportional to its mass, A=F/M).
To get our system moving, we'll consider two initial conditions: The initial stretch, X0, applied to the spring and/or the initial push or force, F0, applied to the spring.
Once we get our spring-mass in motion, we need to calculate the displacement as function of time. X=f(t). To do this we'll need to worry about how fast the mass is moving, velocity, which in turn depends on how fast the velocity is changing over time, acceleration. In math terms, this involves a differential equation. For our purposes, we won't need to worry about derivatives or integration (even though we will be implicitly using both). The up and down motion of our weight is controlled by four primary factors: Gravity, Mass, Spring Constant and Damping.
These six parameters give us enough information to set our mass in motion and predict how it will move. In practice, the animation loop assumes one time unit increment each time through. With some simplifying assumptions, the new distance X increases by the velocity, V (X=X+V). The velocity changes by the acceleration value A (V=V+A). And finally the acceleration depends on the forces acting on the mass as described above. Here's where we need Newton's 2nd law, A=F/M. All of our forces are acting in the same direction (vertical), so we can just add them up: A= (MG-KX-CV)/M or A=G-(KX+CV)/M. When you examine the code, you'll see these three equations for X, V, and A as the heart of the loop. Everything else is detail.
There's another aspect of the spring constant that we have not discussed. Some springs, for example rubber bands or screen door springs, that can only apply force by pulling. The above equations would hold for an automobile spring, but not for one of these floppy springs. We'll leave the addition of this kind of spring for the next version.
The code defines a TSpring object as a descendent of TPanel. A Timage contained in TSpring supplies the drawing canvas. Methods are provided to draw the spring & mass image of a given length and to erase it. The Animate method contains the loop discussed above to calculates a new length and erase the old and draws the new image at each step. Most of the remainder of the code gets parameters from edit fields and ensures that they are valid numbers.
Addendum 6/27/2003: Viewer Don Rowlett reported that the spring animation loop does not always end and suggested some additional error checking of the input parameters. That gave me the excuse to revisit the program with the result posted today. The loop resulted because I was trying to stop when velocity and acceleration were both small and the weight was at displacement 0 (un-stretched spring). Of course, with a weight attached, the spring does not come to rest at 0 but is stretched by G*M/K. Defining a new property, XEnd, to reflect the resting position and testing against that value corrected the problem. While at it, several other changes were made including correcting the damping problem (Damping is now entered and a value between 0 and 1 and adjusted internally to range from no damping to fully damped). And the spring type may be defined as a compression type, acting in both directions, or "rubber band", acting to pull on the weight only. Also the weight end of the spring may be defined as initially constrained or unconstrained.
Addendum February 12, 2007: It has been several years since I have looked at the Spring mass program, but received an email recently from a college instructor who would like to use it in a Differential Equations class to provide visualizations. I made a few enhancements and fixed a few bugs based on his suggestions.
SpringMass2 was posted today for his review. (So Springmass2.1 may not be far behind :>)
Running/Exploring the Program
Suggestions for Further Explorations
Got too much spare time? These should solve your problem.
Copyright © 2000-2018, Gary Darby All rights reserved.