Search Unity

What variables should be persisted on a RigidBody if I'm trying to create a save game functionality?

Discussion in 'Physics' started by icamefromspace, Mar 15, 2018.

  1. icamefromspace

    icamefromspace

    Joined:
    Feb 27, 2018
    Posts:
    1
    I have a game with physics objects. when I save and load the game I want the rigid body to return to what it was doing.

    If I just save the values of

    rigidBody.velocity
    rigidBody.angularVelocity

    and then reassign them on game load, would that return the rigid body to its previous game state?
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Yes, that works. I do the same for replay & continue functionality.
     
    chaosmaker likes this.
  3. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    220
    i am having some trouble saving and loading a "physics" world. When i load the world back in (a stack of boxes for example) they get pushed up slightly like a spring.... due to this small force eventually everything crumbles.
    i am trying to save and load a world in the exact state it was left but its not really working...

    so far i am saving, rigid:position, rigid:rotation, rigid:angularVelocity, rigid:velocity

    Is there anything else that needs to be saved? contact points?
    any other ideas?
    thanks!
     
  4. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    467
    From what I understand, PhysX has an internal cache that holds more information than just position, velocity and angular velocity of the bodies. Maybe something like the previous position and more, idk. I doubt that there is a way to access, serialize and deserialize this cache in C#, so if that’s what’s necessary, you’re out of luck with PhysX (imho).

    You could try to also save the IsSleeping flag and put the rigidbody to sleep if so. Maybe that helps.

    Another way is to use Unity Physics. From what I know, Unity Physics is stateless, meaning that position, velocity and angular velocity is all you need to fully describe the scene.
     
    argibaltzi and Edy like this.
  5. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    220
    i guess its time to switch physics, thank you tjmaul!