Bouncing balls

I’m embarrassed to admit how often I sit around wondering what a laser’s beam would look like if the room I was in was all reflective. Tonight I had some fun with Mathematica to see if I could see some of those cool patterns. I, of course, got off on a tangent, but it was still fun.

So how do we model bounces off a wall? There’s lots of ways to do it but I really didn’t want to have to constantly figure out the angle in (and hence the angle out) and also have to figure out what wall was going to be hit next. Instead I used the Lagrangian formalism to let Mathematica figure all that stuff out for me.

Before letting you know how I did it, here’s a cool animated gif to whet your appetite:

rotating octogon

A ball (or photon) bouncing around inside a rotating octagon

Note how angle out always equals angle in and the next wall is always accurately figured out.

How I did it

The Lagrangian formalism works in the following way: If you know an expression for the kinetic energy and potential energy of the particle, you take a couple of derivatives and, voila, you have the equation of motion. The rest is just putting the equation(s) of motion into a differential equation solver and make pretty pictures. Here’s the ugly math:

L=T-U

\frac{\partial L}{\partial x(t)}-\frac{d}{dt}\frac{\partial L}{\partial x'(t)}=0

The first equation gives us the Lagrangian as the difference between the kinetic and potential energy (T and U respectively). The second equation (really it’s 2 because you need another one for y) is the equation of motion. Really this whole problem boiled down to a determination of the potential energy of the walls. All the rest – angles, who gets hit, etc – was done by the differential equation solver. Pretty slick, huh?

Potential of the walls

What I wanted was for the potential energy to increase linearly the more you penetrated into the wall. The problem was that I needed to figure out the distance normally into the (possibly slanted) walls. I decided to solve that problem with a cross product. Essentially I crossed a normalized vector along the wall with a vector from one point on the wall to the point in question. That gave me the distance into the wall. By using a cross product instead of a dot product I also got a sign difference between being in the wall and being out. I used a piecewise function to make sure that if you weren’t in the wall, there was no potential.

Here’s the trajectory of a ball approaching a horizontal wall (along with a zoom in of the interaction):

single wall

bounce off a single wall

Ok, now that I’ve got one wall figured out, I just needed a function that could take as many walls as I liked. That was just a matter of running several vertices through a wall maker function.

Ok, now for some fun! I can make the walls static or time dependent and the Lagrangian just works!

First a simple static triangle:

simple triangle

simple triangle

And here’s a shot of a triangle who’s center vertex moved in a linear fashion straight down. The pink is the initial triangle and the yellow is the final triangle:

shrinking triangle

Shrinking triangle


One last pic that I thought was cool. I wondered what would happen if I let Mathematica randomly pick vertices to turn into a random polygon. Here’s what happened:

random polygon

Random polygon

Weird, huh? What happened was the initial condition was outside of the polygon (the small pink triangle). That meant that the ball rolled down into the triangle but had too much energy to be contained. What happened next was, I guess, a dynamical bound state.

I had a lot of fun with this, and I love how flexible it is. I think the rotating octagon is my favorite, but mostly what I really like is how flexible Mathematica is. I made use of the following functions built into Mathematica to make all of these (all in an hour and a half, by the way):

  • Polytopes (package)
    • Octagon
    • Triangle
    • Vertices (gives the coordinates of the vertices)
  • RotationMatrix (to rotate the octagon)
  • NDSolve (differential equation solver)
  • ParametricPlot (makes the pretty pics)
  • RandomReal (gave me the random polygon)

About Andy Rundquist

Professor of physics at Hamline University in St. Paul, MN
This entry was posted in fun, mathematica, physics. Bookmark the permalink.

13 Responses to Bouncing balls

  1. Andy "SuperFly" Rundquist says:

    Here’s an interesting question: I know with the shrinking triangle that the speed of the ball changes (because the moving wall imparts more momentum). Is that the case with the rotating octagon?

    • Yes, I believe so. When the ball is compressed on the wall, work will be done on the ball. I’m not sure if it will always be positive or average to zero. If you wanted to get crazy, include spin.

      • Andy "SuperFly" Rundquist says:

        After I posted that I went back and plotted the speed as a function of time. Sure enough, the speed is constant for the fixed triangle and changes for the shrinking triangle and the rotating octagon. For the octagon you can see when there’s a speed change by noting if the contact wall moves in or out compared to the contact point right after contact.

        Spin would be cool, that’s for sure!

  2. Eli Lansey says:

    Very cool. Are you going to share the .nb?

  3. Andy "SuperFly" Rundquist says:

    My brother just pointed out that I could use this to improve my SocCourt game

  4. Pingback: Walking Randomly » A Month of Math Software – June 2011

  5. Pingback: Free charges in conductors | SuperFly Physics

  6. Pingback: Rugby ball bouncing | SuperFly Physics

  7. Pingback: Lasers bouncing inside stuff | SuperFly Physics

Leave a comment