Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# How to implement my next waypoint for pathfinding.

Ryan Tracey
Greenhorn
Posts: 6
Ok here is my robot (still some unfinished methods) but I was wondering if you could tell me if:
1) The logic behind the pathfinding is correct? (See code below)
2) Am I correctly using distance :S?
3) Waypoint is a class I created of type Waypoint(int,int), I have an array of these. How do I input the next Waypoint for the move method - I assume this is done through the main Class is this correct :S?

Campbell Ritchie
Sheriff
Posts: 50258
79
I am afraid the answer to all three questions is . . . it all depends.

We don't know the way your robot is supposed to move. What is the algorithm for moving? Write it down with pencil and paper, and keep breaking down each step until you get it to words of one syllable. Then you can see whether the logic is correct.

We don't know how you calculate distance moved; you appear only to move horizontally and vertically, like a rook in chess. But we can tell that method won't compile because of the extraneous [ and ]. Why are you stepping one at a time? Why not add the difference between your positions? Have you found the Math.abs() method?

What is a main Class? Do you mean a class with the main() method in? You would be better to create a method which instructs the Robot to move. You can pass the Waypoint[] array to it, and iterate through the array; for each element, pass that element to the move() method. The move() method now needs a Waypoint parameter.
You could, instead, pass a Waypoint[] to the move method, and iterate the array in the move method. But I think the first suggestion is better.

Stephan van Hulst
Bartender
Posts: 6337
79
I guess your question boils down to: "How do I find the shortest path to my destination?"

Personally I always use something called A* search. It's relatively fast at finding a good path to the destination.
It's hard to explain in a simple way without knowing the details of your project.

Also you should move to your next way point directly. If you first move horizontally and then vertically, you may end up "stuck behind a wall".