PDA

Click to See Complete Forum and Search --> : modelling a spring (from the actionscript forum)



corbs
11-26-2002, 01:08 PM
I think my post may belong more to this forum

I'm sure these questions are asked all the time and i bet all the actionscript gurus are sick of them. but anyway.

What i'm trying to do is model a spring to follow the basic rules of Hooke’s Law. I'm just wondering what the best way to go about this is and what the best way to model force, in actionscript. is.

Please don't think i'm a :doughnut:

Somar
11-30-2002, 06:23 PM
Well let's see:
(Bolded variables are vectors)

Newton's second law says that the force acting on a body is equal to the body's acceleration multiplied by it's mass.
F = ma
Hooke's law says that the force of a string is proportional to the distance the mass on the string is from the resting point (See below) by a factor k.
F = kd


o <- fixed point
\
\
\
o <- resting point
\ ¯\ distance from resting point
\ _\
o <- mass attached to spring

NOTE: Typically, Hooke's law is for 1-Dimensional objects and is written as
F = kx
however I suspect that you would rather use a 2-Dimensional spring, so we'll replace the 1-Dimensional variable, x, with a 2-Dimensional vector d, <x,y>. ;)

Thus
F = ma
F = kd
Solving for a, the acceleration, we obtain:

a = k/m*d

Oh BTW, k is the "springiness", or "stiffness" of the string, while m is the mass of the mass attached to the spring.


Though we could use ahab's vector library when applying this to Actionscript, I think we'll just break up the vector into components to apply this to ActionScript.


k = .1;
m = 1;
d = .95; // Uh.. Damping constant... Tell you about this later ;)
rest_x = 275;
rest_y = 200;
onEnterFrame = function(){
var dx = rest_x - m1._x; // Calculate distances
var dy = rest_y - m1._y;
var ax = k/m * dx; // Calculate the acceleration
var ay = k/m * dy;
vx += ax; // Add the Acceleration to the Velocity
vy += ay;
vx *= d; // Apply the damping factor to the velocity
vy *= d;
m1._x += vx; // Add the Velocity to the Mass' position
m1._y += vy;
}
onMouseDown = function(){
vx = 0;
vy = 0;
m1._x = _xmouse;
m1._y = _ymouse;
}


Make a clip, give it the instance name m1, and paste the code. Then click away!

I'll add to it later. Gravity, a moving resting point, etc. This is just a start. =\




Later,
Martin Muñoz

corbs
12-01-2002, 09:52 AM
Thanks a lot for the very detailed reply. I should have updated this thread though, i had found a solution in bit-101's tutorials. I am however working on a new problem (if your in any way interested ;))

3 or more objects linked together with invisible 'springs'



[ obj1 ]
|
[ obj2 ]
|
[ obj3 ]
|
[stationary pt]


And a spring going from each one of the objects to the mouse. I want this force exponential so that the objects only react when the mouse is quite close.

This will probably sound silly if i haven't explained it right :)