Role

Programmer

Language

Java

IDE

Eclipse

Summary


Wildlife Simulation was a term-long project for my introduction to Java class. The point of this assignment was to use elements of randomness and natural phenomenon to create a program that simulates a small ecosystem in the ocean with fish, sharks, and bits of food. Each animal object was dependent on an energy model that affected the movement of the animal. With a ‘healthy’ state and a ‘sick’ state, the speed at which an animal chases its food (a shark chases a fish, and a fish chases a cookie) would be normal or at half speed. The application also allowed a user to interact with the simulation and affect the virtual environment. A user can change the speed of the entire simulation and slow it down, increase the size of the cookies (a bigger cookie will give a fish more energy and help them grow), select a shark and have that specific predator move with extra speed for five seconds, and finally cancel the flocking behaviour of the fish. Lastly, I had to break down movement aspects like acceleration into its mathematical elements in order to create fluid movement for the animals, and flocking behaviour into its mathematical elements to mimic natural behaviour for fish.

Process + Challenges


The biggest challenge I faced was implementing the flocking behaviour for the fish.

I started with the why of my problem: why do fish flock? After taking some time to do some research, I found a National Geographic clip on YouTube that gave me my answer. When a predator was nearby, fish would flock together because it was a protection mechanism. While fish flocked, a shark is not able to differentiate between individual fish. They see this huge population of fish in a small space as a larger entity and can’t target a single fish to hunt. Once I understood this, I knew that the flocking would not only change the behaviour of my fish, but how my sharks hunted as well.

Next, I broke my flocking behaviour into different mathematical components. Before I added this behaviour, my fish would move to the closest biggest cookie that is nearby and they would move around each other if they collided with one-another. So I had to consider how I would get a group of fish to move to the biggest piece of food nearby without constantly avoiding each other. After some consideration, I broke my flocking behaviour down into three functions: a revised collision function, an attraction to a group of fish that is stronger than the attraction to a single fish, and movement towards a cookie as a group, rather than individual movement.

Finally, I created my functions. Instead of fish entirely avoiding each other, my fish would now separate by 10 to 20 pixels, which created some overlap between fish, but you could still differentiate between different fish. I then added an attraction force between fish. This force pulled fish together but I made sure to make it a weaker force than the food-attraction force as I felt that food would always take precedence over flocking. Lastly, I created a calculation model that would check which piece of food each part of the flock is most drawn to. From there, based on the size and proximity of the cookie that the majority of the flock is most attracted to, the flock would move there.

My flocking mechanism resulted in behaviour that realistically mimics real-world flocking. The movement of my program looks natural (for a two-dimensional plane) and it seems like the fish can make decisions about how they respond to the presence of predators and food.

Final Product


I learned how to take natural behaviour and phenomena and translate it into mathematical building blocks that can be interpreted by a computer. Had this project continued, I would have liked to add some more functionality to my predators to more actively target sick fish and fish who have gotten separated from a flock. Furthermore, I would add more objects like rocks, that animals would have to navigate around, and kelp, that would slow down any animal that passes through it.