.. _basic_sim: ====================== Basic simulation ====================== Loading ========================== To perform basic simulation with MuJoCo, create a :docs-rs:`~mujoco_rs::wrappers::mj_model::MjModel` struct by calling one of the following methods: - :docs-rs:`~~mujoco_rs::wrappers::mj_model::MjModel::from_xml` (loads XML from a file), - :docs-rs:`~~mujoco_rs::wrappers::mj_model::MjModel::from_xml_vfs` (loads XML from a file on a virtual file system), - :docs-rs:`~~mujoco_rs::wrappers::mj_model::MjModel::from_xml_string` (loads XML from a model defined in a string in memory), - :docs-rs:`~~mujoco_rs::wrappers::mj_model::MjModel::from_buffer` (loads a compiled model from a buffer). For example: .. code-block:: rust fn main() { let model = MjModel::from_xml("model.xml").expect("could not load the model"); } :docs-rs:`~mujoco_rs::wrappers::mj_model::MjModel` represents the compiled moded from an XML file and contains everything from basic metadata to physics parameters. For managing actual simulation state, :docs-rs:`~mujoco_rs::wrappers::mj_data::MjData` is used. It can be created either through the :docs-rs:`~~mujoco_rs::wrappers::mj_data::MjData::new` method or through the :docs-rs:`~~mujoco_rs::wrappers::mj_model::MjModel::make_data` method. For example: .. code-block:: rust :emphasize-lines: 3 fn main() { let model = MjModel::from_xml("model.xml").expect("could not load the model"); let mut data = model.make_data(); // or MjData::new(&model); } Running ==================== Then to run/step the simulation, just call the :docs-rs:`~~mujoco_rs::wrappers::mj_data::MjData::step` method like so: .. code-block:: rust :emphasize-lines: 5 fn main() { let model = MjModel::from_xml("model.xml").expect("could not load the model"); let mut data = model.make_data(); // or MjData::new(&model); loop { data.step(); } } The method :docs-rs:`~~mujoco_rs::wrappers::mj_data::MjData::step` is just a wrapper around the :docs-rs:`~~mujoco_rs::mujoco_c::mj_step` FFI function. Similarly, :docs-rs:`~~mujoco_rs::wrappers::mj_data::MjData::step1` and :docs-rs:`~~mujoco_rs::wrappers::mj_data::MjData::step2` wrap :docs-rs:`~~mujoco_rs::mujoco_c::mj_step1` and :docs-rs:`~~mujoco_rs::mujoco_c::mj_step2`, respectively. For more information about specific MuJoCo functions, see the `MuJoCo documentation `_. Realtime ---------------------- The example above runs the simulation as fast as possible. To slow it down, you can either add a call to `std::thread::sleep `_ for an approximate delay, or use `std::time::Instant `_ with `Instant::elapsed `_ for precise timing. .. code-block:: rust :emphasize-lines: 7, 10, 11 use std::time::Duration; use std::thread; fn main() { let model = MjModel::from_xml("model.xml").expect("could not load the model"); let mut data = model.make_data(); // or MjData::new(&model); let timestep = model.opt().timestep; loop { data.step(); /* Approximate-time delay */ thread::sleep(Duration::from_secs_f64(timestep)) } }