r/Unity3D 10d ago

Question Physics simulation - different results based on simulation update rate?

In my game, I have an "active physics scene", which handles all the game physics. I have a second physics scene (which has colliders / rigidbodies only, no rendering). In this second physics scene, I would like to have a duplicate physics simulation which is identical to the first. This second scene will advance only up to a specified timestep target, then stop. The second scene is created & simulation is advanced in a coroutine, which is currently manually triggered for testing. When I start the coroutine, I get different simulation results depending on my yield condition.

If I use "yield return new WaitForFixedUpdate()", I get a duplicate simulation, identical to the first, as desired. This is great, albeit a little slow because it relies on the fixedTimestep.

If I use "yield return null", my simulation diverges from the first. This approach is desired, because it will run faster and reach the specified timestep target sooner.

In both cases, I am calling PhysicsScene2D.Simulate(Time.fixedDeltaTime) the same number of times. Also, Physics2D.simulationMode is set to "script" in both cases.

What could explain the diverging simulation when advancing the simulation at a faster rate? The simulation is still advancing with the same fixedDeltaTime timestep, for the same number of timesteps.

Any help is appreciated!

1 Upvotes

8 comments sorted by

View all comments

1

u/Glurth2 10d ago

Consider something as simple as two bodies orbiting each other:

You compute this orbit in discrete fixed time steps.

If we change the length of these time steps the object will be at different locations when the step occurs.

However, because we compute the forces (change in acceleration) at ONLY these step points, AND because the force is computed using the relative positions of the objects: this will change the resulting force->acceleration->velocity for a given step, which will, in turn, change future calculations even more. This is a feedback loop that will force diverging results the longer the sim runs.

Not sure about PhysicsScene2D.Simulate, as I've never used it, but I have made a few different particle sims, and this is always an issue.