Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


pendulum simulation code

This topic is 6209 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)

if (rot < 30)
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

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post

Link to post
Share on other sites
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" 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")

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.

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

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!