Skip to content
LumenaryAcademy
Systems 70 min

Simulation

A robot that lives in a computer. How physics engines step time forward, why the choice of integrator decides whether energy leaks or grows, how contact and friction are faked, and how far to trust the result.

Code tested · 6AI-draftedAwaiting review by a roboticist

Your guides: Chloe · The Builder, Margaret · The Experimenter, Gideon · The Safety Engineer

MotivationH

Why build a robot that does not exist?

A team in Zürich wants a four-legged robot to climb stairs. On the real robot, every fall costs an hour of repairs and an attempt takes minutes to set up. In simulation, the same robot can fall a million times overnight, on a thousand staircases at once, on one graphics card, and nobody has to pick it up. That arithmetic is why simulation is now part of almost every robot project:

  • Safety. A new controller is tried first where a mistake breaks nothing and hurts no one.
  • Speed and scale. A simulator can run faster than real time and in thousands of copies in parallel; reinforcement learning (later in this course) depends on this.
  • Testing. Every change to the software can be driven through the same hundred scenarios in continuous integration, closed loop, which recorded data cannot do (the previous lesson showed why replay is open loop).
  • Design. Before buying motors, you can check whether they are strong enough for the motion you want.

But a simulator is a model, and a model is a chain of approximations: equations of motion integrated in finite steps, contact that is faked one way or another, friction described by one number, sensors with invented noise. This lesson opens the box. You will build each piece small enough to see it work, and see each one fail in a way that real engines are designed to avoid.

IntuitionH

What happens in one time step

A rigid-body simulator holds the state of the world (positions and velocities of every body, or of every joint) and advances it by a small time step hh, again and again. In each step it does roughly the same five things.

statepositions, velocitiescollisiondetectioncontact, friction,joint forcesintegrateone step hsensor models+ noise, delaycontroller(your code)torquesnew state, time t + h
The loop inside a robot simulator. Each pass advances time by one step h, typically 1 to 5 ms for a robot. The controller sees only simulated sensor readings, never the true state, exactly as it would on the real robot.
  1. Collision detection finds which shapes touch or overlap, and where: contact points, normals and penetration depths. For simple shapes this is geometry; for meshes it is most of the computing time.
  2. Contact and constraint forces are computed so that bodies do not pass through each other, joints hold together, and friction resists sliding.
  3. The controller (your code) reads the simulated sensors and sets motor torques, at its own rate.
  4. Integration turns forces into accelerations through the equations of motion of Robot dynamics, M(q)q¨+C(q,q˙)q˙+g(q)=τ+JTfcontactM(q)\ddot q + C(q, \dot q)\dot q + g(q) = \tau + J^{\mathsf T}f_{\text{contact}}, and advances positions and velocities by one step.
  5. Sensor models produce what cameras, lidars, IMUs and encoders would have reported, with noise.

Engines differ in how they represent the state (MuJoCo, Drake and Bullet's multibody mode use joint coordinates, so a joint can never come apart; others store every body's full pose and hold the joints together with constraint forces), in how they handle contact, and in which integrator they use. The next three sections take the integrator, then contact, then friction.

DefinitionsH

Time stepping and integrators

Write the state as x=(q,v)x = (q, v), positions and velocities, with q˙=v\dot q = v and v˙=a(q,v)\dot v = a(q, v) given by the dynamics. An integrator turns this continuous law into a rule for xk+1x_{k+1} from xkx_k. Three rules matter here.

Definition— Explicit, semi-implicit and Runge–Kutta integration

With time step hh:

  • Explicit (forward) Euler: qk+1=qk+h vkq_{k+1} = q_k + h\,v_k,   vk+1=vk+h a(qk,vk)\;v_{k+1} = v_k + h\,a(q_k, v_k). Both updates use the old state.
  • Semi-implicit (symplectic) Euler: vk+1=vk+h a(qk,vk)v_{k+1} = v_k + h\,a(q_k, v_k),   qk+1=qk+h vk+1\;q_{k+1} = q_k + h\,v_{k+1}. Update the velocity first, then move with the new velocity.
  • Classical Runge–Kutta (RK4): evaluate the derivative four times within the step (at the start, twice at the midpoint, at the end) and combine them with weights 16,13,13,16\tfrac16, \tfrac13, \tfrac13, \tfrac16.

Both Euler methods are first order: halving hh roughly halves the error over a fixed time. RK4 is fourth order: halving hh divides the error by about 16, for smooth dynamics.

The two Euler methods look almost identical; the only difference is the order of two lines. The pendulum below shows what that order does. A 1 m pendulum is released from 1 rad (about 57°). With no friction its energy should never change, so the relative energy error is a clean measure of how badly each method is lying. Lumenary Math's Numerical methods derives these methods; here we watch them.

python
import math

g, L = 9.81, 1.0                       # a 1 m pendulum

def accel(theta):
    return -(g / L) * math.sin(theta)

def energy(theta, omega):              # per unit mass, zero at the bottom
    return 0.5 * (L * omega) ** 2 + g * L * (1 - math.cos(theta))

def explicit_euler(th, om, dt):
    return th + dt * om, om + dt * accel(th)            # both updates use the OLD state

def semi_implicit_euler(th, om, dt):
    om = om + dt * accel(th)                            # velocity first...
    return th + dt * om, om                             # ...then position with the NEW velocity

def rk4(th, om, dt):
    k1t, k1o = om, accel(th)
    k2t, k2o = om + dt / 2 * k1o, accel(th + dt / 2 * k1t)
    k3t, k3o = om + dt / 2 * k2o, accel(th + dt / 2 * k2t)
    k4t, k4o = om + dt * k3o, accel(th + dt * k3t)
    return (th + dt / 6 * (k1t + 2 * k2t + 2 * k3t + k4t),
            om + dt / 6 * (k1o + 2 * k2o + 2 * k3o + k4o))

E0 = energy(1.0, 0.0)                                   # released from 1 rad (about 57 degrees)
print("largest relative energy error |E - E0| / E0 over the first 10 s and the first 100 s")
print(f"{'method':22}{'dt (s)':>7}{'f-evals/s':>11}{'0-10 s':>11}{'0-100 s':>11}")
for name, step, evals in [("explicit Euler", explicit_euler, 1), ("semi-implicit Euler", semi_implicit_euler, 1), ("RK4", rk4, 4)]:
    for dt in (0.01, 0.001):
        th, om, worst, worst10 = 1.0, 0.0, 0.0, 0.0
        for k in range(1, int(round(100 / dt)) + 1):
            th, om = step(th, om, dt)
            worst = max(worst, abs(energy(th, om) - E0) / E0)
            if k == int(round(10 / dt)):
                worst10 = worst
        print(f"{name:22}{dt:7.3f}{evals / dt:11.0f}{worst10:11.1e}{worst:11.1e}")
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

Read the table row by row.

  • Explicit Euler gains energy, without limit. At a 10 ms step, the pendulum's energy has more than doubled within 10 s and is over ten times too large by 100 s; by then the simulated pendulum is whirling over the top, which a real one released from 57° never does. A ten times smaller step only delays the disaster.
  • Semi-implicit Euler's error stays bounded. The energy wobbles by 1.5% at a 10 ms step (0.15% at 1 ms), and the worst error over 100 s is the same as over 10 s. It does not grow.
  • RK4 is enormously more accurate, about 10−810^{-8} at a 10 ms step, for four times the work per step. But look at the last column: its error grows with time, from 1.1×10−81.1 \times 10^{-8} to 9.6×10−89.6 \times 10^{-8}. Run long enough and it would drift too, slowly, in one direction.

Theorem— Semi-implicit Euler preserves area in phase space

For θ˙=ω\dot\theta = \omega, ω˙=a(θ)\dot\omega = a(\theta), the semi-implicit Euler step (θ,ω)↦(θ′,ω′)(\theta, \omega) \mapsto (\theta', \omega') has Jacobian determinant exactly 1, for every step size hh. The explicit Euler step has determinant 1−h2a′(θ)1 - h^2 a'(\theta).

Proof

Semi-implicit: ω′=ω+h a(θ)\omega' = \omega + h\,a(\theta) and θ′=θ+h ω′=θ+h ω+h2a(θ)\theta' = \theta + h\,\omega' = \theta + h\,\omega + h^2 a(\theta). The Jacobian of (θ′,ω′)(\theta', \omega') with respect to (θ,ω)(\theta, \omega) is (1+h2a′(θ)hh a′(θ)1)\begin{pmatrix} 1 + h^2 a'(\theta) & h \\ h\,a'(\theta) & 1 \end{pmatrix}, whose determinant is 1+h2a′−h2a′=11 + h^2 a' - h^2 a' = 1.

Explicit: θ′=θ+h ω\theta' = \theta + h\,\omega and ω′=ω+h a(θ)\omega' = \omega + h\,a(\theta), with Jacobian (1hh a′(θ)1)\begin{pmatrix} 1 & h \\ h\,a'(\theta) & 1 \end{pmatrix} and determinant 1−h2a′(θ)1 - h^2 a'(\theta). For the pendulum a′(θ)=−(g/L)cos⁡θa'(\theta) = -(g/L)\cos\theta, so near the bottom the determinant is 1+h2(g/L)cos⁡θ>11 + h^2 (g/L)\cos\theta > 1: every step inflates areas.

∎

This is why many physics engines, MuJoCo's default among them, use semi-implicit Euler (with the damping terms treated implicitly for stability) rather than a higher-order method: it is cheap, it does not blow up, and its errors look like a little extra or missing energy rather than an explosion. MuJoCo also offers RK4 and implicit integrators for when accuracy matters more, and Drake offers error-controlled variable-step integrators for smooth problems.

ComputationC

Contact: a stiff spring, or a constraint

A pendulum is easy because it is smooth. Robots touch things, and contact is not smooth: a ball falls freely, hits the floor, and in a millisecond its velocity reverses. Simulators handle this in one of two broad ways.

Definition— Penalty contact and constraint-based contact

Penalty (soft) contact lets bodies overlap slightly and pushes them apart with a stiff spring-damper, a force proportional to the penetration depth dd (and its rate): f=k d+c d˙f = k\,d + c\,\dot d while in contact. Constraint-based (hard) contact forbids overlap: at each step it solves for contact impulses that make the bodies' approach velocity non-negative, subject to friction limits. Mathematically this is a complementarity problem (either the gap is open and the force is zero, or the gap is closed and the force is non-negative), or a convex optimisation that approximates one.

Penalty contact is simple and is what the code below uses: a 100 g ball dropped from 1 m onto a perfectly elastic floor, modelled as a spring of stiffness 10410^4 N/m that acts only while the ball is below the floor. Every apex should be exactly 1 m.

python
import numpy as np

m, g = 0.1, 9.81           # a 100 g ball
k = 1.0e4                  # contact stiffness of the floor's "spring", N/m (no damping: a perfectly elastic floor)

def force(y):
    """Gravity, plus a penalty spring pushing back while the ball is below the floor (y < 0)."""
    return -m * g + (k * -y if y < 0 else 0.0)

def simulate(dt, method, t_end=4.0, y=1.0, v=0.0):
    apexes, min_y, rising = [], 0.0, False
    for _ in range(int(round(t_end / dt))):
        if method == "explicit":
            y, v = y + dt * v, v + dt * force(y) / m
        else:                                   # semi-implicit (symplectic) Euler
            v = v + dt * force(y) / m
            y = y + dt * v
        min_y = min(min_y, y)
        if v > 0:
            rising = True
        elif rising:                            # velocity just turned from up to down: an apex
            apexes.append(y)
            rising = False
    return apexes, min_y

omega = np.sqrt(k / m)
print(f"contact spring: natural frequency {omega:.0f} rad/s, contact lasts {1000 * np.pi / omega:.1f} ms")
print(f"exact: the ball meets the floor at {np.sqrt(2 * g * 1.0):.2f} m/s and sinks {100 * np.sqrt(2 * g * 1.0) / omega:.2f} cm into it; every apex is 1.000 m")
for method in ("explicit", "semi-implicit"):
    for dt in (1e-3, 2e-4):
        apexes, min_y = simulate(dt, method)
        tops = ", ".join(f"{a:.3f}" for a in apexes[:4])
        print(f"{method:>13} Euler, dt = {dt * 1000:.1f} ms: apexes {tops} m; deepest {100 * -min_y:.2f} cm")
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

With explicit Euler at a 1 ms step the ball comes back to 2.7 m, then 7.3 m: each contact is a few steps of a stiff oscillator, and explicit Euler multiplies the oscillator's energy by 1+ω2h2=1.11 + \omega^2 h^2 = 1.1 at every one of them. Even at 0.2 ms it gains about a fifth of its height per bounce. Semi-implicit Euler at 1 ms keeps the apex within 0.7% of 1 m for four bounces. Real engines would add a damper to the spring, which removes energy on purpose and sets the coefficient of restitution; the point here is that the integrator must not add energy by accident.

Constraint-based methods escape this trade by working with velocities and impulses rather than forces: within one step they compute the impulse that stops the approach outright, so a stiff contact does not need a tiny step. The classic formulations are linear complementarity problems (Stewart and Trinkle, and Anitescu and Potra, in the mid-1990s). Modern engines solve variants of them: Bullet and PhysX (NVIDIA's engine, under Isaac Sim) use iterative impulse solvers; MuJoCo solves a convex optimisation with deliberately "soft" constraints, which makes contact smooth enough to differentiate and stable at large steps; Drake offers convex time-stepping and hydroelastic contact, which computes a pressure field over the overlapping volume of two soft shapes instead of a few points, giving more realistic contact patches for grasping.

ComputationC

Friction, and why it is the hardest part

Coulomb friction says that a contact can push sideways with any tangential force ftf_t up to μ\mu times the normal force fnf_n: ∥ft∥≤μfn\lVert f_t \rVert \le \mu f_n. If the contact is sticking, the friction force is whatever is needed to keep it stuck; if it is sliding, friction has magnitude μfn\mu f_n and opposes the slip. In three dimensions the allowed forces form a cone around the contact normal, the friction cone, met again in Manipulation and grasping; engines often replace the round cone by a pyramid with 4 to 8 faces, which turns the problem into a linear one.

The trouble is the word "whatever". Static friction is not a function of the velocity: at zero velocity it can be anything in an interval. One shortcut is to smooth it, ft=−μfntanh⁡(v/vs)f_t = -\mu f_n \tanh(v / v_s), which is a nice smooth function of velocity. The program below puts a 2 kg box on a 20° slope with μ=0.5\mu = 0.5. The friction angle is arctan⁡0.5=26.6°\arctan 0.5 = 26.6°, steeper than the slope, so a real box stays still.

python
import numpy as np

m, g, mu = 2.0, 9.81, 0.5            # a 2 kg box, friction coefficient 0.5
slope = np.radians(20)               # below the friction angle atan(0.5) = 26.6 deg, so it should stay put
N = m * g * np.cos(slope)            # normal force
pull = m * g * np.sin(slope)         # gravity's pull down the slope

def coulomb_stick_slip(v, applied, dt):
    """Velocity-level Coulomb friction: if friction can stop the box within this step, it sticks."""
    v_free = v + dt * applied / m
    max_impulse = mu * N * dt
    if abs(m * v_free) <= max_impulse:
        return 0.0                                   # static friction absorbs everything: stuck
    return v_free - np.sign(v_free) * max_impulse / m

def smooth_friction(v, applied, dt, v_s):
    """Regularised Coulomb friction, -mu N tanh(v / v_s): smooth, but never exactly zero at rest."""
    return v + dt * (applied - mu * N * np.tanh(v / v_s)) / m

dt, T = 1e-3, 10.0
print(f"friction angle {np.degrees(np.arctan(mu)):.1f} deg, slope {np.degrees(slope):.0f} deg: a real box stays still")
for label, v_s in [("stick-slip Coulomb", None), ("tanh friction, v_s = 1 cm/s", 0.01),
                   ("tanh friction, v_s = 1 mm/s", 0.001), ("tanh friction, v_s = 0.1 mm/s", 0.0001)]:
    if v_s is None:
        step = lambda v: coulomb_stick_slip(v, pull, dt)
    else:
        step = lambda v, v_s=v_s: smooth_friction(v, pull, dt, v_s)
    x, v, vmax = 0.0, 0.0, 0.0
    for _ in range(int(T / dt)):
        v = step(v)
        x += dt * v
        vmax = max(vmax, abs(v))
    print(f"{label:30} slid {100 * x:5.2f} cm in {T:.0f} s; speed now {1000 * v:+7.3f} mm/s, peak {1000 * vmax:6.3f} mm/s")
    if v_s is not None:
        creep = v_s * np.arctanh(pull / (mu * N))         # where tanh friction balances the pull
        print(f"{'':30} (exact creep speed of this model {1000 * creep:.3f} mm/s; stiffness x dt = {dt * mu * N / (m * v_s):.2f})")
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

The velocity-level stick-slip rule, which asks each step "can friction stop the box within this step?", keeps the box exactly still. The smoothed law fails in two opposite ways:

  • Creep. With vs=1v_s = 1 cm/s, smoothed friction can only resist the pull while the box is moving, so the box settles into a steady slide at 9.2 mm/s and creeps 9 cm in 10 s. A simulated robot holding a cup with this model slowly drops it.
  • Stiffness. Making vsv_s smaller reduces the creep in theory (0.9 and 0.09 mm/s), but the friction law becomes so steep near zero that the explicit step overshoots back and forth. Once the product of the law's stiffness and hh exceeds 2 (here 4.6 and 46), the numbers chatter, and the box at vs=0.1v_s = 0.1 mm/s actually slides further, 3.1 cm, than the one at 1 mm/s.
ExplanationH

The engines, compared fairly

No simulator is best at everything. Each was built for a purpose, and its strengths follow from it (as of 2026).

EngineOriginDesigned forNotes
MuJoCoEmo Todorov and colleagues, University of Washington (2012); open source from Google DeepMind since 2022Fast, accurate articulated dynamics for control and learningJoint coordinates, convex soft contact, MJCF model files; MJX runs it on GPUs and TPUs through JAX
GazeboUniversity of Southern California (2002); developed by Open RoboticsWhole-robot simulation with sensors, closely integrated with ROSPluggable physics engines, SDFormat world files; the older "Gazebo Classic" reached end of life in January 2025
Isaac SimNVIDIAPhotorealistic sensors and GPU-parallel physics (PhysX), on the Omniverse platformRay-traced cameras and lidar; Isaac Lab, built on it, trains policies in thousands of parallel environments; needs an NVIDIA RTX GPU
PyBulletErwin Coumans's Bullet engine, with Python bindings from about 2016Easy, free, scriptable simulationVery widely used in learning research of the late 2010s; quick to install anywhere
DrakeMIT and Toyota Research InstituteRigorous modelling, analysis, optimisation and verificationHydroelastic contact, convex time-stepping, automatic differentiation; slower per step but precise

Others are worth knowing. Webots (Cyberbotics, Switzerland, open source since 2018) and CoppeliaSim (Coppelia Robotics, Switzerland) are complete, friendly environments used in many universities. Genesis (2024), an open-source GPU physics platform from a consortium of university and industry researchers, and Newton, an open-source GPU physics engine announced in 2025 by NVIDIA, Google DeepMind and Disney Research, target massively parallel robot learning. Robot makers ship their own: ABB's RobotStudio runs the real controller's software against a virtual arm, and FANUC's ROBOGUIDE and KUKA.Sim serve the same purpose for programming industrial cells offline.

How to choose? For training a locomotion or manipulation policy with reinforcement learning, a GPU-parallel engine (MuJoCo with MJX or MuJoCo Warp, Isaac Lab, Genesis) is usual. For testing a ROS 2 software stack with realistic sensors, Gazebo or Isaac Sim. For contact-rich manipulation where the forces themselves matter, Drake or MuJoCo. For a first project on a laptop, any of the free ones. Teams that are serious about transfer to reality often check results in two engines: a policy that works only in one has probably learned that engine's quirks.

Sensors in simulation

The controller should see the world only through simulated sensors, imperfect in the same ways as real ones. Cameras are rendered, by rasterisation (fast) or ray tracing (realistic light, reflections, motion blur). Lidar is simulated by ray casting: shoot a ray per beam and find the nearest hit. IMUs are computed from the simulated body's acceleration and angular velocity, then corrupted. The program below ray-casts a 16-beam lidar in a 6 m × 4 m room with a pillar, adds range noise and missed returns, and then simulates a gyroscope on a robot standing still.

python
import numpy as np

rng = np.random.default_rng(42)

# The world: a 6 m x 4 m room with a round pillar (radius 0.3 m) at (3.5, 2.8).
def ray_range(ox, oy, angle, max_range=5.0):
    dx, dy = np.cos(angle), np.sin(angle)
    hits = []
    for t_wall in ((0 - ox) / dx if dx else -1, (6 - ox) / dx if dx else -1,
                   (0 - oy) / dy if dy else -1, (4 - oy) / dy if dy else -1):
        if t_wall > 1e-9:
            hits.append(t_wall)                     # the nearest positive one is the wall we hit
    cx, cy, r = 3.5 - ox, 2.8 - oy, 0.3             # ray-circle intersection: |t d - c|^2 = r^2
    b = dx * cx + dy * cy
    disc = b * b - (cx * cx + cy * cy - r * r)
    if disc >= 0 and b - np.sqrt(disc) > 0:
        hits.append(b - np.sqrt(disc))
    d = min(hits)
    return d if d <= max_range else np.inf

def lidar(ox, oy, heading, n=8, sigma=0.01, dropout=0.05):
    """Ideal ranges from ray casting, then the imperfections of a real sensor."""
    angles = heading + np.arange(n) * 2 * np.pi / n
    true = np.array([ray_range(ox, oy, a) for a in angles])
    noisy = true + rng.normal(0.0, sigma, n)                  # range noise
    noisy[rng.random(n) < dropout] = np.inf                    # missed returns (dark or shiny surfaces)
    return np.degrees(angles - heading), true, noisy

bearings, true, noisy = lidar(1.5, 2.0, 0.0, n=16)
print("bearing   true range   simulated reading")
for b, t, z in zip(bearings, true, noisy):
    print(f"{b:6.1f}   {t:9.3f} m   {z:9.3f} m")

# A simulated gyroscope on a robot that is standing still: white noise plus a slowly wandering bias.
dt, steps = 0.005, 12000                                       # 200 Hz for 60 s
bias = 0.002 + np.cumsum(rng.normal(0.0, 2e-5, steps))       # rad/s, a random walk
rate = bias + rng.normal(0.0, 0.003, steps)                   # what the gyro reports
heading = np.cumsum(rate) * dt                                 # integrating it, as odometry would
print(f"gyro on a stationary robot: heading after 60 s drifted to {np.degrees(heading[-1]):.1f} deg")
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

The beam at 22.5° meets the pillar at 1.855 m, while its mirror image at 337.5° runs on to the far wall at 4.871 m; one beam (247.5°) came back empty, as real lidars do on black or mirror-like surfaces; and integrating the stationary gyro's small bias gives a heading that has wandered by 2° in a minute. A controller tested only on perfect simulated data has never met an empty beam or a drifting heading, and will meet both on its first day outside.

Digital twins

A digital twin is a simulation of a particular physical system, kept up to date with data from it: this factory cell, with these robots at these positions, this conveyor's actual speed, this gripper's measured wear. Factories use twins to plan layouts and check cycle times before moving any machine, to test new programmable-logic-controller and robot programs against a virtual cell ("virtual commissioning"), and to replay incidents. The word is used loosely in marketing; the useful test is whether the model is calibrated against the real system and updated as the system changes.

ExplanationH

The reality gap

A controller that works in simulation and fails on the robot has fallen into the reality gap: the differences between the model and the world. The usual suspects, roughly in order of how often they bite:

  • Actuators. Motors have torque limits, current loops with finite bandwidth, gear backlash, friction and heating; simulators often model a joint as a perfect torque source.
  • Contact and friction. One coefficient of friction, a few contact points and a guessed stiffness stand in for rubber, dust, wet floors and deformable objects, as the last two sections showed.
  • Latency. Real sensing, computing and communication add delays of several milliseconds to tens of milliseconds that a naive simulation leaves out.
  • Parameters. Masses, inertias and centres of mass from a CAD model are wrong for the robot with its cables, batteries and bolts.
  • Perception. Rendered images differ from camera images in lighting, texture, noise and blur.

Engineers narrow the gap from both sides. System identification measures the real robot's parameters and fits the model to them. Domain randomisation (Tobin and colleagues, 2017) trains on thousands of simulations with randomised masses, frictions, delays and textures, so that the real world looks like just one more variation. Learned actuator models, used by ETH Zürich's team for the ANYmal quadruped in 2019, replace the ideal motor with a neural network trained on data from the real drive. These ideas are the subject of Sim-to-real transfer.

Common misconceptionsH

Common misconceptions

Misconception

"A higher-order integrator is always better." RK4 is far more accurate on smooth problems, but its order relies on smoothness; across a contact or a friction switch it falls back to first order, at four times the cost per step. For contact-rich robots, a cheap, stable, velocity-level first-order step is often the better trade.

Misconception

"A smaller time step fixes everything." It fixes truncation error, but not modelling error: a wrong friction coefficient is wrong at any step size, and regularised friction creeps at any step size. Very small steps also accumulate rounding error and cost time.

Misconception

"If it works in simulation, it will work on the robot." Simulation success is necessary, not sufficient. Every simulator omits something; the question is whether what it omits matters for your task, which only a test on the real robot (slowly, safely, with an emergency stop at hand) can answer.

Misconception

"Simulated sensors should be perfect, to make debugging easier." Debug with perfect sensors first, by all means, but test with noise, dropouts, bias and delay: a perception or control stack that has never seen them is untested.

History & global context

A short history

Numerical integration of the equations of motion is as old as the equations: Euler's method dates from the 1760s, and Carl Runge and Martin Kutta developed their methods around 1900. Efficient recursive algorithms for articulated bodies, which make simulating a many-jointed robot fast, came in the 1980s, notably from Roy Featherstone. Constraint-based contact with friction was put on a firm footing in the 1990s, by David Baraff for computer graphics and by David Stewart, Jeff Trinkle, Mihai Anitescu and Florian Potra for rigid-body mechanics. Game physics engines (Bullet, ODE, PhysX) spread these methods in the 2000s; Gazebo, born at the University of Southern California in 2002, made full-robot simulation standard in research; MuJoCo (2012) was designed for optimisation and control. Since about 2020, GPU-parallel simulators have made it routine to train robot policies on thousands of simulated robots at once.

Exercises

Exercises

1. Simulation

Warm-upHuman

You simulate a frictionless pendulum with a fixed, reasonably small time step for a very long time. For which integrator does the energy error stay bounded forever, rather than growing?

Choose one

2. Simulation

CoreComputational

For the harmonic oscillator x¨=−ω2x\ddot x = -\omega^2 x, one step of explicit Euler multiplies the energy 12v2+12ω2x2\tfrac12 v^2 + \tfrac12\omega^2 x^2 by exactly 1+ω2h21 + \omega^2 h^2. With ω=10\omega = 10 rad/s and h=0.01h = 0.01 s, by what percentage has the energy grown after 1 s of simulated time? Round to the nearest whole percent.

3. Simulation

CoreComputational

A simulator models a 0.5 kg foot hitting the floor with a penalty spring of stiffness 5×1045 \times 10^4 N/m and no damping. While in contact the foot is a mass on a spring, so the contact lasts half an oscillation period. How long is that, to the nearest millisecond?

4. Simulation

StretchComputational

A simulator uses smoothed friction f=−μNtanh⁡(v/vs)f = -\mu N \tanh(v / v_s) with μ=0.4\mu = 0.4 and vs=5v_s = 5 mm/s. A box rests on a 10° slope, gentle enough that real friction would hold it. In steady state the box creeps at the speed where smoothed friction exactly balances gravity's pull along the slope. How far does it creep in one minute, in millimetres (nearest whole number)?

5. Simulation

CoreHuman

A simulated robot picks up a smooth plastic box every time; the real robot drops it about one time in three. Which of these are plausible causes? Select all that apply.

Choose all that apply

6. Simulation

CoreComputational

Write rk4_step(f, t, y, h): one step of classical fourth-order Runge–Kutta for y˙=f(t,y)\dot y = f(t, y), where y is a NumPy array and f returns an array of the same shape. Return the new y at time t + h. The tests check that it gets ee from y˙=y\dot y = y, is exact for y˙=3t2\dot y = 3t^2, and that halving the step divides the error by about 16 on an oscillator.

Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

7. Simulation

CoreComputational

The function first_apex(dt) drops a ball from 1 m onto a perfectly elastic penalty floor and returns the height of its first apex. With explicit Euler it comes back far higher than it started (about 2.7 m at dt = 1e-3). Change the integration so that the first apex is within 1 cm of 1 m for dt = 1 ms, 0.5 ms and 0.2 ms, without changing the step size or the physics.

Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

Further reading and resources