# A pathfinding solution

**URL:** <https://discourse.processing.org/t/a-pathfinding-solution/1809>\
**Category:** Processing\
**Created:** [July 16, 2018, 10:58am UTC](https://discourse.processing.org/t/a-pathfinding-solution/1809 "2018-07-16T10:58:57Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [August 10, 2018, 8:57am UTC](https://discourse.processing.org/t/a-pathfinding-solution/1809/21 "2018-08-10T08:57:26Z")

</div>

Hi i have one more question,

the code work perfectly when i have one creature but when i have more than one creature, they all go to the closest water source.  
So i have three creatures and three water sources, the code find the closest water source for one creature (the creature with the shortest distant between that creature and a water source) and then all the creatures go to that water source.

How can i differenciate the creatures ?

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [August 10, 2018, 9:07am UTC](https://discourse.processing.org/t/a-pathfinding-solution/1809/22 "2018-08-10T09:07:13Z")

</div>

That’s weird, the code I gave you should differenciate the creature…

Can you copy/paste your code here to see what is going on?

---

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [August 10, 2018, 9:10am UTC](https://discourse.processing.org/t/a-pathfinding-solution/1809/23 "2018-08-10T09:10:42Z")

</div>

i dont think i have change anything

```auto

float currentSmallest = (parent.height * parent.height) + (parent.width * parent.width);
float d;
PVector target = null;
for(int a = carn1m.size()-1; a >= 0; a--){
	Carnivor1M c1m = carn1m.get(a);
	for(int b = lac.size()-1; b >= 0; b--){
		Lac l = lac.get(b);
		d = ((c1m.position.x - l.position.x) * (c1m.position.x - l.position.x)) + ((c1m.position.y - l.position.y) * (c1m.position.y - l.position.y));
					
		if(d < currentSmallest){
			currentSmallest = d;
			target = new PVector(l.position.x, l.position.y);
		}
	}
	c1m.arrive(target);
}

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [August 10, 2018, 9:12am UTC](https://discourse.processing.org/t/a-pathfinding-solution/1809/24 "2018-08-10T09:12:33Z")

</div>

My bad, you should reset the currentSmallest for every loop…

```auto
float currentSmallest; // CHANGE HERE
float d;

for(int a = carn1m.size()-1; a >= 0; a--){
    Carnivor1M c1m = carn1m.get(a);
    currentSmallest = (parent.height * parent.height) + (parent.width * parent.width); // CHANGE HERE
	for(int b = lac.size()-1; b >= 0; b--){
		Lac l = lac.get(b);
		d = ((c1m.position.x - l.position.x) * (c1m.position.x - l.position.x)) + ((c1m.position.y - l.position.y) * (c1m.position.y - l.position.y));
						
		if(d < currentSmallest){
			currentSmallest = d;
			PVector target = new PVector(l.position.x, l.position.y);
		}
	}
    c1m.arrive(target);
}

```

---

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [August 10, 2018, 9:23am UTC](https://discourse.processing.org/t/a-pathfinding-solution/1809/25 "2018-08-10T09:23:57Z")

</div>

yes now its work perfectly 🙂 thank you again !

i have a final question just for understand properly : when i put my PVector target in the if statement like you said i got an error in my c1m.arrive(target) saying “target cannot be resolved to a variable”  
whats the problem ?

its work without that change and i think its not a problem but im not sure

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [August 10, 2018, 9:38am UTC](https://discourse.processing.org/t/a-pathfinding-solution/1809/26 "2018-08-10T09:38:11Z")

</div>

It is because you declare the variable inside your `if` statement so it exists only here (If I’m not saying any non sense scope of variables are inside the block they are declared in).

What you should do is actualy declare your variable in the first for loop and then only update the x and y parameter lke so:

```auto
for(int a = carn1m.size()-1; a >= 0; a--){
  Carnivor1M c1m = carn1m.get(a);
  PVector target = new PVector(0, 0);
  currentSmallest = (parent.height * parent.height) + (parent.width * parent.width); 

  for(int b = lac.size()-1; b >= 0; b--){
    Lac l = lac.get(b);
    d = ((c1m.position.x - l.position.x) * (c1m.position.x - l.position.x)) + ((c1m.position.y - l.position.y) * (c1m.position.y - l.position.y));
						
    if(d < currentSmallest){
      currentSmallest = d;
      target.x = l.position.x;
      target.y = l.position.y;
    }
  }
  c1m.arrive(target);
}

```

---

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [August 10, 2018, 10:21am UTC](https://discourse.processing.org/t/a-pathfinding-solution/1809/27 "2018-08-10T10:21:28Z")

</div>

ok now i get it, many thanks for your time

---

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [September 6, 2018, 9:47am UTC](https://discourse.processing.org/t/a-pathfinding-solution/1809/28 "2018-09-06T09:47:42Z")

</div>

Hi guys thanks for yours help and time. Here what i made for now:

[![](https://img.youtube.com/vi/PCc20GYKsfo/maxresdefault.jpg "Ecosystem part 1") ](https://www.youtube.com/watch?v=PCc20GYKsfo&t=18s)

[Previous page](https://discourse.processing.org/t/a-pathfinding-solution/1809.md?page=1)
