Other/ Modelling elastic strings - Hooke's Law
Model of connected pendulums obeying Hooke's Law. Some are fixed, others are restrained by tension in the supporting springs.
Hooke's Law
Hooke's law says that the force due to tension in an ideal spring or elastic string is proportional to the extension.
Force = λ x Extension
λ is the elasticity constant, a number that measures how stiff or how bouncy the elastic is.
In the above activity the position of the fixed black movieclip is (x0,y0) and the position of the grey moving movieclip is (x1,y1).
The length of the string is the distance between these two points (see line 1 below).
The difference between the extended length at the natural length is the extension (line 2 below)
The extension is coloured red in the above activity.
Here is the code:
1. var exLength:Number = Math.sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1)); 2. var extension:Number = exLength-natLength; 3. var theta:Number = gettheta(x0, y0, x1, y1);
These lines calculate the extended length of the string, the extension and the angle of the string (measured anticlockwise from horizontal)
4. if (extension>0) { 5. //if extension > 0 then find force (proportional to the extension) 6. var force:Number= lambda*extension; 7. var forcex:Number = -force*Math.cos(theta); 8. var forcey:Number = force*Math.sin(theta); 9. } 10. else { 11. forcex = 0; 12. forcey = 0; 13. }
If the string is taught (extension > 0) then we calculate the tension in the string, lambda times the extension, where lambda is the string constant.
We know from school maths that given the hypotenuse of a triangle we multiply by cosine and sine to get the x and y components. There is a minus sign on the x component because the tension acts in the opposite direction to the string (pulling against the grey movieclip). There ought to be a minus sign in front of the y component too, but that is cancelled out by another minus sign - because flash's y axis is upside-down.
If the string is slack (extension < 0) then there is no tension at all. We need these lines otherwise we would get a negative force which is obviously not realistic.
14. var accnx:Number = forcex/weight; 15. var accny:Number = gravity+(forcey/weight);
Use Newton's law (acceleration = force / mass) to find the acceleration.
In the y-direction we also add the force due to gravity.
16. velx += accnx; 17. vely += accny; 18. x1 += velx; 19. y1 += vely;
Each frame, the velocity is incremented by the acceleration, and the position is updated by the velocity.
20. function gettheta(x0, y0, x1, y1):Number { 21. var theta:Number; 22. if (x1 == x0 && y1 >=y0) { 23. theta = 3*Math.PI/2; 24. } 25. else if (x1 == x0 && y1 < y0) { 26. theta = Math.PI/2; 27. } 28. else { 29. theta = Math.atan((y1-y0)/(x1-x0)); 30. if (x1 < x0) { 31. theta += Math.PI; 32. } 33. } 34. return (-theta); 35. }
Finally, this function returns the angle between (x0,y0) and (x1,y1). It is measured anticlockwise from horizontal.