How do video games simulate the physics of the real world so well? (e.g. gravity)

3.56K views
0

How do video games simulate the physics of the real world so well? (e.g. gravity)

In: Mathematics

Math. Gravity is a constant force, and approximately 9.81m/s for us, and using an equation (0.5) * acceleration * time squared + initial velocity * time, we can get the current velocity which can be then turned into time and distance.

because we have a very good grasp at real world physics. and it is rather simple to simulate real world physics into software.

Especially video games, where high accuracy is not important.

Gravity is easy to simulate. The physics for projectiles and falling objects is easily teachable with only high school algebra. Another handy thing is that simulating physics that require calculus tends to be easy because the way that game engines work is to calculate things in chunks that take a short period of time.

If I wanted to code a rock in a game to have gravity, it’d just go in the loop that executed every time physics is calculated. So the loop for my rock might look like: (apologies if formatting is bad, on mobile atm)

delta is a variable that is the amount of time that passes between every time the loop executes, basically how long it is between every frame of the game.

g = 9.8
function physics_process(delta):
self.velocity.y = self.velocity.y – g*delta
self.position.y = self.position.y + self.velocity.y*delta

That’s it. The first line just says that the force of gravity changes the velocity, the second line says to subtract the velocity from the vertical position. The deltas just make makes sure that it’s using the right amount of acceleration and velocity for how long it takes between each frame.

Even if you don’t understand the code, the point is that gravity is simple enough to work with that it only takes a few lines to code.