hi sorry for the long silence, i dont have a lot of day off and i do programing in my free time.
i tried your solution guys but i have a problem and i cant manage it.
for now thats my code:
the world: (where i launch everything)
float currentSmallest = (parent.height * parent.height) + (parent.width * parent.width);
float d;
float target;
for(int a = carn1m.size()-1; a >= 0; a--){
for(int b = lac.size()-1; b >= 0; b--){
Carnivor1M c1m = carn1m.get(a);
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 = currentSmallest;
//c1m.arrive(target);
}
}
}
my water source :
public class Lac {
PApplet parent;
ArrayList<PVector> lac;
public PVector position;
@SuppressWarnings("deprecation")
public Lac(PApplet p,PVector l){
lac = new ArrayList<PVector>();
parent = p;
position = l.get();
}
@SuppressWarnings("deprecation")
public void add(PVector l){
lac.add(l.get());
}
void update(){
parent.stroke(1);
parent.fill(0,182,210);
parent.ellipse(position.x, position.y, 50, 50);
}
public void run(PApplet p){
update();
}
public ArrayList<PVector> getLac(){
return lac;
}
}
and my creatur for the test :
public class Carnivor1M {
PApplet parent;
ArrayList<PVector> carn1m;
public PVector position;
DNA2 dna2;
float xoff;
float yoff;
////////////variables genetiques
float health;
float r; ///la taille
float maxspeed;
int lastChildTime = -1;
float maxforce;
PVector acceleration;
PVector velocity;
@SuppressWarnings("deprecation")
public Carnivor1M(PVector l, DNA2 dna2_, PApplet p){
carn1m = new ArrayList<PVector>();
parent = p;
position = l.get();
xoff = p.random(1000);
yoff = p.random(1000);
dna2 = dna2_;
health = PApplet.map(dna2.genes[0], 0, 1, 50, 250);
r = PApplet.map(dna2.genes[0], 0, 1, 10, 25);
maxspeed = PApplet.map(dna2.genes[0], 0, 1, 10, 1);
acceleration = new PVector(0,0);
velocity = new PVector(0,0);
maxforce = (float) 0.1;
}
void update(){
float vx = PApplet.map(parent.noise(xoff), 0, 1, -maxspeed, maxspeed);
float vy = PApplet.map(parent.noise(yoff), 0, 1, -maxspeed, maxspeed);
PVector velocity = new PVector(vx,vy);
xoff += 0.01;
yoff += 0.01;
position.add(velocity);
//health -= 0.1;
}
void borders(PApplet p){
if (position.x < -r) position.x = p.width+r;
if (position.y < -r) position.y = p.height+r;
if (position.x > p.width+r) position.x = -r;
if (position.y > p.height+r) position.y = -r;
}
void display(){
parent.ellipseMode(PConstants.CENTER);
parent.strokeWeight(2);
parent.stroke(0,0,0, health);
parent.fill(213, 0,0, health);
parent.ellipse(position.x, position.y, r, r);
}
public DNA2 getDNA2(){
return dna2;
}
public float getR(){
return r;
}
public ArrayList<PVector> getCarnivor1M(){
return carn1m;
}
public void run(PApplet p){
update();
borders(parent);
display();
}
void applyForce(PVector force){
acceleration.add(force);
}
public void arrive(PVector target){
PVector desired = PVector.sub(target, position);
float d = desired.mag();
if(d < 100){
float m = PApplet.map(d, 0, 100, 0, maxspeed);
desired.setMag(m);
}
else{
desired.setMag(maxspeed);
}
PVector steer = PVector.sub(desired, velocity);
steer.limit(maxforce);
applyForce(steer);
}
}
so here is my problem: i need to turn the pythagorean theorem that jb4x explain to me into a PVector.
I have my float for the currentSmallest, the d and the target.
But when i launch c1m.arrive (my creature test) i need a PVector and not a float and i dont know how.
I think its not a good idea to change the creature or water source class, i tried and it come really mess up really fast.
can you help me find a solution ?
Thanks again for your times guys.