Sign in to follow this  
VitaminCpp

Firefly movement

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this