Sunday, October 01, 2006

Springs: Elasticity

The physics behind it:

We will take the example of a mass attached to a spring. Let's say that the normal state of the system, that is to say when nothing moves, in other words the equilibrium, happens when the spring has a length l. If you move the mass by dx, making so that the spring has now a length l', the force will then be

-k.dx = -k(l'-l)
k is a specific to each spring, and it is <1

All this to show you what you already know : the more we pull on the spring, the faster it will go. Here, pulling hard means big dx, hence a big force, and a high speed.

But we must not forget about inertia. The mass always tends to go back to equilibrium, but it has a certain speed when it does, so it doesn't just stop there.

The ActionScript code:
This code goes inside the Actions panel of the mass, which has to be a movie clip.

onClipEvent (load) {
// inertia relates to the quantity of energy that
// the spring will carry
// inertia = 1 would mean that the spring doesn't
// loose any energy, and that it will oscillate
// forever
inertia = 0.9 ;

// k relates to the spring, and how "hard" it will be.
// The higher k the faster the mass will come back.
k = 0.1 ;
}

onClipEvent (enterFrame) {

// We calculate the distance to the mouse
x = -this._x + _root._xmouse ;
y = -this._y + _root._ymouse ;

//We calculate the amount by which the mass will to move
xp = xp * inertia + x*k ;
yp = yp * inertia + y*k ;

//We move it
_x += xp ;
_y += yp ;
}


For further information, click here.