Leon Milewski

Custom Physics Engine

Solo Developer

Soooo… Physics… A complicated subject, and one that not many game devs dive into! These days, we have game engines that handle it for us, and it’s all quite convenient! 😊 However, how do these engines actually simulate physics? Well, I decided to find out! A quick little week-long adventure, right?

In Unreal Engine 5 (UE5, C++):

In Unreal Editor for Fortnite (UEFN, Verse):

Part 1: Research

Some light bedtime reading

Wow! Check out all this documentation! 🤯

Basically, physics engines are an infinite loop with the following parts:

  • Step
  • Broadphase
  • Narrowphase
  • Collision resolution
  • Constraints resolution
  • Repeat

1: Step

Imagine that all physics move in “steps” of time. For example, if I had a cube moving at 1m/s, then called Step(1.0), the cube would move 1m. To move the entire world in real time, we’ll measure delta-time (dt). This is the time in-between the previous iteration and this iteration. Then, we’ll call Step(dt) on all physics objects to progress our world’s state by dt. Note that this progression can causes invalid states, such as objects being inside each other. My player might be phasing through the ground! We should detect and fix that.

2: Broadphase

Now that our objects have moved, we need to figure out which objects are colliding (such as my player and the ground 😋)! While we do have some real neato algorithms to detect collisions, they’re quite expensive if we perform them on every pair of objects. Hence, we’ll do a simpler test first. We’ll ask the question: Is it possible that these two objects are colliding? If it’s obvious that they couldn’t, we’ll skip that pair. However, if it’s feasible, we’ll perform the more expensive test. Our cheap test of choice? Axis-aligned bounding boxes (AABB)! Basically, draw 2 unrotated rectangles containing each object. If the rectangles overlap, it’s possible the objects themselves do too! Here’s a few fun broadphase algorithms:

Exponential: Perform AABB on every pair. It’s better than no broadphase at all but far from optimal.

Spatial Hashing: Evenly divide the worldspace into a grid. Assuming objects aren’t physically large enough to be within more than 4 grid spaces at the same time (8 in 3D), we can “hash” grid spaces to quickly identify possibly-adjacent objects. Now that we can quickly get objects within a grid space, we’ll check for overlaps in a grid space and all adjacent grid spaces using AABB tests. Besides hash collisions, objects in non-adjacent grid spaces are ignored, reducing the number of AABB tests.

  • What is hashing? It’s the idea of taking data and converting it into a “hash” number. For example, how can we hash the data of a position: (10,32)? Well, we could do x*2+y*3, getting 116. Ta-da! We just converted an arbitrary position into a single number! Note that it’s now possible to have a “hash collision.” This is where two different pieces of data result in the same hash. For example, (1,38) also results in a hash of 116, as 1*2+38*3=116.
  • Why do we want to use hashes? Couldn’t we store objects within the same grid space, then query from that? Well, that won’t scale well. For example, we’ll make a list of objects for all the objects in the grid space (0,0), another for (1,0), another for (2,0), …, and finally another for (999,0). Then another for (999,1), … See why that won’t work? In large worlds, this is too much to remember. Using hashes, we can quickly query objects that are within a grid space without having a separate container for each grid space. We’ll first decide how many “buckets” we want, being the number of lists we’ll have. For example, we could have 10 buckets, one for each digit 0 through 9. Hashes that end in a digit have that physics object put into that bucket. In practice, we’d use the modulo operator to expand this to any number of buckets. While we’ll have hash collisions, this won’t result in incorrect behavior; we’d just be performing unnecessary AABB checks on non-near objects. Increase the number of buckets to reduce the probability of this occurring. 

Bounding Volume Hierarchies

//TODO

Ah, sorry! I’ll finish this once I find more time!

Feel free to reach out through LinkedIn, though! Physics is a fun topic, and it’s a great conversation starter 🙂

Part 2: TODO

TODO

Want to read more?

My other blog posts: 😄