Creating Custom Bodies

Creating Custom Bodies#

This exercise guides you through creating a simple Body with visual and collision shapes and adding it to a World.

You will:

  • Create a Body and assign simple shapes to its collision and visual collections

  • Add the Body to a World and visualize it

0. Setup#

Just execute this cell without changing anything. It imports the required classes and sets up the environment used in this exercise.

1. Create and attach geometry#

Create two simple shapes relative to the body frame and assign them as collision and visual geometry.

Your goal:

  • Create a red Box of size 0.2 x 0.2 x 0.2 centered at the Body origin

  • Create a Sphere of radius 0.1 located at y = 0.3 in the Body frame

  • Put the Box into the collision collection and the Sphere into the visual collection

  • Create a Body with the name “exercise_body” and assign the collections to its collision and visual collections

  • Add the Body to the World

collision_box = Box(
    origin=TransformationMatrix(),
    scale=Scale(0.2, 0.2, 0.2),
    color=Color(1.0, 0.0, 0.0, 1.0),
)
visual_sphere = Sphere(
    origin=TransformationMatrix.from_xyz_rpy(y=0.3),
    radius=0.1,
)
collision = ShapeCollection([collision_box])
visual = ShapeCollection([visual_sphere])

body = Body(name=PrefixedName("exercise_body"), collision=collision, visual=visual)
with world.modify_world():
    world.add_body(body)