pendulum simulation code

Started by
3 comments, last by penetrator 22 years, 9 months ago
hi, I need your help because i''d like to simulate a pendulum-like objcet movement, i.e. an object that rotate from 0 to 30 and, after that, from 30 to 0, returning to it''s starting position. How''d the code look like ? glHorizon_Project

www.web-discovery.net

Advertisement
I''m no guru, but wouldnt this make the same effect you want?.

int rot = 0;
int plus;
int minus;

plus rot+=1;
minus rot-=1;


if (rot > 30)
minus;

if (rot < 30)
plus;
-----
I''m sure this code wouldnt make it through the compiler, but however, this was just to show you the general idea..
does this work? =)

Kenneth Wilhelmsen
--------------------------
He who joyfully marches to music in rank and file has already earned my contempt. He has
been given a large brain by mistake, since for him the spinal cord would fully suffice. This
disgrace to civilization should be done away with at once. Heroism at command, senseless
brutality, deplorable love-of-country stance, how violently I hate all this, how despicable and ignoble war is; I would rather be torn to shreds than be a part of so base an action! It is my conviction that killing under the cloak of war is nothing but an act of murder
Basically, yes. But if you want a physically (somewhat) correct pendulum, you''ll have to respect physics. I think a formula describing the position of a pendulum over time is given in about every book about basic mechanics. Perhaps check a schoolbook.
hi. For small pendulum movements the formula is reduced and the actual motion is called "simple harmonic motion". "For small movements" means sin(theta) ~= theta and theta is the angle between the pendulum and the world surface normal.

So for small angles, this motion (simple harmonic motion) is described by: F = -k*x which is also observed in springs. At any location x away from the steady state location, the force applied is equal to some constant times x. "k" is constant of spring. Higher "k" means smaller period.

-(--------o--------)+
...............O...
<--x-->

"O" is the oscillating body, and "o" is the origin where the body will not move if it was left free.

Ok, by knowing this equation you should be able to write the code:

F = -k.x

you know "k" and you initially define an "x"
so for this k and x you find F. Note that minus sign actually defines the direction of the force. If x is positive force is negative meaning that it forces to left(assuming left is negative right is positive).

Once you have F, define an "m" which stands for mass. The heavier the body the higher the period(time elapsed for one "lap")

then,
a = F/m

where "a" is acceleration. Rest is easy. Once you have "a" simply

v = a*dt

where v is speed and dt is the time elapsed between each update of equations.

then,
x = x + v*dt

where you find a new x. Voila!

Note that at the beginning I said for small angles, but during a game or something nobody will recognize this. Since this is the absolutely correct formula for spring oscillation it does a decent job for pendulums also. However if you really need a perfect formula (for a science project or something) you can still use the computer easily, but it needs some more physics which I cannot explain without some drawings, if so e-mail me and I'll try my best.

I hope this helps, I am always bad at explaining things to others.

PS: I just found that period = 2*pi*sqrt(m/k). Note that it does not depend on initial x


Edited by - oztanharmanci on July 23, 2001 12:44:34 PM
forget my pathetic ascii art attempt there, it screwed up.

This topic is closed to new replies.

Advertisement