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

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.



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.

16 thoughts on “Getting Schooled

  1. Sheldon

    Hi there! Do you know if they make any plugins to protect against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on.
    Any recommendations?

    1. TimGraf Post author

      I’m sorry I don’t know of any plugins off hand. Since most sites are hacked through some vulnerability my best recommendation would be to keep your WordPress software up to date as well as any plugins you are using. Of course frequent backups will also help in case something awful does happen.

      With that said, I did a search on the WordPress site and ran across this plugin. (URL below)

      It sounds decent and has good reviews. I hope this helps.


  2. pokemon x and y emulator

    Hello there! This post couldn’t be written much better!
    Going through this article reminds me of my previous roommate!
    He continually kept talking about this. I most certainly will forward this article to him.
    Pretty sure he’s going to have a very good read. Thanks for sharing!

  3. pokemon x and y emulator

    I’m not positive the place you’re getting your info,
    however good topic. I must spend a while learning much more or working out more.
    Thanks for fantastic information I was on the lookout for this information for my mission.

  4. clash of clans hack

    Hello there, just became aware of your blog through Google, and found that it is truly
    informative. I am gonna watch out for brussels. I
    will appreciate if you continue this in future. A lot of people will
    be benefited from your writing. Cheers!

  5. Matthew C. Kriner

    I just want to tell you that I’m very new to blogs and actually enjoyed you’re page. Very likely I’m planning to bookmark your site . You definitely have wonderful articles. Thanks a lot for sharing your blog site.

Leave a Reply