MuJoCo Heightfield Basics
What is a Heightfield?
A heightfield is a 2D grid where each cell contains a height value. MuJoCo uses heightfields to create realistic terrain with variable elevation. Key Properties:- Resolution: Number of grid cells (e.g., 256×256)
- Size: Physical dimensions
[x_size, y_size, z_scale, z_offset]x_size,y_size: Horizontal extent (meters)z_scale: Maximum height variation (meters)z_offset: Baseline elevation (meters)
- Data Format: PNG grayscale image or binary
.binfile
Current Terrain Configuration
File:~/workspace/source/model/world_train.xml
- Physical size: 7.1m × 9.3m
- Height variation: ±0.025m (2.5cm)
- Base height: 0.1m
The heightfield must be in the same directory as
world_train.xml or use an absolute path.Generating Terrain Heightfields
Method 1: Perlin Noise Terrain
Install Dependencies
Install Dependencies
Python Script: Perlin Noise Generator
Python Script: Perlin Noise Generator
Create Usage:
generate_terrain.py:Method 2: Staircase Terrain
Python Script: Staircase Generator
Python Script: Staircase Generator
Method 3: Random Obstacle Course
Python Script: Obstacle Course Generator
Python Script: Obstacle Course Generator
Using Custom Terrains
Step 1: Generate Heightfield
Step 2: Update World XML
Edit~/workspace/source/model/world_train.xml:
Step 3: Test in Simulation
If the robot falls through the terrain, increase
z_offset (4th parameter in size).Step 4: Train with New Terrain
model/world_train.xml.
Terrain Design Guidelines
Height Scale Recommendations
Choosing z_scale
Choosing z_scale
Robot Leg Capabilities:
Example:
- Maximum step height: ~0.08m (8cm)
- Typical step height: 0.04m (4cm)
- Body clearance: 0.05m (5cm)
| Difficulty | z_scale | Description |
|---|---|---|
| Easy | 0.015m | Gentle undulations, good for initial training |
| Medium | 0.025m | Default, balanced difficulty |
| Hard | 0.040m | Challenging, near robot’s step limit |
| Extreme | 0.060m | May require advanced adaptation |
Resolution Considerations
Choosing Resolution
Choosing Resolution
Heightfield Resolution:
- 64×64: Very coarse, fast simulation, low detail
- 128×128: Acceptable for training, moderate detail
- 256×256: Recommended, good detail-to-performance ratio
- 512×512: High detail, slower simulation
- 1024×1024: Maximum detail, significant performance cost
Feature Frequency
Balancing Smooth vs Rough
Balancing Smooth vs Rough
Perlin Noise Parameters:Rule of Thumb:
- Scale < 30: Very rough, may be too difficult
- Scale 30-60: Balanced
- Scale 60-100: Smooth, good for early training
- Scale > 100: Very smooth, may be too easy
Advanced Terrain Generation
Curriculum Learning Terrains
Progressive Difficulty
Progressive Difficulty
Create a series of terrains with increasing difficulty:Usage in Training:See
~/workspace/source/callbacks/curriculum_callback.py for implementation.Combining Multiple Noise Sources
Layered Terrain Features
Layered Terrain Features
- Large-scale hills (base)
- Fine surface texture (detail)
- Discrete obstacles (obstacles)
Real-World Terrain from DEMs
Import GeoTIFF Elevation Data
Import GeoTIFF Elevation Data
Convert real-world elevation data (Digital Elevation Models) to MuJoCo heightfields:Data Sources:
- USGS Earth Explorer: https://earthexplorer.usgs.gov/
- OpenTopography: https://opentopography.org/
- NASA SRTM: https://www2.jpl.nasa.gov/srtm/
Testing and Validation
Verify Terrain in Viewer
Measure Terrain Statistics
Analyze Heightfield Properties
Analyze Heightfield Properties
Troubleshooting
Robot Sinks into Terrain
Robot Sinks into Terrain
Cause:
z_offset too low or terrain has large negative values.Solution:Terrain Appears Flat
Terrain Appears Flat
Cause:
z_scale too small or PNG has uniform gray values.Solution:- Check PNG contrast:
python3 -c "from PIL import Image; import numpy as np; img = np.array(Image.open('hfield.png')); print(f'Range: {img.min()}-{img.max()}')" - Increase
z_scalein XML - Regenerate terrain with higher amplitude
Simulation Very Slow
Simulation Very Slow
Cause: High-resolution heightfield (>512×512) or very rough terrain.Solution:
- Reduce resolution:
--width 128 --height 128 - Smooth terrain:
--scale 80.0 - Simplify contact model in
robot.xml
Example Terrain Recipes
Beginner Terrain
size="7.1 9.3 .015 0.1"Standard Training
size="7.1 9.3 .025 0.1"Rocky Challenge
size="7.1 9.3 .040 0.1"Obstacle Course
size="7.1 9.3 .035 0.1"Next Steps
Train on Custom Terrain
Use your terrain for RL training
Extending Controllers
Adapt controller for new terrain types