0

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;
    }
}
4
  • 1
    where did you declare xPosition and yPosition? are declared somewhere outside the two functions? static? Commented Jan 27, 2014 at 9:59
  • print and debug the value of xPosition != xTarget) && (yPosition != yTarget. It is the problem with your condition in while loop. Commented Jan 27, 2014 at 10:00
  • 1
    Try checking if it's close to the target, rather than exactly the same. double types can be extremely long, and will be considered unequal even if they are 0.00001 off. Commented Jan 27, 2014 at 10:00
  • xPosition and yPosition are double, declared outside the two functions, not static just double Commented Jan 27, 2014 at 10:04

2 Answers 2

4

You have a logical error:

You say: "if destination.X is closer than '1' from source.X then don't move it any closer (dx = 0)."

This can potentially go on forever.

To answer your comment-question (lack of space and editing in comment section):

Move the if (Math.abs(dest.coords.getX() - source.coords.getX()) < 1) and if (Math.abs(dest.coords.getY() - source.coords.getY()) < 1) out into the while loop's condition.

You don't want to stop changing the position when it's getting close inside the update() method, instead you want your loop to stop. Otherwise the loop will keep running and the update() method will do nothing.

Sign up to request clarification or add additional context in comments.

2 Comments

Like this: while( (Math.abs(dest.coords.getX() - source.coords.getX()) < 1) && (Math.abs(dest.coords.getY() - source.coords.getY()) < 1) )
Move it like this ? Or.. ?
3

Comparing double-variables using == or != is bound to get you in trouble, since the tiniest rounding-error will break your comparison. Use something like Math.abs(xPosition - xTarget) < tolerance.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.