This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to implement my next waypoint for pathfinding. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to implement my next waypoint for pathfinding." Watch "How to implement my next waypoint for pathfinding." New topic

How to implement my next waypoint for pathfinding.

Ryan Tracey

Joined: Jan 20, 2011
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

Joined: Oct 13, 2005
Posts: 46424
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

Joined: Sep 20, 2010
Posts: 4642

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".

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
I agree. Here's the link:
subject: How to implement my next waypoint for pathfinding.
jQuery in Action, 3rd edition