Getting Schooled

By | September 30, 2013

I’m not sure how much the predators are getting schooled but the prey are using nature’s age old defense mechanism of safety in numbers. So here is a refactored version of a flocking/schooling behavioral model that I had posted on chiti-graf.net.

Also I had a request recently to provide the source code via GitHub. You can now access the source code via the following link.

PredatorPrey Source Code Via GitHub

So I’ve rewritten my predator and prey pursuit JavaScript code. Not to be confused with the predator prey population model. I’ve been intrigued by flocking, schooling, and herding algorithms since running across the Boids web site.

Link to the Boids page.

Ben Dowling at coderholic implemented the Boids behavior model in JavaScript and you can check out his post at the following link.

Link to the JavaScript Boids page.

Basically my code is based on Ben’s code with a predator thrown in to pursue the prey. I was inspired by watching sharks swim through a school of fish as shown the images below. I added the avoidance behavior to the prey’s Boids behavior model. The predator just has a “chase prey” behavior with a basic tendency to stay on the screen, from which the prey also inherit that same behavior.

sharkfish1

sharkfish2

I migrated my original code from using the Raphaël-JavaScript Graphics Library to using KineticJS which seems to be a little more widely used HTML5 Canvas framework. The grouping behavior might need more work so that the prey don’t occupy the same space. I’d also like to abstract the graphics library in the event it needs to change at some point in the future. I’d also like to visually model the shark and fish a bit more realistically.

Update: It took me some time to get around to this but I finally updated the code for the flocking behavior models.  Previously the avoidance function was not working very well. I spent some time and did some major refactoring, cleaning up the code a bit and fixing the avoidance function.  I’m fairly happy with the results now.

Leave a Reply