Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (515)
Games in Android Showcase (122)
games submitted by our members
Games in WIP (577)
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 1519 times)
0 Members and 1 Guest are viewing this topic.
Offline gioppo

Junior Newbie





« Posted 2010-05-13 12: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

JGO Kernel


Medals: 409
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2010-05-13 12: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 19:31:35 »

Yes sorry for the bad description.
L
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


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

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

See my work:
OTC Software
Offline Nate

JGO Kernel


Medals: 153
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #4 - Posted 2010-05-14 06: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 14:21:57 »

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

 

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

The first screenshot will be displayed as a thumbnail.

TehJavaDev (30 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (39 views)
2014-10-26 19:37:11

Luminem (21 views)
2014-10-26 10:17:50

Luminem (25 views)
2014-10-26 10:14:04

theagentd (31 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (57 views)
2014-10-16 15:22:06

Norakomi (46 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!