Basics of Physics Simulations
March 17, 2025
The Purpose
As I mentioned in one of my articles that the question "why" could be utterly meaningless outside of human context, i.e. we might never unravel the mystery of why the universe exists. What we can do however is, observe the laws and patterns that universe seems to follow. That's the only thing we can do, both at micro and macro levels. Observe the rules and make predictions.
🙶The existence is not a mystery to solve, but a reality to experience.🙷
- Someone, I don't remember
Well, the conclusion is that I have to learn what I can learn. What better way to learn the rules and laws than to create a world with your own laws that reflect the laws in real world as accurately as possible. Fortunately, we have machines that allow us to do just that. They work on descrete points despite universe seemingly being continuous (is it?) but we have to work with what we have been given. It kind of makes you feel like a God. Yes, I have narcisistic traits.
The Attempt
Now we have the reason to justify building a physics simulator. "Physics Simulator" is a big word because I know nothing significant about the subject, very little if any, but hey learning is what we are here for. We will learn one rule at a time and play god by simulating it in our small world. All we need is a screen and ability to manipulate every pixel there is on the screen, i.e. change the color of pixels. That's all we need to do.
Here's an example. How do you create an illusion of motion in a straight line? Assuming a screen flickers 60 times in a second, it could be something like:
- Turn all the pixels black execpt the one at (x,y) (that's the special pixel we want to move)
- On the next flicker (2nd), Turn all the pixels black except the one at (x+1, y)
- On the next flicker (3rd), Turn all the pixels black except the one at (x+2, y)
- ... and so on
Do you notice the movement? The special pixel has moved 60 pixels to the right. The flicker is so fast that it creates the illusion of movement. The pixel is not actually moving. This epiphany is what I needed to actually get excited about the whole simulation thing.
So, what could be the simplest physics rule that we can simulate? How about a simple object falling under gravity in an empty space?
But before we do that, we need to understand an important concept: Fixed Time Stepping. We would not need this if every game loop update (flicker) occurred after a fixed time delta. The game loop update can be delayed due to CPU/GPU load fluctuations causing physics updates - intended for single frame - to occur at irregular interval; and this would be a recipe for inaccurate simulation.
Fixed Time Delta
...
An Object Falling Under Gravity
...
Tags: physics simulation programming canvas