Simulating Steering Behaviors of Flock of Animals


Project Description

This project simulates how a group of birds moves together, how they steer together. Behaviors such as obstacle avoidance, object seeking, and wander are discussed and implemented in JAVA 2D.

The customer requirements: I need an implementation of the "Boids" ALife (artificial life) algorithm, which simulates the behaviour of people that come out of a subway train and are on their way out (exit flow). On this course, the group can meet various obstacles: people coming in the subway station, people standing still, pilons, kiosks and stores.
I need the case where the entrance is the same as the exit, as well as a case where they are different. The simulation must be shown graphically on a 2D Java canvas (panel, etc..), without using any external rendering engines or similar libraries
The application needs to respect the 3 conditions presented on [THIS PAGE]

References 
  • Not Bumping Into Things, Notes on "obstacle avoidance" for the course on Physically Based Modeling at SIGGRAPH 88, August 1 through 5 in Atlanta, Georgia.[HTML]
  • Steering Behaviors For Autonomous Characters , Craig W. Reynolds [PDF]
  • Flocks, Herds, and Schools:A Distributed Behavioral Model, Craig W. Reynolds, Symbolics Graphics Division[HTML]
Java 2D Code
The code can be directly downloaded from [HERE]

Code Implementation in Java 2D (Sample of the code is shown)

1. Wander behavior 
Vec3 steerForWander (float dt); Returns a steering force for wandering behavior. The steering value is purely tangential (has no Forward component). The time step value allows wander rate to be consistent when frame times vary.
Vec3 steerForWander (float dt)
{
    // random walk WanderSide and WanderUp between -1 and +1
    const float speed = 12 * dt; // maybe this (12) should be an argument?
    WanderSide = scalarRandomWalk (WanderSide, speed, -1, +1);
    WanderUp   = scalarRandomWalk (WanderUp,   speed, -1, +1);

    // return a pure lateral steering vector: (+/-Side) + (+/-Up)
    return (side() * WanderSide) + (up() * WanderUp);
}
inline float scalarRandomWalk (const float initial,
                                   const float walkspeed,
                                   const float min,
                                   const float max)
    {
        const float next = initial + (((frandom01() * 2) - 1) * walkspeed);
        if (next < min) return min;
        if (next > max) return max;
        return next;
}
inline float frandom01 (void)
    {
        return (((float) rand ()) / ((float) RAND_MAX));
}



2. Seek behavior 
Vec3 steerForSeek (const Vec3& target); Returns a steering force to seek the given target location. Causes a vehicle to turn toward the target and move to it. If this behavior is used alone and unconditionally, it will cause the vehicle to pass through the target then turn around for another pass.

steerForSeek (const Vec3& target)
{
    const Vec3 desiredVelocity = target - position();
    return desiredVelocity - velocity();
}


No comments:

Post a Comment