development & media



OpenGL Reef Tank - Back to main page
2002-04-14 Fish Movement

I've managed to get the fish in the OpenGL Reef Tank to swim around smoothly, without any sudden changes of direction (for the most part). Every fish has a timer that tells it to change velocity (speed and direction). A fish knows what its next velocity will be after the current timer runs out. During the countdown of the timer, it moves from its current velocity to its next velocity, by dividing of the velocity difference by the timer, so on each tick (i.e. frame redraw) it will move incrementally closer to the next velocity. Once it hits the next velocity (when the timer runs out), the timer is restarted, a new next velocity is calculated, and the process is restarted. The next velocity is calculated for non-schooling fish by simply picking a random velocity that is below the fish's maximum speed. The fish also have a parameter that describes their activeness (desire to change direction). If they have a low activeness parameter, instead of choosing a random next velocity, they will maintain their current one. For schooling fish, the process is a bit different, as described in a different column.

This process works fine by itself as long as there is nothing else in the tank (and if there are no tank walls). When the time comes to calculate the next velocity, collisions need to be taken into account. The simplest, avoiding the walls of the tank, is done by making sure the next velocity won't carry the fish outside of the tank. If it does, the fish simply reverses the velocity and heads the other way. The collision detection and avoidance systems are better described in another column.




©2002 shrugsoft - got a problem with that? drop us a line