Hi!
Today I'm happy to present an academic project we made at
EPFL for the
School of Life Sciences. In two words, it simulates a population of mice (the preys) and another one of scorpions (the predators) with pretty much a full life cycle: from born to death, with reproduction and predation.
A second part of the simulation consists in building a neuronal network, with sensors & inhibitors, to models the behaviour of a scorpion tracking its preys by sensing the ground's vibrations made by mice. That was the fun (understand: tricky :-) part of the project.
This project was then implemented during the spring semester by more than 180 first-year students in C++ using SFML for the rendering.
Unfortunately, I'm not allowed to distribute the source code. I can only give you a list of features present in this project.
- The window is divided into two parts. The main one displays the real time simulation of mice and scorpion. We can zoom in / out and move the camera around. The second view can display different kind of statistics: students where asked to keep track of the number of mice, scorpion, food, and so on. For a bonus, they could add, for example, a chart representing the evolution of the different simulated populations.
- The simulation was held in a toroidal world where animals can only see foods, mates and predators in their eye sight. If an animal sees nothing of interest it will randomly walk until it finds something to eat, mate or to run away from – or starve…
- Both scorpions and mice have male and female individuals that can mate when they are close enough, not too much hungry, etc… Usually, mice create between 1 and 6 babies whereas scorpion have between 20 and 28 «things» (see gif below).
- When a scorpion sees a mouse it will try to run toward it and eat it. But if a mouse see the scorpion first (e.g. the scorpion is not coming from behind) it will try to escape and run faster in the opposite direction.
- Many parameters rule the simulation. We used a simple file format to store every settings in a config file that we can edit in real time during the simulation (modulo pressing `L` to reload the file). These settings include, for example, a debug-mode boolean, a time factor to slow down or speed up the simulation, paths to the different textures used for rendering, energy consumption factors, animals masses, view ranges, … and a lot more!
- The build system is based on scons and the implementation takes advantage of some new features from C++11 like `auto`, range-based for loops or the random number generators to mention only those three. We didn't used templates or advanced C++(11) features like move semantics because the students are only beginning to code. Moreover, they are not software engineer but life scientist so we can't expect from them to code like pro after less than a year.
- We provided the students with a few core functionalities like stubs for the core `Application` class that handles event management, updating the logics and drawing (they had to modify it to insert their own environment and all). We also give them a `Vec2d` class to do most of the mathematical operation you can expect from such type, with a nice conversion operator to `sf::Vector2f` to do the interface between their code and SFML API easily. We also gave them a texture manager so they could focus more on the simulation logics (and not on white square).
- All entities are stored in a heterogenous collection. We used double dispatch to tell if a given entity should be attracted to, eat or feat another entity – so no type test or casting!
Now, enough with this blabla. Here are some screenshots:
A scorpion tracking a mouse:
Mice trying to run away from scorpions:
The birth of scorpions (beurk!):
A slice of simulation:
Generating entities:
A scorpion moved by a «random walk» algorithm until it sees its target:
Debug mode ON – view range, status, collision circle, … are drawn:
Zoom in:
The sensors and neuronal network of a scorpion:
A wave activating a sensor:
A scorpion aware of the mouse:
A peaceful world with only mice and salads!
Higher resolution images can be found at
http://sdrv.ms/1aP3KeKAs I said, I cannot show you any piece of code. However, if you have a question on a particular point I probably can give you more-but-not-too-precise-details. ;-)
Btw, it's the first time we use SFML at EPFL for teaching (I kinda pushed toward it because before that we used an ugly, home made, library based on SDL and OpenGL). And next year we will also used SFML for another awesome project!