ffi_attributes/
ffi_attributes.rs

1//! An example on how to use ffi attributes.
2//! While most structs in this library are direct FFI structs to the MuJoCo,
3//! some have to be wrapped for safety reasons.
4//! The wrapped structs contain a `ffi()` method and a `ffi_mut()` method, which return
5//! a reference to the wrapped FFI struct.
6
7
8use std::time::Duration;
9
10use mujoco_rs::viewer::MjViewer;
11use mujoco_rs::prelude::*;
12
13
14const EXAMPLE_MODEL: &str = "
15<mujoco>
16  <worldbody>
17    <light ambient=\"0.2 0.2 0.2\"/>
18    <body name=\"ball\">
19        <geom name=\"green_sphere\" pos=\".2 .2 .2\" size=\".1\" rgba=\"0 1 0 1\" mass=\"0.1\" solref=\"0.004 1\"/>
20        <joint type=\"free\"/>
21    </body>
22
23    <geom name=\"floor\" type=\"plane\" size=\"10 10 1\" euler=\"5 0 0\" solref=\"0.004 1\"/>
24
25  </worldbody>
26</mujoco>
27";
28
29fn main() {
30    let model = MjModel::from_xml_string(EXAMPLE_MODEL).expect("could not load the model");
31
32    /******************************************/
33    /* Access a FFI attribute */
34    let timestep = model.ffi().opt.timestep;
35    /******************************************/
36
37    let mut data = model.make_data();  // or MjData::new(&model);
38    let mut viewer = MjViewer::launch_passive(&model, 100)
39        .expect("could not launch the viewer");
40
41    while viewer.running() {
42        viewer.sync(&mut data);
43        data.step();
44        std::thread::sleep(Duration::from_secs_f64(timestep));
45    }
46}