Groups | Blog | Home
all groups > flash actionscript > april 2004 >

flash actionscript : Pendulum


MiniMurderdoll
4/25/2004 3:44:33 PM
Hey, if possible could someone make a template for me to build on. basicly a
pendulum which you can control the height of the swing by 2 buttons.

For example it starts at 0? and then if you press one of the buttons it
increased it by 2? so it now swings from 2? to -2?. But then there is a slight
problem that it needs to slow and stop slowley before swinging the other way,
rather that stoping dead.

I found this hard so please anyone help me,

Thanks in advance

Chris
eric76
4/25/2004 4:23:29 PM
Uh, well I'm not gonna do a template, but I can give some hints:

I'm not a physics expert, but I'm quite certain the solution to the pendulum
dynamic system is an ordniary sine swing. So given the pendulum object (p),
maximum angle (maxAlfa), current angle (alfa), time (t) you could do the
following loop:
while (keepSwinging) {
t += 0.1; // Or any other speed. It is determined by the distance from the
origin to the centre of gravity I think.
alfa = maxAlfa * Math.sin(t); // remember this is in radians.
p._rotation = alfa * 180 / Mah.PI; // Assuming the pendulum is aligned as you
want it.
}

Hope it helps!
/Mikael
MiniMurderdoll
4/25/2004 4:28:43 PM
Hmmm, i should have mentioned that im 14, lol.

eric76
4/25/2004 5:03:44 PM
Well I implemented a simple example that works, but it's not a complete
template. I don't even know exactly how you do them. But here is the code. Put
it on frame 1:

if (keepSwinging == undefined) {
keepSwinging = true;
t = 0;
speed = 0.3; // determined by the distance from the origin to the centre of
gravity I think.
alfa = 0; //radians
maxAlfa = 1; //radians
}

onEnterFrame = function() {
if (! keepSwinging) return;
t += speed;
alfa = maxAlfa * Math.sin(t); // remember this is in radians.
p._rotation = alfa * 180 / Math.PI; // Assuming the pendulum is aligned as
you want it.
};

The pendulum Symobl was pretty much just a line aligned vertically with its
top fixed at the centre of the symbol mark.
I'll try attaching the .fla if it works... Btw, your attachment didn't seem to
work.

... Oh, seems you are not permitted to upload .fla's?? Well, you can get it
now from
http://www.geocities.com/mikael_eriksson76/pendulum.fla

/Mikael
MiniMurderdoll
4/25/2004 5:14:09 PM
eric76
4/25/2004 5:54:51 PM
... you flatter me :-)

The distance it swings is called amplitude in technical language. I called it
maxAlfa, i.e. the maximum of the instantaneous angle. You can get some strange
effects by making it bigger than 180 degrees :-)

Actually the 180 is there just as part of the 180/Math.PI which is the contant
used to convert between radians (which the Flash trigonometric functions use)
and degrees, which is what the Flash objects' _rotation use (why don't they use
the same? Dunno, but it's kind of irritating I know).

The magic of the whole application lies in the sine function (Math.sin), which
is a trigonometric function you can use to describe all kinds of circular
motions.

It was fun helping you out.
/Mikael
AddThis Social Bookmark Button