I have problem with A while loop. Looks like it never ends and tryLadowanie() never runs. I guess something is wrong with this: while( (xPosition != xTarget) && (yPosition != yTarget) ). Update() works just fine, it goes from point A to point B just fine, but once it is in point B it still runs. What do you think ?
Here is my code:
public void lecimy(Lotnisko source, Lotnisko dest){
xPosition = source.coords.getX();
yPosition = source.coords.getY();
xTarget = dest.coords.getX();
yTarget = dest.coords.getY();
while( (xPosition != xTarget) && (yPosition != yTarget) ) {
update();
try {
sleep(100);// ok
}
catch (InterruptedException e) {
System.out.println("Error");
}
}
tryLadowanie();
}
public void update() {
paliwo -= 0.05;
double dx = xTarget - xPosition;
double dy = yTarget - yPosition;
double length = sqrt(dx*dx+dy*dy);
dx /= length;
dy /= length;
if (Math.abs(dest.coords.getX() - source.coords.getX()) < 1)
dx = 0;
if (Math.abs(dest.coords.getY() - source.coords.getY()) < 1)
dy = 0;
xPosition += dx;
yPosition += dy;
}
}
xPosition != xTarget) && (yPosition != yTarget. It is the problem with your condition inwhileloop.target, rather than exactly the same.doubletypes can be extremely long, and will be considered unequal even if they are0.00001off.