Brains and AIMiddleLesson 2985 min read

A virtual fly body

A brain needs a body. Researchers have built detailed physics simulations of a fly's body that can walk and fly, controlled by trained neural networks.

Lesson in motion

In 60 seconds

A virtual fly body

A brain needs a body. Researchers have built detailed physics simulations of a fly's body that can walk and fly, controlled by trained neural networks.

1/4
In simple words
A brain on its own cannot walk. It sends signals to muscles, the muscles pull on legs and wings, and the body moves through the world. Then the eyes and legs send new signals back to the brain. To study this loop in a computer, scientists built a virtual fly, like a very detailed video-game character that obeys the laws of physics.
A fly's brain controls six legs, two wings, a head and more. Every movement changes what the fly sees and feels, which changes what the brain does next. This loop between brain, body and world is called closed-loop control.
Researchers have built detailed physics simulations of a fly's body, based on careful measurements of real flies. The virtual body has joints and parts that move under simulated forces such as gravity, contact with the ground and, for flight, forces from the air. These virtual flies can walk and fly in simulation.

Who drives the body?

In the work described here, the virtual body is controlled by trained neural networks: artificial networks that learned, often from recordings of real fly movement, how to move the legs and wings to follow a path or keep balance. They are not copies of the fly's real brain wiring. They are a way to test whether a body model can move like a fly.
  1. 1

    Sense

    The simulator reports the body's position, speed and joint angles.
  2. 2

    Decide

    The controller, here a trained neural network, chooses forces for joints or wings.
  3. 3

    Move

    The physics engine moves the body one small time step.
  4. 4

    Repeat

    This loop runs many times each simulated second.

Why build a virtual body?

GoalHow a virtual body helps
Understand movementTest ideas about how legs and wings are coordinated, safely and quickly
Join brain and bodyIn future, brain models built from wiring could be connected to a body model
Help roboticsIdeas from insect walking and flying can inspire small robots
Fewer experimentsSome questions can be explored in simulation first, then tested in real flies

A tiny controller you can understand

Real virtual flies are complex. We can see the key idea with one number: height. A hovering insect must push up just enough to fight gravity. Too little and it falls; too much and it shoots up. A simple controller pushes harder when it is too low, and gently brakes when it is moving fast.
Python: one simulated second of falling with no controlpython
# Toy physics: height h (metres), velocity v, gravity g. MADE-UP simple body.
g, dt = 9.81, 0.01
h, v = 1.0, 0.0
for step in range(100):          # 100 steps of 0.01 s = 1 second
    thrust = 0.0                 # no controller yet
    v += (thrust - g) * dt
    h = max(0.0, h + v * dt)
print("Height after 1 s with no thrust:", round(h, 2), "m")
Note
The trained controllers in the real research are neural networks with many inputs and outputs. Our lab uses two hand-tuned numbers instead, so you can see why feedback matters.
Real example
Think of balancing a cricket bat upright on your palm. Your eyes see it tilt, your brain reacts, your hand moves, and the bat tilts a new way. You are running a closed control loop. A virtual fly runs the same kind of loop, many times every second.
Watch out
A virtual fly that walks well is not proof that we understand the real fly's brain. The controller may move the body in a different way from real neurons. Keep "can move like a fly" separate from "works like a fly's brain".

Watch and read more

Lab

A tuned feedback controller that makes a toy one-dimensional flying body hover at a target height without falling or overshooting much.

~20 min

The problem

This runs in your browser. The "body" is a made-up toy: one number for height, with gravity and upward thrust.

  1. Run the starter. With zero thrust the body falls to the ground.
  2. Finish controller(h, v, target) so it returns an upward thrust. A good idea: g + KP * (target - h) - KD * v. The first part holds up the weight; KP pushes towards the target; KD brakes when moving fast.
  3. Choose values for KP and KD. Aim to end within 0.1 m of the target, and not rise more than 30% above it on the way.
  4. Try targets 1.0 and 2.0. Write two lines: what happens with a large KP and a zero KD?

Practice lab

Edit the code and run it. Python runs in your browser — nothing is sent to our server.

Output

No output. Add print(...) to see values.

You are done when

Hard questions

Try to answer before you reveal. If you can answer these, you understood the lesson.

Q1Your controller works for a body of mass 1. What happens if the real body is twice as heavy, and how would a real system handle it?Reveal

The same thrust would lift a heavier body less, so the "hold up the weight" part would be too small and the body would settle below the target, or fall if thrust is limited. Real controllers deal with this by adding a term that slowly corrects a steady error (an integral term), by estimating the mass, or, for learned controllers, by training on many body variations so the network adapts.

Q2Why do researchers use trained neural networks instead of hand-tuned rules for a full virtual fly?Reveal

A full fly body has many joints and wings that must move together, and the right forces depend on many inputs at once. Writing hand rules for all of that is very hard. A neural network can learn a controller from data or from trial and error in simulation. The cost is that the learned controller is harder to understand.

Please sign in to continue.

Questions people ask

Is the virtual fly's brain the FlyWire connectome?

In the body simulations described here, no. The bodies are driven by trained artificial neural networks. Joining a wiring-based brain model to a body model is a goal many scientists talk about, but it is a separate, harder step.

What is a physics engine?

Software that calculates how objects move under forces such as gravity and contact, step by small step. Games and robot simulators use them too.

Why not just use a real fly?

Real flies are used, and they are essential. A simulation adds things a real fly cannot give: you can see every force, repeat the same moment exactly, and try ideas that would be impossible in a living animal.

Lesson test

5 questions. Get 3 right (60%) to pass and complete this lesson.

Sign in to take the test and save your progress