DEV Community

Cover image for 🏃Running Unity Physics Substepping
Mateusz_Borowiecki
Mateusz_Borowiecki

Posted on

🏃Running Unity Physics Substepping

Recently I developed the physics substepping solution for Unity Game Engine and today want to share it with you.
But before we begin, let's answer some questions and clarify things.

What is physics substepping?

Physics substepping is the process of doing many calculations in one physics frame.
This could be the definition I think of Physics Substepping. But if we want to explain it to a child, we can say we just run the function multiple times at one time.

Why do we need physics substepping?

I don't think we need the physics substepping every time we do something physics-related in Unity.
But when I was developing a custom vehicle physics I approached the wall of having too much difference in parameters between each step of physics.

I am sure Unity built-in physics works most of the time, but when we talk about wheel angular velocity it is not accurate at all if we don't set the Fixed Timestep to something like 1/500.

When we do this, there is another problem. The whole physics engine simulation gets updated too many times including collisions, accelerations, etc. But we still need to update some parts of our code many times. Here the physics substepping starts being helpful.

How physics substepping works?

To be honest it is not that advanced, at least in the form I coded it.

  1. Let's start at the program start. Let's say the time is 0 seconds at that moment. Our substepping is doing a step every 0.5 seconds.
  2. Let's say the engine simulation runs every 1 second, so we get 1 delta time in tick at a time of 1 second. So this is the point where we calculate physics. But we need to calculate it twice because 1 second of delta / 0.5 seconds of desired delta is 2.
  3. In the first calculation because of parameters, we add 5 units of force. In the second we do not add any force because the speed should be already equal. So the force added in this time should be 5 right? No
  4. If we would add 5 units of force the engine adds 5 units for 1 second.

Remember that acceleration based on force is depending on time acceleration = force / mass, and velocity_added = acceleration * delta
What we need to do is to divide the substepping force by substeps elapsed.

And that's it. Now we know how substepping works.

But wait. In the second substepping tick how do we know, we don't need to add force? The answer is simple. We need to estimate.

All substepping is based on estimations. I am no expert on this, I thought of it during 30 minutes of drive home and implemented it in 3 hours and this is just explained as I understand it.

How do we estimate velocity?

It is really simple. Earlier I wrote the equation
velocity = velocity + acceleration * delta_time
we know the delta_time, because it is 0.5 seconds of our substepping delta time.

But what is the acceleration?
acceleration = force / mass
We just take the force we applied and divide it by mass. Easy? Sure it now is.

Summary

More topics need explanation, but this is just an overview. If you want to try it I created public package which can be accessed on my github account in One, ready to use repo.

Hope you understand my point of view and learned something new today. Comments on this and new issues on github are welcome. See ya 👋

Top comments (0)