Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
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  
  terrain cost  (Read 1298 times)
0 Members and 1 Guest are viewing this topic.
Offline gioppo

Junior Newbie





« Posted 2010-05-13 14:32:08 »

Need to implement a terrain cost on a 2D map any advice on how to do it?
Are there any good solution to adopt?
Any lib to use?
I'm really new to the subject so any idea is welcome.
Thanks
Luca
Offline princec
« League of Dukes »

JGO Kernel


Medals: 194
Projects: 3


Eh? Who? What? ... Me?


« Reply #1 - Posted 2010-05-13 14:49:06 »

Your question doesn't really mean anything.

I think you mean to ask: I wish to develop a pathfinding solution for a 2D map which contains terrain cost information. Is that right?

Cas Smiley

Offline gioppo

Junior Newbie





« Reply #2 - Posted 2010-05-13 21:31:35 »

Yes sorry for the bad description.
L
Games published by our own members! Check 'em out!
Try the Free Demo of Titan Attacks
Offline Eli Delventhal
« League of Dukes »

JGO Kernel


Medals: 39
Projects: 12


Game Engineer


« Reply #3 - Posted 2010-05-13 23:25:41 »

A*
http://www.cokeandcode.com/node/1087

See my work:
OTC Software
Offline Nate

JGO Wizard


Medals: 81
Projects: 3


Esoteric Software


« Reply #4 - Posted 2010-05-14 08:01:10 »

Below is a simple, unoptimized A* implementation. It has a main method to test it out in a JFrame. I suggest trying to implement it yourself first using Wikipedia, or at least being sure you understand what the below is doing, still using Wikipedia. Ask questions for all the parts you don't understand.

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  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.HashSet;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class PathFinder {
   ArrayList<Node> open = new ArrayList();
   HashSet<Node> closed = new HashSet();
   int width, height;
   int[][] map;
   int targetX, targetY;
   int startX, startY;
   private JFrame frame;

   public static void main (String[] args) {
      PathFinder p = new PathFinder();
      p.width = 18;
      p.height = 16;
      p.map = new int[p.width][p.height];
      p.map[4][1] = -1;
      p.map[4][2] = -1;
      p.map[4][3] = -1;
      p.startX = 2;
      p.startY = 2;
      p.targetX = 16;
      p.targetY = 12;
      p.run();
   }

   private void run () {
      frame = new JFrame();
      frame.getContentPane().setLayout(new GridLayout(height, width, 5, 5));
      frame.addMouseMotionListener(new MouseMotionListener() {
         public void mouseDragged (MouseEvent e) {
         }

         public void mouseMoved (MouseEvent e) {
            startX = (int)(e.getX() / 800f * width);
            startY = (int)(e.getY() / 600f * height);
            update();
         }
      });
      frame.setSize(800, 600);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      update();
   }

   void update () {
      frame.getContentPane().removeAll();
      ArrayList<int[]> path = go();
      for (int y = 0; y < height; y++) {
         for (int x = 0; x < width; x++) {
            JPanel panel = new JPanel();
            panel.setOpaque(true);
            panel.setBackground(map[x][y] == 0 ? Color.black : Color.red);
            if (path != null) {
               for (int[] coord : path)
                  if (x == coord[0] && y == coord[1]) panel.setBackground(Color.green);
            }
            if (x == startX && y == startY) panel.setBackground(Color.blue);
            if (x == targetX && y == targetY) panel.setBackground(Color.blue);
            frame.getContentPane().add(panel);
         }
      }
      frame.validate();
   }

   public ArrayList<int[]> go () {
      long start = System.nanoTime();

      open.clear();
      closed.clear();

      Node root = new Node(null, startX, startY, 0);
      open.add(root);

      while (!open.isEmpty()) {
         int lowestIndex = -1;
         int lowestCost = Integer.MAX_VALUE;
         for (int i = 0, n = open.size(); i < n; i++) {
            Node node = open.get(i);
            int cost = node.g + node.h;
            if (cost < lowestCost) {
               lowestCost = cost;
               lowestIndex = i;
            }
         }

         Node check = open.remove(lowestIndex);
         if (check.x == targetX && check.y == targetY) {
            ArrayList<int[]> path = new ArrayList();
            while (check != root) {
               path.add(0, new int[] {check.x, check.y});
               check = check.parent;
            }

            long end = System.nanoTime();
            System.out.println((end - start) / 1000000f);

            return path;
         }
         closed.add(check);

         addNode(check, check.x, check.y + 1, 10);
         addNode(check, check.x, check.y - 1, 10);
         addNode(check, check.x + 1, check.y, 10);
         addNode(check, check.x - 1, check.y, 10);
         addNode(check, check.x + 1, check.y + 1, 14);
         addNode(check, check.x - 1, check.y - 1, 14);
         addNode(check, check.x + 1, check.y - 1, 14);
         addNode(check, check.x - 1, check.y + 1, 14);
      }

      return null;
   }

   private void addNode (Node parent, int x, int y, int cost) {
      if (x < 0 || y < 0) return;
      if (x >= width || y >= height) return;
      if (map[x][y] != 0) return;
      Node node = new Node(parent, x, y, cost);
      if (closed.contains(node)) return;
      int existingIndex = open.indexOf(node);
      if (existingIndex == -1) {
         open.add(node);
      } else {
         Node existing = open.get(existingIndex);
         if (node.g < existing.g) {
            existing.parent = parent;
            existing.g = node.g;
         }
      }
   }

   private class Node {
      public Node parent;
      public int x, y;
      public int g, h;

      public Node (Node parent, int x, int y, int cost) {
         this.parent = parent;
         this.x = x;
         this.y = y;
         g = parent == null ? 0 : parent.g + cost;
         h = Math.abs(x - targetX) + Math.abs(y - targetY);
         h *= 10;
      }

      public int hashCode () {
         int result = 1;
         result = 31 * result + x;
         result = 31 * result + y;
         return result;
      }

      public boolean equals (Object object) {
         Node node = (Node)object;
         return node.x == x && node.y == y;
      }
   }
}

Offline gioppo

Junior Newbie





« Reply #5 - Posted 2010-05-15 16:21:57 »

Thanks will look at it!!!
L
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (37 views)
2013-05-17 21:29:12

alaslipknot (46 views)
2013-05-16 21:24:48

gouessej (75 views)
2013-05-16 00:53:38

gouessej (75 views)
2013-05-16 00:17:58

theagentd (83 views)
2013-05-15 15:01:13

theagentd (77 views)
2013-05-15 15:00:54

StreetDoggy (119 views)
2013-05-14 15:56:26

kutucuk (143 views)
2013-05-12 17:10:36

kutucuk (143 views)
2013-05-12 15:36:09

UnluckyDevil (153 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.469 seconds with 20 queries.