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

5.59K views

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

In: Mathematics

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.