PDA

View Full Version : Writing a pendulum simulation - ideas and comments please


Summer
10-14-2005, 04:47 PM
Hello

I want to try to write a "simple" physics demo (with user interaction). I have written some ideas (that need work) and would like some comments and other ideas and pointers on the subject of physics simulations.

The idea is having a pendulum in the center of the screen (2d). Then there will be 2 events, Force Left and Force Right, wich will apply a force in one of those directions (2d movement). The pendulum will also have gravity.

So the user will be alloweed to give the pendulum some movement trough the events wich will set make the pendulum move.

Example:
start ) the pendulum is stopped in it's lowest possition
then ) the user presses the LEFT event key, pendulum will start moving left
end ) the pendulum enters it's left-right-left movement until it stops (stop action - there is no air speed to counter the gravity).

Note 1: keeping it simple, that force that the user applies is not continuos (that is, the user doesn't keep pushing the object, he just gives a little touch that starts the movement).

Note 2: for now forces (other than grvity) while there is movement will be ignored.

The loop could look like

loop
// calculate time
// apply some formula to get the new position of the object -
// taking the current forces in consideration and the time
redrawObject ( x, y );


Then the code would also write a line that represents the string and maybe some liens representing the aceleration vectors.

As for the stop action, I was thinking of reducing the acelleration (not at once) so it stops in a smooth fashion.

Now I will to try to find a way, trough physics rules, to update x and y so the object can be drawn in a "realistic" pendulum motion. Any suggestions?

If you think a pendulum should not go as the first physical simulation writen, please give other ideas that are better to start.

Thanks.

corey
10-14-2005, 07:05 PM
There's nothing wrong with using that as a demo, if that was your question.

corey

Summer
10-15-2005, 04:32 PM
Hello.

The question actually is: where can I start gathering information about this? Any Suggested articles? Howto "program physics"?

Thanks.

MJeannig
10-16-2005, 03:32 AM
Chris Hecker wrote some very good article about physic for games years ago
http://www.d6.com/users/checker/dynamics.htm
The acceleration will smooth automatically as the gravity will progressively decrease the initial speed.
MJ

Faelenor
10-21-2005, 06:36 PM
The acceleration will smooth automatically as the gravity will progressively decrease the initial speed.

Without friction, the pendulum will never stop, the gravity won't remove energy from the system. The gravity will just convert potential energy to kinetic energy and so on.

VividHazE
10-27-2005, 08:15 PM
Without friction, the pendulum will never stop, the gravity won't remove energy from the system.

Shouldn't it be possible to add code that adds a negative value to the forces in motion to slow it down gently. By looking at a real pendulum swing it seems to get slower after every swing.

I guess that wouldn't be using any cool physics stuff, but it would be a rough hack if you couldn't get it completed before the deadline :yes:

NomadRock
10-28-2005, 12:07 AM
Shouldn't it be possible to add code that adds a negative value to the forces in motion to slow it down gently. By looking at a real pendulum swing it seems to get slower after every swing.

A real pendulum will slow down due to frictional forces. It is pretty simple to just multiply it's total energy (or just speed) by some constant factor each timestep. Probably the multiplyer will be some large number still smaller than 1, so maybe 0.99

This is not a physically accurate model technically, but it is close enough that not even a physics student would probably notice :)

Jenesaispas
10-29-2005, 09:45 PM
Im not quite sure if this is going to help you but here are some formulas to describe the motion of a pendulum.

T = 2(Pi) Radical(L / G) - Only works for under 15 degrees over vertical

T = The time it takes the pendulum to travel through its motion
Pi = 3.14
Radical - can't type a radical..
L = the length of the suspending string
G = 9.8 mps -Gravitational constant

Friction = Friction coefficient for air to metal(-I don't know this off the top of my head google it) * the normal force ( gravity * mass) -this will give you the force of friction working against the pendulum.

V = D / T - D = distance, t = time v = velocity

Acceleration = Change in V / Change in T

Force = Mass * acceleration

The pendulum will stop moving when the net force on the bob is equal to zero. Beginning Force - Air resistance * Time.

-I hope this helps and is some what clear.

giannoni
04-05-2006, 05:14 PM
Be advised that a classical pendulum normally assumes relatively small angles of deflection where Sin(d) ~= d etc. This gives rise to the classical sinusoidal motion. However if the angle becomes appreciable, then the governing differential equation become insoluable and numerical computer methods are required. I have extensive experience with just this exact problem ....

peterbone
04-07-2006, 05:40 AM
Why are you bringing back up such an old thread?

If anyone's interested I wrote an inverted pendulum and double inverted pendulum demo in Delphi which uses Runge Kutta to solve the differentiall equations (it works as a non-inverted pendulums too). The double pendulum is a challenging and fun game.

http://atlas.walagata.com/w/peterbone/Balance.zip (includes source)

I used mathematica to convert the 3 coupled second order differential equations of the double pendulum into 6 coupled first order equations so it could be solved using runge kutta.

giannoni
04-08-2006, 06:19 PM
Well, just to show that things sometimes are not as simple as initially perceived, etc. Nice job on the analysis and programming (I think), but I don't have Delphi or Mathematica, so don't know your analysis, etc. Do you have it summarized (in more detail) in some document, etc.? Could you have used Runge-Kutta-NYSTROM method for the 3, 2nd order equations? It "feels" like going to 6, 1st order equations might have been a compromise ...

giannoni
04-08-2006, 06:39 PM
What're the dynamics of the cart? It looks like it is a mass, spring loaded to return to the mouse pointer horizontal position ...

giannoni
04-08-2006, 06:49 PM
Also, try as I might, I can't get the double pendulum arrangement to rotate "over the top", which doesn't seem quite right ...

peterbone
04-09-2006, 07:46 AM
Here's a web page I found for a single inverted pendulum that explains the process of creating a computer simulation and using mathematica to reduce the equations to first order:
http://drewk.net/projects/ipendulum/ipendulum.html

Here's the derivations of the double pendulum model written in a control theory paper.
http://www.tf.uni-kiel.de/etech/ART/paper/2001/ieee_cca_isic_zhong.pdf

Here's the mathematica resulting first order equations for the single and double pendulums
http://img.photobucket.com/albums/v424/peterbone/invertedpendulum.gif
http://img.photobucket.com/albums/v424/peterbone/doubleinvertedpendulum2.gif
It took mathematica nearly 1 minute to calculate the double pendulum equations so doing it on paper would be totally impractical.

I'm guessing the Runge-Kutta-Nystrom method solves systems of second order equations. I think I looked into it but couldn't work out how to do it or something. I probably thought it would just be easier to follow the same principles used in that first link.

I should have said how to control it. The force on the cart is proportional to the distance of the cursor from it (horizontally).

You can't get the double pendulum to rotate over the top? I don't really understand. Maybe you just don't have the knack.

BTW, here's my top score on the double
http://img.photobucket.com/albums/v424/peterbone/proof_337s.gif

and here's an advanced trick. Rotating the bottom pole while keeping the upper pole upright and then back into a balance
http://atlas.walagata.com/w/peterbone/balance_sim_trick.avi

and here's why I made the program in the first place. I wanted to learn it in real life and practicing on a 2D version made it easier to learn.
http://www.jugglethis.net/filemgmt/visit.php?lid=168

giannoni
04-09-2006, 12:53 PM
PB - Absolutely fascinating, and "Hats Off To You ! ...", for developing all of what you did!! BTW, my browser can't get "http://www.jugglethis.net/filemgmt/visit.php?lid=168" so you might want to check to see if there's a problem. My experience is VERY relatively rudimentary, using R-K and R-K-N to solve 1st and 2nd order "simple" problems respectively. All my application programming is done in QB45, VB v3 then v6, and NSBasic for Palm stuff. We have a site for some interesting Strange Attractor stuff : http://www.homestead.com/giannoni/sa18.html . Again, congratulations on your work! Steve & Lana