MJCF (the MuJoCo XML format) is the native modeling language of the MuJoCo physics engine: a single XML document that describes bodies, joints, geometry, actuators, sensors and solver settings for a complete physics simulation. In robotics simulation it plays the role that URDF plays in ROS tooling, but it was designed around a physics engine first, so it expresses contact parameters, tendons, equality constraints and numerical options that a pure kinematic description cannot hold.
An MJCF file is not executed directly. The MuJoCo compiler parses the XML and produces a compact, optimized mjModel structure, which the simulator then steps forward in time. Understanding this model-compile-simulate pipeline explains most of the format’s design choices, including its default classes and its automatic computation of inertia.
How an MJCF model is structured
An MJCF model is a tree of nested bodies inside a root element named mujoco, plus several global sections that configure the compiler, the physics options and the elements attached to that tree. The kinematic tree lives in `worldbody`: every body is declared inside its parent, and a joint declared in a body defines the motion of that body relative to its parent. This nesting replaces the explicit parent and child references used by URDF.
The top-level sections are optional and can appear in almost any order. The following table summarizes the sections a robotics engineer meets most often.
| Section | Purpose | Typical content |
|---|---|---|
| compiler | Controls how the XML is parsed | Angle units, mesh directory, inertia rules |
| option | Sets global physics parameters | Timestep, integrator, gravity, solver |
| default | Defines reusable attribute classes | Joint damping, geom friction, actuator gains |
| asset | Declares shared resources | Meshes, textures, materials, height fields |
| worldbody | Holds the kinematic tree | Bodies, joints, geoms, sites, cameras, lights |
| actuator | Maps controls to forces | Motors, position servos, muscles |
| sensor | Defines simulated measurements | Joint position, IMU, force, touch |
| equality | Adds extra constraints | Closed loops, welds, coupled joints |
Inside a body, geoms carry collision and visual geometry, sites mark named reference frames for sensors and tendons, and joints of type hinge, slide, ball or free define degrees of freedom. A body without any joint is rigidly welded to its parent, which is a frequent source of surprise for newcomers who expect a default floating base.
Physics and contact parameters in MJCF
MJCF exposes the numerical behavior of the solver directly in the model file, which is its most important difference from description formats built for visualization. The option element sets the timestep, with a default of 0.002 s, the integrator (Euler, RK4, implicit or implicitfast), the friction cone type and the constraint solver (Newton, CG or PGS).
MuJoCo models contact as a soft constraint rather than as a rigid impulse. Each geom pair can tune how stiff and how damped that constraint is, and the parameters below determine whether a gripper holds an object, slips or jitters.
| Attribute | Default | Effect on simulation |
|---|---|---|
| condim | 3 | Contact dimensionality: 1 frictionless, 3 sliding, 4 torsional, 6 rolling |
| friction | 1, 0.005, 0.0001 | Sliding, torsional and rolling coefficients |
| solref | 0.02, 1 | Time constant and damping ratio of the constraint |
| solimp | 0.9, 0.95, 0.001, 0.5, 2 | Impedance curve that controls softness versus penetration |
| margin | 0 | Distance at which a contact becomes active |
| contype and conaffinity | 1 and 1 | Bit masks that filter which geoms can collide |
A practical rule is that the solref time constant should be at least twice the timestep; shorter values make the constraint stiffer than the integrator can resolve and produce energy gain and chattering. Mass and inertia can be specified explicitly with an inertial element or computed from geom shapes with a default density of 1000 kg per cubic meter, which is convenient but often unrealistic for hollow robot links.
MJCF compared with URDF and SDFormat
MJCF, URDF and SDFormat all describe robots in XML, but they target different layers of the tool chain. URDF is the lingua franca of ROS for kinematics and visualization, SDFormat describes whole worlds for Gazebo, and MJCF describes a model for one specific engine with full control over its physics.
| Capability | MJCF | URDF | SDFormat |
|---|---|---|---|
| Kinematic structure | Nested bodies | Links and joints by reference | Links and joints by reference |
| Closed kinematic loops | Via equality constraints | Not supported | Supported in format, engine-dependent |
| Actuator models | Built in, several types | Only transmission tags | Through plugins |
| Tendons and muscles | Built in | Not supported | Not supported natively |
| Solver options in file | Yes | No | Partly, per engine |
| Multiple robots and world | Yes, one scene | One robot only | Yes, full worlds |
MuJoCo can load a URDF and convert it to MJCF, and saving the compiled model produces an equivalent MJCF file. The conversion is a good starting point, but engineers usually add contact exclusions, actuators and default classes by hand, because URDF carries none of that information. Macro tools such as Xacro have no direct role in MJCF, which instead relies on include files and default classes for reuse.
Default classes, assets and model reuse
Default classes are the main mechanism for keeping large MJCF files maintainable. A class sets attribute values for joints, geoms, actuators or other elements, and any element can reference it with the class attribute; nested classes inherit and override their parent values. The childclass attribute on a body applies a class to its whole subtree.
Typical practice is to define separate classes for visual geoms and collision geoms, so that detailed meshes are rendered while simplified primitives handle contact. Assets keep meshes and textures in one place, and the include element splits a model into files, for example one file per robot and one scene file that combines them.
- Use primitives for collision (capsules, boxes, spheres) wherever possible, because primitive contacts are faster and more stable than mesh contacts.
- Remember that mesh geoms collide as convex hulls, so concave parts such as a hollow cup need decomposition into several convex pieces.
- Set angle units explicitly in the compiler, because MJCF defaults to degrees while many converted models assume radians.
- Enable autolimits so that joint and actuator ranges activate limits without a separate flag.
Actuators, sensors and common modeling mistakes
MJCF actuators connect a control signal to a joint, tendon or site through a gain, a bias and an optional activation state. Shortcut elements such as motor, position and velocity expand into the general actuator with preset parameters, which makes it possible to model torque-controlled and servo-controlled robots without writing a controller in code.
Most failed simulations trace back to a small set of modeling errors. The table below lists symptoms that experienced users recognize quickly.
| Symptom | Likely cause | Typical fix |
|---|---|---|
| Robot explodes at start | Initial interpenetration of geoms | Exclude adjacent pairs or fix the initial pose |
| Gripper drops objects | Low condim or soft contact | Use condim 4 or 6 and stiffer solref |
| Joints rotate far too much | Degrees and radians confused | Set the compiler angle attribute |
| Base falls through floor | Missing contype match | Align the collision bit masks |
| Unstable, jittery motion | Timestep too large for stiffness | Reduce timestep or soften constraints |
Sensors in MJCF are noise-free by default; the noise attribute is stored for user code but not applied by the engine. Any policy trained for real hardware therefore needs explicit noise injection and domain randomization, otherwise it learns to rely on perfect measurements that no physical robot provides.
In simple terms
MJCF is a recipe card for a virtual robot. It lists every part, how the parts are connected, what they weigh, how they touch other objects and which motors move them. The MuJoCo program reads this card and builds a moving digital copy of the robot.
Unlike some other robot files, which mostly describe shape, MJCF also says how the physics should behave: how slippery a surface is, how firm a collision feels and how small each time step should be. That is why researchers use it when realistic motion and contact matter more than pretty pictures.
A practical example
A research team training a robotic hand to rotate a cube starts from a vendor URDF, converts it to MJCF and then refines the file. They replace the finger meshes with capsule collision geoms, set condim to 4 on the fingertips so that torsional friction resists slipping, and add a position actuator for each of the 16 joints with gains matched to the real servos.
They keep the timestep at 0.002 s, define one default class for visual meshes and another for collision, and add a randomization script that varies friction by about 30 percent and cube mass between episodes. After roughly a day of parallel training, the learned policy transfers to the physical hand and completes repeated cube rotations, largely because the MJCF model captured contact behavior closely enough for the gap between simulation and reality to stay small.