Modeling Physics (Gravity and Drag) in Processing

Posted by Burak Kanber | Comments

My girlfriend (an artist) is in one of Cooper Union’s learn-to-computer-program courses. The language they’re using is called Processing, a very simple programming language based on Java and aimed toward graphical output. The project she decided to do was to make a simple bounce-a-ball game, and she asked me for help with the physics of the ball motion. I helped her of course, but being an engineer, I took what I did and expanded on it (and maybe went over the top). What I ended up with was a gravity simulator that also takes into account inelastic collisions, air friction for laminar flow around the ball, and drag force for turbulent flow around the ball. The code is open for you to play with, all variables are parameterized (everything from the size of the ball and screen, to the density and viscosity of air), and heavily commented.

I’ll display the program right here as a Java applet, and below it I’ll have a link to the source code as well as executable files for Linux and Windows. I could explain the code, but I think downloading Processing and looking at my uber-heavily commented code is enough. Here’s exactly what the program does:

  1. Models gravity. You can change the acceleration due to gravity, to simulate any environment.
  2. Models inelastic collisions with walls. You can change the coefficient of restitution for any of these collisions.
  3. Determines the Reynolds number of the flow around the ball, and determines whether or not flow is laminar or turbulent. You can set this threshold.
  4. Based on laminar or turbulent flow, the program simulates air drag. Simple Stoke’s Drag for laminar flow, and Lord Rayleigh’s Drag for turbulent flow. You can edit all parameters, such as the density and viscosity of the fluid (not necessarily air).

One major caveat: all the math done in this program was done in meters. I set the diameter of the ball to 10 (meters) because processing interprets that as pixels, and 10 is really the best size for viewing the simulation. Because a 10-meter, 3 kilogram ball exhibits huge drag forces, it’s not really too much fun to watch. So I adjusted some other parameters, and even though the ball on the screen is 10 pixels wide, it simulates the motion of roughly a .75 meter ball. The dimensioning of this scale model is kinda hard to get under control, but all the physics is in place. I leave it up to you to download the code and play with parameters =)

Click and drag in the screen to “throw” the ball. My personal favorite thing to do is shoot the ball way up, and then watch it reach terminal velocity…

(If you don’t see the applet below this line, then you don’t have Java)

As promised, here are some links:

  1. Source code (Processing File)
  2. Windows Executable (Zip)
  3. Linux Executable (tgz)

Sadly, I don’t have a Mac so I can’t make a Mac executable. Mac users can download Processing from their site and just run the source code.

If anyone has any questions or any corrections (I did this all in one night, and didn’t really proof read my code :P ), please leave a comment!

Comments

No comments have been made.    post comment

Post a comment