Physics with Danno

Next Chapter: Collisions

Lesson 0: Simulation loop

Lets just get into it.

So first a warning. I'm going to be making some claims about the nature of the universe that are, strictly speaking, not true. I'm about to describe the simulation that we're going to be working with and claiming that this is *exactly* how the universe works. It is not, but it is a good enough approximation for us at the moment. I'm also going to be leaving some things out. This is for simplicity's sake; we'll be adding missing things in as they come up.

So with all that out of the way, let's start.

The loop

The universe operates on rules. These rules are unbreakable, the rules are known, and they can be written down as a fairly simple loop of instructions. Here it is, and here's how it works:

loop for every instant of time {
    loop for every object {
        1. force = ...
        2. dvel = force / mass * dt
        3. velocity += dvel
        4. dpos = velocity * dt
        5. position += dpos
    }
    time += dt
}

There is a thing called time. This is a quantity that, every time the loop reaches the bottom and loops back around to the top, it increments by a constant value.

Now in the real universe, this timestep is absurdly small, many many billionths of billionths of a second. But that's not very useful for a simulation that a computer runs; it'd be calculating forever. So for the purposes of approximation, I'm going to be setting the timestamp for our simulations to be a small but manageable number, like 1/100th, 1/1000th of a second, something like that. The smaller the timestep, the more accurate the simulation is to reality. More on that later.

There are also *things* in the universe. Now what that means is a bit of a touchy subject for us at the moment, but generally a *thing* is something physical in the world, like a ball or a cup. Of course, a ball is made up of lots of little *things* all connected together. So *thing* in this context can also mean collections of things. We'll come back to this later probably, but for the moment *things* for us are going to be stuff like bouncy balls, heavy boxes, things like that. I'll call these things by various other names, like objects or entities.

Now every *thing* in the universe has three properties associated with them.

Now if that was all there was, the universe would be a pretty boring place. But luckily, things in the universe can influence each other. Objects can collide, they can push and pull other objects using gravity of magnetism, that kind of thing. The way that objects interact is called a force. I'll talk about specific forces later, but for right now there is one important rule about forces: whenever two objects interact, they always exert forces on each other, and those forces are always equal and opposite. In other words, if one object is feeling a force of +5, the other one will feel a force of -5.

Those concepts all come together in this loop.

Surely you're joking!

So you're probably wondering, why is it like this? What's the deal with dividing the force number by the mass number?

The answer is, perhaps unsatisfying as it is, that that's just how the universe works. Those are just the rules. They've been experimentally verified by millions over the last several centuries, and this is just how it works. To quote a famous physicist: "I can only tell you what it looks like!"

Forces

Now, in the simulation loop above, it doesn't say how we get the force for each object.

This is a big subject. Essentially, each type of interaction between objects have their own formula. A good bit of this series will be spent talking about these.

But there are a few common properties that all forces have:

Specifically what I mean are these changes to the simulation loop:

loop for every instant of time {
    loop for every object pair (object 1, object 2) {
        force for object 1 += force_function(object 1, object 2)
        force for object 2 += force_function(object 2, object 1)
    }

    loop for every object {
        1. force = total force for this object
        2. dvel = force / mass * dt
        3. velocity += dvel
        4. dpos = velocity * dt
        5. position += dpos
    }
    time += dt
}

There's one more vitally important rule. I've already mentioned it, but I'll say it again:

What I mean by this is that force_function(object 1, object 2) = -force_function(object 2, object 1). So in the loop above, whatever gets added to the force for object 1 gets subtracted from the force for object 2, and vice versa.

Examples

To get a little more comfortable, let's look at some concrete examples.

This is a simulation of two colliding bouncy balls. We'll be going over collisions in detail in the next several lessons. For now, I want to talk general. The important thing to know about collisions for right now is that the balls will only feel a force while they're touching.

There are 3 parts to this simulation: before the collision starts, during the collision, and after the collision ends.

Before the collision starts, neither ball feel any force at all. And so

So before the collision starts, both balls are moving at a constant unchanging speed. Same thing after the collision ends, too.

During the collision:

The big thing to note here is that the right ball didn't just "bounce off" the left one immediately. It made contact, and then sped up.

Nothing does an instant acceleration like that. They have to take some non-zero amount of time to accelerate.

Let's look at another one:

In the simulations here and in the next couple lessons, objects with larger mass will be physically larger. That's not very physically accurate. It's just a visual thing.

In this simulation, the left ball has twice the mass as the right one.

In the simulation loop above, mass comes into play only in one spot, on line 2, dvel = force / mass * dt. The effect of mass on the simulation is to lessen the effect of force on an object.

For example, in this simulation the larger mass object is able to "barrel through" the smaller one, because its dvel is half as large as the right object's. So it's velocity reacts to the same force at a slower rate.

Here's another example, but where the ball with the larger mass is on the right:

Here a similar thing happens. The two balls are feeling equal and opposite forces, but the right ball, due to its larger mass, reacts slower. So the left ball ends up slowing, stopping, and actually reversing direction before the right ball speeds up enough to move out of the way.

Changelog:

Next Chapter: Collisions

© by Daniel Taylor