# Firefly movement

## Recommended Posts

VitaminCpp    121
I'm currently working on a 2D game project and I have a screen that I felt so empty. So I decided to create fireflies flying around the screen. Is there a way to make random movement for this? I'm open for any suggestion. Thanks before.

##### Share on other sites
Captain P    1092
It's easy to add random movement. Just change their coordinates by a random, not too large, value.

That usually doesn't look very natural, but it's 'random movement'. More interesting results can be achieved by keeping a velocity vector, and changing that vector rather than the position. Now you indirectly affect the position, by changing the firefly's course.

It's probably even more interesting when we look at the firefly's movement as a direction and a speed. You can then steer the movement by a few degrees, at random, and modify the speed as well. By capping the maximum steering values, the movement should look more natural than the above two methods.

##### Share on other sites
Vorpy    869
If you want to make them really interesting you could try to roughly approximate actual firefly behavior. I've noticed that fireflies like to hover around a certain area, then move around a bit, then hover around some more. This could be modeled by giving the firefly two states, hover and move. Hmm..actually, maybe give the firefly a position that it is trying to be in, and it accelerates (along with maybe some random acceleration) to be in that position, and the position itself might slowly drift around, and then suddenly jump somewhere else to make the firefly appear to hover around then fly across to somewhere else. Or the point that the firefly flies towards could be modeled as some sort of levy flight approximation, since a levy flight actually sort of models the hovering around and then moving somewhere else behavior.

##### Share on other sites
VitaminCpp    121
Thanks for the replies guys.

##### Share on other sites
Get current X,Y position.
Generate new X,Y potition.
Move to new X,Y position.
Repeat.

M.

##### Share on other sites
smitty1276    560
You need to keep a velocity vector... occasionally you should randomly determine a new velocity vector then interpolate the velocity from the original to the new velocity over some randomly generated period of time.

Simply picking new X, Y coordinates or picking a new vector every frame--even if it's only a slight adjustment--will look very, very bad.

##### Share on other sites
zedz    291
float current_direction = 0.0;

if ( change_dir )
{
current_direction += signed_random( 0.1 ); .. -0.1 -> 0.1
// clamp to 2*PI
}

pos.x += sin( current_direction ) * speed;
pos.y += cos( current_direction ) * speed;