Skip to main content
Methods for modifying model parameters at runtime. Useful for domain randomization in RL training.

setGravity(g)

Change the gravity vector.
api.setGravity([0, 0, -9.81]);    // Earth gravity (Z-up)
api.setGravity([0, 0, -1.62]);    // Moon gravity
api.setGravity([0, 0, 0]);        // Zero gravity
g
[number, number, number]
required
Gravity vector in m/s^2.

setTimestep(dt)

Change the simulation timestep.
api.setTimestep(0.001);  // 1ms (more stable, slower)
api.setTimestep(0.005);  // 5ms (faster, less stable)
dt
number
required
Timestep in seconds.
Changing the timestep affects simulation stability. Smaller values are more stable but slower. Large values may cause the simulation to explode.

setBodyMass(name, mass)

Change a body’s mass.
api.setBodyMass("block", 0.5);   // 500g
api.setBodyMass("block", 2.0);   // 2kg
name
string
required
Body name.
mass
number
required
New mass in kg.

setGeomFriction(name, friction)

Change a geom’s friction coefficients.
api.setGeomFriction("floor", [1.0, 0.005, 0.0001]);  // High friction
api.setGeomFriction("ice", [0.1, 0.001, 0.00001]);    // Low friction
name
string
required
Geom name.
friction
[number, number, number]
required
MuJoCo friction coefficients: [slide, torsional, rolling].

setGeomSize(name, size)

Change a geom’s size.
api.setGeomSize("box", [0.05, 0.05, 0.05]);  // 10cm cube
api.setGeomSize("ball", [0.03, 0, 0]);        // 3cm radius sphere
name
string
required
Geom name.
size
[number, number, number]
required
New size (semantics depend on geom type — see MuJoCo docs).

Example: Domain Randomization

function DomainRandomizer() {
  const { api } = useMujoco();

  function randomize() {
    // Randomize gravity
    const gz = -9.81 + (Math.random() - 0.5) * 2;
    api.setGravity([0, 0, gz]);

    // Randomize object mass
    api.setBodyMass("cube", 0.05 + Math.random() * 0.2);

    // Randomize floor friction
    const friction = 0.5 + Math.random() * 1.0;
    api.setGeomFriction("floor", [friction, 0.005, 0.0001]);

    // Reset to apply changes
    api.reset();
  }

  return <button onClick={randomize}>Randomize</button>;
}