Skip to main content

Serialization

When the serde-serialize feature of Rapier is enabled, most data structures of Rapier become serializable using serde. Serialization can be useful to save the complete state of the simulation, e.g., to send it over the network, or to restore this state later. The complete state of the simulation is serialized by serializing its physics world as a whole, as shown by the following example using bincode as the serialization format. Note that if you don't use the physics world, every structure described in the simulation structures page must be serialized instead, except for the PhysicsPipeline and the CollisionPipeline which don't hold any useful state:

// Serialize the whole physics world.
let serialized = bincode::serde::encode_to_vec(&world, bincode::config::standard()).unwrap();
// Deserialize it.
let (mut deserialized, _): (PhysicsWorld, usize) =
bincode::serde::decode_from_slice(&serialized, bincode::config::standard()).unwrap();
// The simulation can continue using the deserialized world.
deserialized.step();
info

If the enhanced-determinism feature of Rapier is enabled, and if your platform fulfills the required determinism requirements, then you have the guarantee that running the exact same simulation on two different machine will result in the exact same byte vectors if the physics state is serialized on both machines after the same number of timesteps.