toddlerbot.sim.terrain package

Submodules

toddlerbot.sim.terrain.generate_terrain module

Main terrain generator that composes a global heightfield from modular terrain patches.

Each tile in the 2D terrain_map is a string specifying a terrain type, such as “flat”, “bumps”, “slope”, etc. The generator stitches these patches into a single global heightmap (as an hfield in MuJoCo), returns an MjSpec model, and optionally loads a robot and defines contact pairs.

Used during simulation setup and RL environment creation.

toddlerbot.sim.terrain.generate_terrain.add_debug_spheres_from_hmap(spec, global_hmap, nrow, ncol, total_width, total_length, max_height)

Add debug visualization spheres at heightmap positions.

Creates colored spheres at every heightmap pixel for debugging terrain generation. Should only be used with small terrains due to performance impact.

Parameters:
  • spec – MuJoCo spec to add spheres to.

  • global_hmap – The heightmap array.

  • nrow – Number of heightmap rows.

  • ncol – Number of heightmap columns.

  • total_width – Total terrain width.

  • total_length – Total terrain length.

  • max_height – Maximum terrain height for color scaling.

toddlerbot.sim.terrain.generate_terrain.create_terrain_spec(tile_width, tile_length, terrain_map, robot_xml_path=None, robot_position=(0.0, 0.0, 0.315053), timestep=0.004, pixels_per_meter=16, robot_collision_geom_names=None, self_contact_pairs=None)

Creates a MuJoCo MjSpec model with procedurally generated terrain and an optional robot.

Terrain is built from a 2D grid of tile names (e.g., “flat”, “slope”, “stairs”). If all tiles are “flat”, a single box geom is used instead of a heightfield for performance.

If a robot is loaded from XML, its initial pose is set, and contact pairs are added between terrain geoms and the robot’s collision geoms. If robot_collision_geom_names is not provided, it is automatically inferred from the geom names appearing in the MJCF <pair> tags.

Parameters:
  • tile_width (float) – Width of each terrain tile in meters.

  • tile_length (float) – Length of each terrain tile in meters.

  • terrain_map (List[List[str]]) – 2D list specifying terrain type per tile.

  • robot_xml_path (str) – Path to MJCF XML file describing the robot (optional).

  • robot_position (Tuple[float, float, float]) – Initial robot base position.

  • timestep (float) – Physics simulation timestep.

  • pixels_per_meter (int) – Resolution scale for the terrain heightfield.

  • robot_collision_geom_names (List[str] or None) – Names of robot geoms to register for terrain contact. If None, inferred from MJCF <pair> entries and filtered to geoms defined under the robot body.

  • self_contact_pairs (List[List[str]] or None) – A list of custom contact pairs (geom1, geom2) to be added explicitly to the MuJoCo scene. If defined, this overrides and removes the original <contact> section from the robot MJCF.

Returns:

Compiled MuJoCo scene specification. terrain_geom_names (List[str]): Names of the geoms used for terrain. safe_spawns (List[Tuple[float, float, float]]): List of spawn positions per tile. global_hmap (np.ndarray): Full assembled heightmap (even for flat terrain).

Return type:

spec (MjSpec)

toddlerbot.sim.terrain.terrain_types module

Defines functions to procedurally generate different terrain tiles. Each function returns a heightmap patch (2D numpy array) and its maximum height value.

Supported tile types: - Bumps: Perlin-based smooth noise with edge fade and flat center. - Rough: Higher-frequency Perlin noise, rougher than bumps. - Slope: Radial frustum shape with a flat top. - Stairs: Step-like concentric square levels increasing in height. - Boxes: Random box-shaped bumps, avoiding the center spawn area.

Depends on terrain_utils.py for helper utilities.

toddlerbot.sim.terrain.terrain_types.generate_boxes_patch(size, num_boxes=40, box_height=0.01, box_size_ratio=0.1, center_ratio=0.1)

Generate a sparse grid with randomly placed square boxes.

Avoids placing boxes in a central region to ensure safe spawning.

Parameters:
  • size (int) – Size of the grid (square).

  • num_boxes (int) – Number of boxes to place.

  • box_height (float) – Height of each box bump.

  • box_size_ratio (float) – Fraction of the grid taken up by one box.

  • center_ratio (float) – Size of protected center area as fraction of grid.

Returns:

Heightmap with boxes and max height.

Return type:

Tuple[np.ndarray, float]

toddlerbot.sim.terrain.terrain_types.generate_bumps_patch(size)

Generate a smooth Perlin noise bump patch with edge fade and flat center.

Parameters:

size (int) – Resolution of the square heightmap.

Returns:

Heightmap array and its max height.

Return type:

Tuple[np.ndarray, float]

toddlerbot.sim.terrain.terrain_types.generate_rough_patch(size)

Generate a rougher version of Perlin terrain with smaller bumps.

Parameters:

size (int) – Resolution of the square heightmap.

Returns:

Heightmap array and its max height.

Return type:

Tuple[np.ndarray, float]

toddlerbot.sim.terrain.terrain_types.generate_slope_patch(size, peak=0.2, flat_ratio=0.15)

Generate a radial frustum-shaped slope with a flat top.

Parameters:
  • size (int) – Size of the heightmap (square).

  • peak (float) – Maximum height at the center.

  • flat_ratio (float) – Ratio of flat top region.

Returns:

Heightmap and max height.

Return type:

Tuple[np.ndarray, float]

toddlerbot.sim.terrain.terrain_types.generate_stairs_patch(size, num_steps=5, peak_height=0.1)

Generate a stair-like concentric step pattern.

Parameters:
  • size (int) – Size of the square heightmap.

  • num_steps (int) – Number of concentric step levels.

  • peak_height (float) – Maximum height at the innermost step.

Returns:

Heightmap and max height.

Return type:

Tuple[np.ndarray, float]

Module contents

Terrain generation module for procedural ToddlerBot environments.

Provides utilities for generating various terrain types including slopes, stairs, bumps, and flat surfaces for simulation environments.