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.
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.
Who drives the body?
- 1
Sense
The simulator reports the body's position, speed and joint angles. - 2
Decide
The controller, here a trained neural network, chooses forces for joints or wings. - 3
Move
The physics engine moves the body one small time step. - 4
Repeat
This loop runs many times each simulated second.
Why build a virtual body?
| Goal | How a virtual body helps |
|---|---|
| Understand movement | Test ideas about how legs and wings are coordinated, safely and quickly |
| Join brain and body | In future, brain models built from wiring could be connected to a body model |
| Help robotics | Ideas from insect walking and flying can inspire small robots |
| Fewer experiments | Some questions can be explored in simulation first, then tested in real flies |
A tiny controller you can understand
# 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")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.
The problem
This runs in your browser. The "body" is a made-up toy: one number for height, with gravity and upward thrust.
- Run the starter. With zero thrust the body falls to the ground.
- 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;KPpushes towards the target;KDbrakes when moving fast. - Choose values for
KPandKD. Aim to end within 0.1 m of the target, and not rise more than 30% above it on the way. - Try targets 1.0 and 2.0. Write two lines: what happens with a large
KPand a zeroKD?
Practice lab
Edit the code and run it. Python runs in your browser — nothing is sent to our server.
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.
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.