A pathfinding solution

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 ?

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?

i dont think i have change anything


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);
}

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

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);
}

yes now its work perfectly :slight_smile: 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

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:

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);
}

ok now i get it, many thanks for your time

1 Like

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

1 Like