Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (686)
Games in Android Showcase (198)
games submitted by our members
Games in WIP (758)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Help implementing path finding.  (Read 1439 times)
0 Members and 1 Guest are viewing this topic.
Offline syrinth

Senior Newbie





« Posted 2011-06-29 00:36:30 »

Hello all, I've looked at the sample pages listed in the pathfinding for beginners thread, and yet I'm still having problems implementing my pathfinding. The following code returns a path, and I'm sure the total value is the shortest path.

Unfortunately, what it does is it uses all the horizontal distance first, and then travels diagonally. This creates a very unnatural form of movement. I was hoping someone could help me?

I am using the Chebyshev distance equation listed at http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html#S7 to calculate the heuristic, I've checked to ensure that the G values are all being added properly and both forms of movement cost the same... What am I doing wrong?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
public void pathFind(Node target){
      this.target = target;
      open.add(node);
      target.setG(0);
      Node current = node;
      Node min = null;

      while(!open.isEmpty()){
         for(int i=0; i<current.getNeighbours().size(); i++){
            Node temp = current.getNeighbours().get(i);
            if(!open.contains(temp) && !closed.contains(temp)){
               open.add(temp);
               temp.setParent(current);
               temp.setG(current.getG() + current.getMovement());
               calculateF(temp);
            }
            else if(open.contains(temp) && !closed.contains(temp)){
               if(temp.getG() < current.getG()+current.getMovement()){
                  temp.setParent(current);
                  temp.setG(current.getG() + current.getMovement());
                  calculateF(temp);
               }
            }
         }
         open.remove(current);
         closed.add(current);
         min = open.get(0);
         for(int i=0; i<open.size(); i++){
            if(open.get(i).getF() < min.getF())
               min = open.get(i);
         }
         current = min;
         
         if(closed.contains(target)){
            deconstruct();
            break;
         }
      }
      System.out.println("FINISHED");
   }
   
   
   public void calculateF(Node aNode){
      int move = aNode.getStraight();
     
      aNode.setH(move * Math.max(Math.abs(aNode.getCentreX()-target.getCentreX()), Math.abs(aNode.getCentreY()-target.getCentreY())));
      aNode.setF(aNode.getG() + aNode.getH());
   }
Offline Gudradain
« Reply #1 - Posted 2011-06-29 04:04:49 »

can't understand question without drawing
Offline Jono
« Reply #2 - Posted 2011-06-29 06:56:11 »

Because the horizontal and diagonal moves both reduce the Chebyshev distance by the same amount (1), it just comes down to the ordering of the neighbours. I'd guess that your getNeighbours() method returns the horizontal move first and in case of ties your open list returns the earlier node.

One work-around is to add a penalty value in addition to f and g. Say, for each consecutive horizontal move add a 0.001 penalty. Just make sure that the penalty values for an entire path never add up to more than the cost of one move.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 
roseslayer (393 views)
2016-08-06 11:43:29

roseslayer (362 views)
2016-08-06 09:43:11

xTheGamerCodes (436 views)
2016-08-04 15:40:59

xTheGamerCodes (429 views)
2016-08-04 15:40:24

orrenravid (780 views)
2016-07-16 03:57:23

theagentd (855 views)
2016-07-11 14:28:54

Hydroque (953 views)
2016-07-06 05:56:57

Hydroque (981 views)
2016-07-03 08:52:54

GrandCastle (793 views)
2016-07-01 09:13:47

GrandCastle (634 views)
2016-07-01 09:09:45
Rendering resources
by Roquen
2016-08-08 05:55:21

Rendering resources
by Roquen
2016-08-08 05:52:42

Rendering resources
by Roquen
2016-08-08 05:50:38

Rendering resources
by Roquen
2016-08-08 05:49:53

Rendering resources
by Roquen
2016-08-08 05:32:39

Making a Dynamic Plugin System
by Hydroque
2016-06-25 00:13:25

Java Data structures
by BinaryMonkL
2016-06-13 21:22:09

Java Data structures
by BinaryMonkL
2016-06-13 21:20:42
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!