# Module 2.2: Visual Meshes vs. Collision Primitives

- Course: [Course 2: Robot Modeling, Velocities & Physical Structure](https://roboticsconcepts.dev/courses/robot-modeling-velocities-physical-structure.md)
- HTML Page: https://roboticsconcepts.dev/courses/robot-modeling-velocities-physical-structure/visual-meshes-vs-collision-primitives/
- Key Concepts: Visual vs. collision geometry, Convex primitives (capsules, spheres, boxes), Contact-check performance trade-offs

The first time I imported a detailed CAD assembly of a robot arm into a physics simulator, my frame rate dropped from real-time to a slideshow. The culprit wasn't the physics math — it was asking the collision engine to test every bolt hole and chamfer on a 50,000-triangle mesh every millisecond.

## Two faces for every link: one for your eyes, one for physics

Think about how a video game character works. What you see on screen might have jacket folds, shoelaces, and detailed armor plates, while the physics engine quietly treats that character as a smooth vertical pill so it slides cleanly past doorways without snagging on a belt buckle.

Robots in simulation do the exact same thing. Inside every `<link>` block in a URDF or MJCF file, there are separate `<visual>` and `<collision>` blocks. The `<visual>` block points to a high-poly `.obj` or `.stl` mesh so engineers and camera sensors see realistic hardware. The `<collision>` block replaces that intricate shape with a handful of simple convex primitives.

## Why capsules, spheres, and boxes win on speed

Checking whether two arbitrary triangle meshes overlap requires inspecting thousands of triangle pairs. Checking whether two capsules overlap comes down to a single line-segment-to-line-segment distance formula:

- Spheres: fastest possible check — just compare center distance against the sum of radii `r₁ + r₂`.
- Capsules (swept spheres): a line segment with a radius around it. Ideal for forearms, shins, and robot legs because they have zero sharp corners to catch on edges.
- Boxes: clean for flat grippers, table tops, and rectangular battery torsos.

## Interactive Sandbox: URDF & Collision Capsule Inspector

Use the Visual vs. Collision opacity slider in the inspector below to fade out the detailed arm and quadruped silhouettes and reveal the underlying collision capsules and spheres. Notice the primitive count badge as you switch presets.

## Tight envelopes, safe margins, and self-collision

When fitting collision primitives around a visual link, you want them slightly conservative — hugging the real hardware closely enough that the arm can reach into bins, yet padded by a few millimeters so cables and screw heads don't graze obstacles.

Once those capsules are in place, collision checking isn't only for tables and walls. Simulators and motion planners also run self-collision checks between non-adjacent links on the same robot (for example, making sure a quadruped's front shin never clips through its own belly) thousands of times per second.

## Capsule-to-capsule clearance test

```text
clearance = min_dist(segment_A, segment_B) − (r_A + r_B)
```

If `clearance <= 0`, the two capsules are touching or penetrating. Computing segment-to-segment distance takes a few dozen floating-point operations instead of thousands of triangle tests.

## Check Your Intuition

**Question:** You are simulating 4,096 parallel quadruped robots on a GPU to train a walking policy. What happens if you point `<collision>` directly at the 40,000-triangle CAD meshes instead of capsules?

- [ ] Nothing changes — GPUs ignore `<collision>` tags during walking
- [✓] Contact checks become orders of magnitude slower and sharp mesh edges cause jittery contact forces
- [ ] The robot walks faster because more triangles give better traction

**Explanation:** High-poly triangle meshes create huge memory and compute overhead in contact solvers and introduce non-convex edge snags, whereas capsules give analytic, smooth, fast contact normals.

## Where You'll See This in Modern Robotics

Modern GPU simulators like MuJoCo MJX and Isaac Lab rely heavily on primitive collision geoms (especially spheres and capsules) so thousands of environments can step physics in parallel without blowing up contact buffers.

## Companion Python Notebook: 02 · Visual Meshes vs. Collision Capsules Lab

Implement exact point-to-capsule distance checks and benchmark primitive contact checks against dense polygon vertices.

- Open in molab (WASM): https://molab.marimo.io/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/course-2/02_visual_vs_collision_primitives.py/wasm
- Open in Google Colab: https://colab.research.google.com/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/colab/course-2/02_visual_vs_collision_primitives.ipynb
- Download .py: https://roboticsconcepts.dev/downloads/notebooks/course-2/02_visual_vs_collision_primitives.py
- Download .ipynb: https://roboticsconcepts.dev/downloads/notebooks/colab/course-2/02_visual_vs_collision_primitives.ipynb
- Run locally: `uvx marimo edit --sandbox notebooks/course-2/02_visual_vs_collision_primitives.py`
