2D Physics Engine Tutorial – Part 1

In this article series the focus will be on identifying the different parts of a physics engine and then how we can implement those parts as simple as possible. There are tons of 2D and 3D engines available on the internet – this is not going to be a contestant to any of those engines, it will purely function as an educational physics engine to better grasp the concepts behind it.

What does a physics engine do

Almost all games need some way of detecting if two objects collide and respond using the proper reaction. It might be Mario standing on top of a platform or Max Payne shooting a bad guy – they both need collision detection and physics.

Wikipedia definition
A physics engine is a computer program that simulates physics models, using variables such as mass, velocity, friction, and wind resistance. It can simulate and predict effects under different conditions that would approximate what happens in real life or in a fantasy world. Its main uses are in scientific simulation and in video games.

Physics engines can be used for so much more than just fun and games:

To us it is naturally that if we toss a ball against a wall, it will hit the wall and bounce back. In the computer world all this has to be simulated.

Types of physics engines

There are two different kinds of physics engines. One of them is used for soft body (deformable) objects and the other is used for rigid body objects (hard/non-deformable)

Soft body physics

Soft body engines are used for deformable physics such as cloth, rubber, jelly or balloons. You can enlarge, rip or puncture a soft body in any way you want. There are both 3D soft body engines and 2D soft body engines. Most of them define their shapes using triangles (or particles) that can change properties (side lengths and angles) and thus deform the body that contains the triangle.

Soft body dynamics are hard to work with because of the deformations applied to a body. If a body needs to try and keep its original shape, all the deformations will have to be kept track off and slowly be resolved to gain its original shape.

Rigid body physics

Rigid body engines are used for non-deformable physics such as spacecrafts, buildings, terrain, player characters and more. They can’t be deformed in any way and only support linear (movement) and angular (rotation)  motion.

Rigid body engines are a lot easier to work with compared to soft body physics. You only have to keep track of movement on the x, y and z-axis and rotation around the centroid of the object.

Play Doh (Soft) and a metal box (Rigid):

Real-time vs. High-precision

How all this is calculated depends on the type of engine used. There are generally two different types of engines:

Real-Time

Real-time engines are typically used in games and other real time applications. They are used to simulate the game environment, vehicle dynamics, player interactions and more. Real-time engines are usually more focused on performance than accuracy – when a tree falls on a vehicle, we don’t need to know where every single fiber of the tree hits the vehicle with a accuracy of 0.01 mm.

They are identified by the following characterizes:

• Iterative algorithms
• Low accuracy
• High number of concurrent objects
• Game oriented tools
High-Precision

This kind of engine is mostly used in scientific applications and animated movies. They are used to simulate weather, train collisions, explosions and space shuttle trajectory. They can’t be used for real-time applications because they are too expensive in their computations.

They are identified by the following characterizes:

• Single pass algorithms
• High accuracy and precision
• Low number of concurrent objects
• Scientific application tools