# How to achieve deterministic logic

I want to control the physics of an actor with custom logic that reacts to its surroundings. Basically, the actor ought to avoid the ground and walls and adjust its velocity and rotation.

I am currently solving this by calculating the distance with rays and applying a respective force and/or rotation each frame. However, this only works if a constant high framerate is provided. With a low framerate, the actor may move too far between frames making it unable to adjust. See this illustration for clarification.

Is there a way to get a more deterministic behavior? I have thought of two possible solutions - one would be to stop using the built in physics (velocity) and setting the position and rotation manually, with substepped logic - the other one would be to try to react to collisions and guess how the actor should have acted in the first place. Both don’t sound perfect by all means, so I’m hoping for an existing better solution.

This question on the UE4 answerhub goes into some detail on the issue, How to make physics forces independent of frame rate? - World Creation - Epic Developer Community Forums

The gist of the discussion is you need to take several smaller timesteps per frame(sub-stepping) to minimize the scene change between any physics update, or you need to decouple your physics from the framerate entirely. By going to fixed time-step, your simulation is very predictable(100% deterministic) because your simulation makes identical steps which are based on accumulated delta time rather than the framerate.

Physics sub-stepping: Physics Sub-Stepping - Unreal Engine

Fixed time-step: Fix Your Timestep! | Gaffer On Games

I knew of substepping, however, there is one quote standing out:

This would be exactly what I need - I didn’t think it was possible, since I received no answers regarding this in AnswerHub and also didn’t find anything in the search engine.

You see, it’s important that I get to run the calculations constantly. To specify the illustration above: The actor moves forward at a constant velocity. Only substepping the physics won’t help, the actor would still only move straightly forward - the velocity and rotation though should be constantly changed depending on the distance measured by the LineTraces. Recalculating this on each substep would be ideal.

I’ve implemented neither of these solutions for myself, so I can’t really say how difficult it would be. Once you have control over the physics tick, you could probably implement either option. I guess the question is, do you need truly deterministic physics or just accuracy? Accuracy comes with running the simulation more often, while determinism is a result of providing the exact same values every run ie. invariant time-step.

It’s probably more accuracy than real determinism that I’m after, accuracy that doesn’t degrade with lower framerates. I realize I’d get that only with either running my own sub-stepped simulation and bypassing the integrated physics, or running the logic on each integrated-physics sub-step if such thing is possible.

The latter apparently has been utilized in the vehicle demo. I’ll look into it, but if anyone already knows how to achieve it, I’d be very grateful for help.