You can probably guess that I always have some project on the back burner. This week, I was working on a physics problem related to a superhero movie, and I started working with gravitationally interacting objects in Python. It’s not exactly what I wanted, but I ended up making a bunch of gravity balls. And I can’t stop playing with them.
If you don’t know what a gravity ball is, don’t worry! Because I totally made them up. A lot of the time, we use physics to describe motion in the real world. I can analyze a viral video to figure out if it’s fake, or describe the motion behind your favorite toy, or predict whether a cat will survive a fall from a high-rise building. But sometimes the best way to understand physical properties is by modeling something totally beyond the realm of real life—like gravity balls.
Two Gravity Balls
Let me start with the simplest case of gravity balls so that you can really get a feeling for what’s going on. Suppose I have two balls somewhere in space. It’s just these two balls and nothing else—no planets, no stars, no Starbucks nearby. In that case the only interaction between the two balls would be gravitational. What will happen to these two balls? Since there is a gravitational force pulling them together, they will both increase in speed (that’s what forces do). But as they move closer together, the gravitational force increases even more. Overall, it’s an interesting motion problem.
This gravitational force would depend on the product of the mass and the distance between the two balls. Maybe this picture will help.
These two balls are somewhere with respect to some origin (it doesn’t really matter). What matters is the vector from A to B which is labeled as just r in the above diagram. This vector can be found from the following:
With this vector r, I can find the gravitational force on ball B as:
I should point out a few things with this expression. This is the gravitational force on B as exerted by A. There is also a force from B on A, it would have the same magnitude but opposite direction. The G is the universal gravitational constant. The r with the “hat” is called r-hat (surprise) and it is a unit vector. This unit vector doesn’t change the magnitude of the force, but without it, you would just have a scalar force and that wouldn’t be so useful.
What do we do with the force anyway? The gravitational force on object B can be used to find the change in momentum of B after some time interval where the momentum is the product of mass and velocity. This comes straight from the momentum-principle.
Yes, the p is momentum. Before you ask, I don’t know why we always use “p” for momentum. I guess it’s because “m” was already taken to be used for the mass. OK, there is one more equation. If I know the object’s momentum, then I can use the definition of momentum and average velocity to find the new position of this thing after some short time interval.
The r-vector represents the position since “p” was already taken. That was just a joke: Actually, we use “r” for position because it comes from the “r” in spherical coordinates. But this leads to something good. It goes like this: calculate the force, update the momentum and then update the position for some short time interval. Do this also for the other mass. Since the positions change, the force changes so you need to do it all again. And again. And again. Keep doing this until you want to stop—and boom. That’s the whole idea behind a numerical calculation.
Let’s just do it. Here is a numerical calculation for two masses in space. Go ahead and click the “play” to run and you can see the code by pressing the “pencil” icon.
I tried to put some comments (comments are lines with the # symbol in front of them) in the code so that you can sort of figure out what is going on if you want to play with it. It might not look like anything is happening at first because the balls start off really slowly. Just wait and you should see something.
What happens to the balls? They do indeed start to move towards each other and speed up during that process. However, they aren’t real balls so that there is nothing preventing them from passing through each other—and so they do. After that, the gravitational force slows them down until they start moving towards each other again. If you let it, these would just oscillate back and forth forever.
Also, I cheated. The balls have a mass of 1 kilogram, but I changed the universal gravitational constant to some much larger number than the real life version. In real life these balls would take forever to get together since real gravity is super weak.
Finally, one other note. If you look at the code you can see that when the balls overlap, I set the gravitational force to the zero vector. There is a reason for this—it’s not just because I’m crazy. If the center of the balls get really, really close then the magnitude of the r-vector is really, really small. A small r means a giant gravitational force. This giant gravitational force for a very short time can make weird things happen. The overlap force of zero prevents weird things.
More Gravity Balls
OK, but I think we can agree that two interacting gravity balls are sort of boring. What about 10 gravity balls? How would you make 10 balls interact? It’s really just more of the same thing that worked for just two balls. With 10 balls, I can take one ball at a time. I need to find the distance to all the other balls and the gravitational force from all other balls. After that, I can update the momentum and position for that ball. Oh, I still need to do the same thing for the other nine balls. Yes, it’s a bunch of work—but that’s why I’m making a computer do it instead of me.
Here is an animation of what this looks like. If you want to play with the code (and you should), it’s right here.
That’s pretty cool right? Notice that I gave the balls a random color—just for fun. Also, these things will just keep moving around forever (or as long as the program runs). But what’s next? How about 100 balls! No wait, 200 balls! This is the exact same code as before, just with more gravity balls. Actually, this is running at 5x speed so that it goes faster.
I don’t know about you, but I’m really starting to get excited. You really need to check out the code and run it yourself (you can’t break anything—I promise). See how many gravity balls you can run before your computer explodes (no, it won’t really explode). Of course as you increase N (the number of gravity balls) you dramatically increase the number of computations. Not only do you have more balls to update with the momentum, but each ball has more balls that exert a force on it. I can get up to 1,000 balls, but it doesn’t run super fast.
Adding a Drag Force
Let’s change it again. I don’t want these balls to oscillate in and out forever. I want them to settle down into one big blob. I can do that by adding a drag force. This drag force has two main properties. First, it has to be in the opposite direction of each ball’s momentum (or velocity) so that it makes the ball slow down. Second, the drag force should be proportional to the magnitude of the momentum so that a ball at rest has no drag force and a super fast ball has a super high drag force.
Actually, I’ve had a drag force in the code the whole time—but the drag coefficient was set to zero so that it wouldn’t do anything. If I put the coefficient to some arbitrary value (I picked 0.2), this is what happens (for 100 gravity balls). Note: I increased the playback speed by a factor of 10—just because.
If you zoom in to the final form, it looks like this:
The individual gravity balls still move around a little bit. I think I know what’s going on. Most of the balls are within the radius of the other balls (they are overlapping) so that most of these do not have a gravitational force on them. For the ones that do have a gravitational force, they get pulled back into the ball blob.
A Different Random Distribution
After playing with the gravity balls for a while (I hope someone is keeping track of how many times I write “gravity balls”), I saw something weird. With a large number of balls, I always get a formation that looks like this.
Hopefully you can see that “x”. Why is there an “x”? Actually, you can even rotate this distribution of balls around and it’s still and “x” in three dimensions. I think I know the cause of the “x”. I created 1,000 gravity balls by picking three random numbers for each ball position. The x,y,z coordinates of each random number is between -1 and 1 so the possible positions of balls would be a 2x2x2 cube. Of course, the average center of mass for a distribution would be in the center of this cube and the masses are all attracted towards that point. But the non-spherical distribution of balls must be the cause of this x-feature.
I can test this out if I make a spherical distribution of gravity balls. Instead of creating random x,y,z coordinates, I am going to use spherical coordinates and create the following three variables: r (0 to 1), θ (0 to π), φ(0 to 2π). This should make a spherical distribution and there shouldn’t be the resulting “x” pattern. Here we go.
Looks good. Oh, since that has 1,000 gravity balls, I played it back at 30x speed. If you want the code, here you go (it’s pretty much the same as the other stuff).
I haven’t assigned homework problems lately, but there are many questions you could answer for this situation.
- In my gravity balls model there is no force when two balls overlap. How can you make this interaction so that it is more like hard sphere collisions with no overlapping?
- Is energy conserved in the case of 100 gravity balls with no drag force? What about momentum? Hint: Make a graph.
- Do you get significantly different results if the ball masses are not constant? Give the balls a random mass between some range (you pick the range).
- I don’t know if this will work, but what would happen if a giant collection of gravity balls (let’s say 500) had a slight rotation to begin with. When the whole thing compresses into a compact ball, would that ball be spinning super fast? It should. Oh, don’t forget about the drag. If your drag force is too high you will lose angular momentum.
- What if you just give all the gravity balls some small random momentum to start with. What happens then?
- Suppose there were two massive gravity balls at the edge of the distribution of balls (opposite ends). Could these two balls make two different blobs form? That would be cool.
- Did you try playing around with the drag coefficient? You should.