# Algorithm for random flight of a fly

I'm new to game development. What is a good algorithm to model the random flight of a fly? I've been looking at path-finding algorithms, but they don't give any interesting random behaviour.

• Brownian noise? Commented Feb 14, 2012 at 12:01
• check this red3d.com/cwr/steer/gdc99 Commented Feb 14, 2012 at 12:22
• Does it need to ultimately need to land up (close to or at) a specific place? Or does it just need to move about randomly? Try searching for boids. If you 'apply forces' you can get them to gravitate toward a specific destination, if you don't they will wander about aimlessly. Commented Feb 14, 2012 at 12:24
• I recently asked a similar question: gamedev.stackexchange.com/questions/23670/… You can ignore the bits regarding biasing towards a target, and adding a 3rd dimension should be trivial. Commented Feb 14, 2012 at 12:27
• Try searching for boids and flock simulation. Even if the results are not for insects, it should still give you some ideas. Commented Feb 14, 2012 at 12:27

## 2 Answers

Lévy flights or brownian motion should work. These are random walks where at each time step the insect moves a random direction and distance. They differ in what distribution the random variables are sampled from.

The motion of hunting sharks can be modeled as brownian motion when prey is plentiful and lévy flight when prey is scarce.

Depending on what you use it for, you might want to restrict their motion (to keep them near a specific part of a level) or limit the acceleration (to make them appear to have have more inertia).

• Somewhat useful but incomplete. Why is each of the techniques mentioned a good approximation for the motion of a fly? How do they work?
– Anko
Commented Mar 15, 2013 at 0:53

I made something similar to this a while back for 2D movement, but it should be a similar concept, similar to the way a fly buzzes around.

/*
Pick a random direction angle; call it dir.
Pick a random direction for dir to change (clockwise/counterclockwise). Call it accdir.
Pick a random time within a range before changing direction, call it chtime.
Start timer.
Every frame (or time period):
change dir according to accdir.
if timer == chtime:
reset timer
Pick new random chtime
Pick new random accdir

*/


This should create smooth acceleration, and it should be buzzing around in circles. In two dimensions, accdir would be either clockwise or counterclockwise, so it would be spinning in circles by adding or subtracting from the vector angle, changing direction every so often. In 3D, it might be a bit more complex.