# Determinism with complex collider

Hi guys, we need help!

We are currently developing a game with time loops, where clones of yourself are executing the actions you have done in the previous loops.
We need to be determinstic, it seems to work with primitive colliders, with convex mesh colliders sometimes, but not with child colliders. The values are close, but not exactly the same.
We enabled the Enhanced Determinism flag in the physics settings and disabled Auto Simulate.

We tried to isolate the problem in a blank project, and came up with this:

public class DeterminismTest : MonoBehaviour
{
public int iterations = 1;
public int LogTick = 100;
private int currTick;

private Vector3 startPos;
private Quaternion startRot;

private Rigidbody rb;
private Vector3 rb_startPos;
private Quaternion rb_startRot;

private void Awake()
{
rb = GetComponent<Rigidbody>();
startPos = transform.position;
startRot = transform.rotation;
rb_startPos = rb.position;
rb_startRot = rb.rotation;
ManualReset();
StartCoroutine(CoManualFixedUpdate());
}

private IEnumerator CoManualFixedUpdate()
{
while (true)
{
Physics.Simulate(Time.fixedDeltaTime);
currTick++;
if (currTick == LogTick)
{
currTick = 0;
Debug.Log(\$"Position: {ToStringManyDecimals(transform.position)}");
ManualReset();
}
yield return new WaitForFixedUpdate();
}
}

private void ManualReset()
{
for (int i = 0; i < iterations; i++)
{
transform.position = startPos;
transform.rotation = startRot;
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
rb.position = rb_startPos;
rb.rotation = rb_startRot;
}
}

private string ToStringManyDecimals(Vector3 v3)
{
return \$"{v3.x:F10}, {v3.y:F10}, {v3.z:F10}";
}

private void OnDestroy()
{
StopAllCoroutines();
}
}

We added this MonoBehaviour to an object with a collider and a rigidbody with the following outcomes:

• Primitive Collider( Box, Sphere): Works like a charm
• Convex Mesh Collider: If the object is reset multiple times(iterations > 1) it works?!
• Collider on parent and child: Does not work. Sometimes it is only the 2nd loop thatâ€™s different and everything from there is the same, sometimes there simple are multiple different results

Note: If we reload the scene, the outcomes are exactly the same. So it seems if a combination of collider does not work, at least it does so consistently.
In the attached project you should be able to observe that each loop is different until at some point it starts to repeat (best observable with the â€śCollapseâ€ť option from the console enabled).
If you want to modify the attached test scene be aware that you need to delete all other instances of â€śDeterminismTestâ€ť in the scene, otherwise Physics.Simulate() is called multiple times.
Another problem which seems to destroy the determinism even for simple colliders is setting the rigidbody to kinematic for a certain amount of time. E.g. set kinematic in tick 40 to true and in tick 80 to false.

Is there a possibility to achieve this kind of determinism with child colliders?

Tested in unity version 2019.4.4f1
Any hint or input is very much appreciated!

1 Like

As for what Iâ€™ve tested, resetting a rigidbody completely involves keeping it kinematic for a couple of frames and repositioning it while itâ€™s kinematic. Hereâ€™s my code for a complete rigidbody reset:

public class ResetRigidbody : MonoBehaviour
{
public Transform target;

IEnumerator ResetInternal (Vector3 position, Quaternion rotation)
{
if (target != null)
{
Rigidbody rb = target.GetComponent<Rigidbody>();

if (rb != null && !rb.isKinematic)
{
rb.isKinematic = true;
yield return new WaitForFixedUpdate();
rb.position = position;
rb.rotation = rotation;
yield return new WaitForFixedUpdate();
rb.isKinematic = false;
}
else
{
target.position = position;
target.rotation = rotation;
}
}

yield return null;
}

public void ResetRigidbody (Vector3 position, Quaternion rotation)
{
StartCoroutine(RespawnInternal(position, rotation));
}
}

Whenever you call ResetRigidbody(position, rotation) in that component the targetâ€™s rigidbody will be reset completely to the exact given position and rotation.

If I were you I would just record positions and interpolate between them without any physics involved.
Do you have to interact with this objects that are repeating actions?

Thank you very much Edy! Iâ€™m also working on the game so Iâ€™m going to reply here.
I tried your reset, but for me, it exhibits the same behavior as our previous reset where it works with boxcolliders etc. but more complex ones do not work.
This is my implementation now:

public class DeterminismTest : MonoBehaviour
{
public int iterations = 1;
public int LogTick = 100;
private int currTick;

private Rigidbody rb;
private Vector3 rb_startPos;
private Quaternion rb_startRot;

private void Start()
{
rb = GetComponent<Rigidbody>();
rb_startPos = rb.position;
rb_startRot = rb.rotation;
StartCoroutine(CoManualFixedUpdate());
}

private IEnumerator ResetRigidbody(Rigidbody rigidbody, Vector3 pos, Quaternion rot)
{
rigidbody.isKinematic = true;
yield return new WaitForFixedUpdate();
rigidbody.position = pos;
rigidbody.rotation = rot;
yield return new WaitForFixedUpdate();
rigidbody.isKinematic = false;
yield return new WaitForFixedUpdate();
}

private IEnumerator CoManualFixedUpdate()
{
while (true)
{
Physics.Simulate(Time.fixedDeltaTime);
currTick++;
if (currTick == LogTick)
{
currTick = 0;
Debug.Log(\$"Position: {ToStringManyDecimals(rb.position)}");
yield return StartCoroutine(ResetRigidbody(rb, rb_startPos, rb_startRot));
}
yield return new WaitForFixedUpdate();
}
}

private string ToStringManyDecimals(Vector3 v3)
{
return \$"{v3.x:F10}, {v3.y:F10}, {v3.z:F10}";
}
}

Yes, unfortunately, we want to interact with our replays. We tried recording all objects, but it has a few annoying edge cases in our game and it takes away some interactivity.
This is some gameplay of our game

Thank you Edy!
I tried your code and it does exactly what you said: it will reset the rigidbody to the exact given position and rotation. Unfortunately this was not the original problem, the problem was that the position at the end of a loop (e.g. after 100 ticks) differs from time to time, even though the start position and rotation were the same.

Thanks for the suggestion koirat!
In each loop, we add a clone of the player. The player should be able to interact with these clones (take away their guns, etc.), so the interaction is a must have.

So we experimented for a really long time and I think we got a solution that is somewhat working. There is still one problem, Iâ€™ll touch on that later.

Turns out Rigidbody resetting has to be done differently, as outlined here for 2D Physics.
For anyone that also needs a solution, here are our steps. They are almost identical to the 2D steps.
I attached our project if you want to look at the code.

Project Settings: Enhanced Determinism: On, Auto Simulation: Off.

• On Start we record the startposition, rotation, velocity and angularVelocity of our Rigidbodies.
• Then the Rigidbody reset:
• Activate all Rigidbodies and Colliders
• Set all Rigidbodies to kinematic.
• Apply the recorded start values
• Set all Rigidbodies to not kinematic
• Deactivate GameObjects with Colliders and Rigidbodies again in reverse order (reverse order not needed I think)
• Simulate one physics frame (call Physics.Simulate())
• Then activate all GameObjects with Colliders and Rigidbodies again
• Call WakeUp on all Rigidbodies
• Then start a Coroutine what yields WaitForFixedUpdate and calls Physics.Simulate for the simulation loop.
• After enough time/ticks have passed, deactivate all GameObjects with Colliders and Rigidbodies in reverse order.
• Simulate one physics frame.
• Then call the Rigidbody reset again.

Other learnings:
We had to activate/deactivate not only Rigidbodies but also Colliders without Rigidbodies.
If you have child colliders, in addition to the parent the children also have to be activated/deactivated in the right order. (SetActivateRecursively and Inactive do this in our code)
In our tests, the GameObjects didnâ€™t have to be inactivate at scene start.
Every collision detection mode seems to work as far as we tested.

We still got one problem:
If we set a Rigidbody to kinematic during the simulation the determinism breaks. Even though it happens in the same tick every run.
e.g.:

if (currTick == 40)
rigidbodies.First().isKinematic = true;

if (currTick == 80)
rigidbodies.First().isKinematic = false;

My guess is that this messes up the internal simulation order.
Does anybody know if this is possible or how to do it?

I think we can find a workaround for it, but it would be great if we could just set our Rigidbodies to kinematic during gameplay when we need it.

1 Like