Search Unity

Unable to capture/restore full state of Rigidbody precisely

Discussion in 'Physics' started by TomasRiker, Jun 12, 2019.

  1. TomasRiker

    TomasRiker

    Joined:
    Jan 26, 2012
    Posts:
    48
    Dear all,

    Context (not really relevant to the problem, just in case you're wondering):
    I'm trying to implement a networked multi-user scenario that includes physics simulation. Each client simulates the world locally, sends its input to the server and receives the inputs of other clients. Once the input of the other clients arrives, it has to roll back in time to a previous state (because it used prediction to hide latency) and simulate again with the correct inputs. The idea is to send only the inputs over the network instead of having to synchronize the internal state of the simulation.

    For that to work, I need to be able to fully capture and restore the physics engine's internal state. Specifically, I only care about the position, rotation, velocity and angular velocity of each Rigidbody.

    Getting and setting the velocity and angular velocity works fine.

    However, I'm unable to set a Rigidbody's position and rotation precisely. Something strange happens here: Apparently, after setting
    rigidbody.position = x
    , the position isn't precisely
    x
    (same for rotation). Instead, there's a tiny difference that's only notable when you use
    Equals
    and not the overloaded
    ==
    operator. That tiny difference quickly becomes a huge difference when collisions take place, and thus the clients' physics simulations diverge. :(

    An important thing to notice: It's most noticeable when the Rigidbody has at least 2 colliders. With only one collider, it seems to work fine as long as there are no collisions. While collisions take place (e. g. object is lying on the floor), there's always trouble.

    I already tried setting
    transform.position
    and
    transform.rotation
    followed by a call to
    Physics.SyncTransforms()
    , but to no avail.

    I'd be grateful for any suggestions.
     
    Last edited: Jun 12, 2019
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    If its a floating point error you can use doubles to store the value, maybe an int with a divisor?
     
  3. hugeandy

    hugeandy

    Joined:
    Nov 2, 2016
    Posts:
    131
    Hey,

    Did you make any progress with this? Especially interested in this part, as that's exactly what I'm dealing with at the moment:

    I'm in the exact same context as you mention.

    Cheers,
    Andy