One of the most challenging things to make a realistic looking
fish tank is to keep things from swimming through other things.
I haven't quite gotten it right yet in the OpenGL Reef Tank, but
it works most of the time. At the beginning of the project, I just
was trying to get my fish to swim around smoothly, and to do that,
I ended up figuring out where each fish will be ahead of time, as
describe in another column. Doing that
really helped me out when it came time to keep the fish from colliding
with things. Here's what each fish does now to avoid collisions:
- First, a fish cycles though the other fish to see if it will
collide with any of them. If it is going to, the action taken
differs depending on the relative "rank" of the two
fish. A higher rank fish will ignore a lower rank fish, while
a lower rank fish will turn around if it's going to hit a higher
rank one. Two equal rank fish will each move to avoid the other.
- Second, a fish cycles through each rock to see if it will collide
with it. If it will, it will turn and swim directly away from
- Lastly, if a fish will end up going out of the tank on its next
move, it will turn around and slow down a bit.
Each successive step in that process will override the previous
ones, since each successive collision would be more serious if it
One problem with the above collision avoidance scheme is the cycling
through of objects in the tank. If a fish figures it will have to
change velocity to avoid the first fish (or rock) in its list, and
then decides it must change again for another one, it possibly could
go back to hitting the first one.