# How do I keep the fishes in the pond?

**URL:** <https://discourse.processing.org/t/how-do-i-keep-the-fishes-in-the-pond/13430>\
**Category:** Coding Questions\
**Created:** [August 17, 2019, 1:27pm UTC](https://discourse.processing.org/t/how-do-i-keep-the-fishes-in-the-pond/13430 "2019-08-17T13:27:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leonhard](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@Leonhard](https://discourse.processing.org/u/Leonhard)\
**Post date:** [August 17, 2019, 1:27pm UTC](https://discourse.processing.org/t/how-do-i-keep-the-fishes-in-the-pond/13430/1 "2019-08-17T13:27:29Z")

</div>

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 --\> [https://www.openprocessing.org/sketch/20105/](https://www.openprocessing.org/sketch/20105/).

Please help me!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [August 17, 2019, 4:01pm UTC](https://discourse.processing.org/t/how-do-i-keep-the-fishes-in-the-pond/13430/2 "2019-08-17T16:01:14Z")

</div>

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:

if(x\>400)  
xadd = xadd \* -1;

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 17, 2019, 6:53pm UTC](https://discourse.processing.org/t/how-do-i-keep-the-fishes-in-the-pond/13430/3 "2019-08-17T18:53:55Z")

</div>

Hello,

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).

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

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

```auto
   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:  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/d6035e2466bfb3fa946863c506ccf45c557edc34.png)

🙂

![fish](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1e519ea3dc1ed70c1e35dc51adbe2d36c11a8f48.gif)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 18, 2019, 2:37am UTC](https://discourse.processing.org/t/how-do-i-keep-the-fishes-in-the-pond/13430/4 "2019-08-18T02:37:56Z")

</div>

> [@glv](#):
>
> Update:  
> Below is **NOT** a solution although at first, it appeared to be so!

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  
`translate(width/2+18*pos[1],height/2+18*pos[2]);`

I will not be exploring this further anytime soon.

Have fun!

🙂

---

<div class="post-metadata">

**Author:** ![Leonhard](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@Leonhard](https://discourse.processing.org/u/Leonhard)\
**Post date:** [August 18, 2019, 4:21am UTC](https://discourse.processing.org/t/how-do-i-keep-the-fishes-in-the-pond/13430/5 "2019-08-18T04:21:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [August 18, 2019, 6:57am UTC](https://discourse.processing.org/t/how-do-i-keep-the-fishes-in-the-pond/13430/6 "2019-08-18T06:57:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 18, 2019, 12:40pm UTC](https://discourse.processing.org/t/how-do-i-keep-the-fishes-in-the-pond/13430/7 "2019-08-18T12:40:06Z")

</div>

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…

> [@Leonhard](#):
>
> sorry for my english.

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

Thanks!  
glv

---

<div class="post-metadata">

**Author:** ![Leonhard](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@Leonhard](https://discourse.processing.org/u/Leonhard)\
**Post date:** [August 18, 2019, 8:37pm UTC](https://discourse.processing.org/t/how-do-i-keep-the-fishes-in-the-pond/13430/8 "2019-08-18T20:37:02Z")

</div>

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/eb6d07266e71a903d2ac7c33e563469ff965787c.png)

---

<div class="post-metadata">

**Author:** ![Leonhard](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@Leonhard](https://discourse.processing.org/u/Leonhard)\
**Post date:** [August 18, 2019, 8:45pm UTC](https://discourse.processing.org/t/how-do-i-keep-the-fishes-in-the-pond/13430/9 "2019-08-18T20:45:37Z")

</div>

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
