How do I keep the fishes in the pond?

Hi, I’m new in this “Processing World”. I’m trying to keep the fishes in the pond. I’m using this code by Nicholas Tang -->

Please help me!


It’s a 2d sketch so you need to check the x,y position against the wall of the pond and then change the movement add / vector

I haven’t really looked at your code but something like this:

xadd = xadd * -1;



My steps in exploring this code:

  • reduced the code down to one fish to keep it simple and to work through an understanding of code.
  • translated everything so (0, 0) was at the center of the screen.
  • the boundaries were set as a fish bowl (circle) and I assumed the fish would avoid the glass and turn around without bumping its head or hitting its tail fins on glass; it always turns the same way (that is just how this fish is).

Below is NOT a solution although at first, it appeared to be so!

Initial exploration that seemed to work with my code (see GIF):

   if (sqrt((pos1[1]*pos1[1] + pos1[2]*pos1[2])) > 100/5 )  //inside circle
    f1.setth(f1.getth()+TAU/8); // fish turns

I had to fiddle with denominators in above (and fiddle with original code) but will leave that exploration up to you.

Avoiding glass and turning:




It does work with some tweaking:

  • update() and setfisht() after my code checks position
  • some tweaking when changing speed of fish
  • there is a scale factor of 18 in original code that I replaced with a variable mul in :
    translate(mul*pos[1] , mul*pos[2]);
    to replace

I will not be exploring this further anytime soon.

Have fun!


1 Like

beforehand, sorry for my english. Well, I made some changes … and the fish swims in circles.


In circles…?

That’s a bit boring

Can’t you give them different kind of movement and say swim straight or swim an 8 figure…

Or when they come near a wall make either a left or right turn

Or make a U turn sometimes?

Thanks for bringing this example to us!
I had some fun with it…

  • one lonely fish in a bubble
  • eyes blinking on fish
  • fins on fish flapping
  • colors changing

There are endless possibilities!
Animated background with bubbles and plants moving would be cool!

The fish is lonely and will add more; I will assume they live in a 3D world with depth and we are only viewing in 2D so will not be concerned about them bumping into each other; that is a challenge for another day.

In the future perhaps…

Don’t be sorry be happy! You can communicate in another language and learning a programming language.



1 Like

Here it is my progress, now i’m going to improve the layout and then to start the second part : Use the concept of genetic algorithm.

Thanks my dear