Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1] 2
1  Game Development / Newbie & Debugging Questions / Re: A* Pathfinding Problem...like big one. on: 2011-10-06 15:34:35
Thanks guys. I also moved more stuff around and tried to reduce clutter and some problems that might occur.

This is what I have:

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  
public class Pathfinder {

   Map map;
   ArrayList<Square> open = new ArrayList<Square>();
   HashSet<Square> closed = new HashSet<Square>();
   
   
   Pathfinder(Map map) {
      this.map = map;
   }
   
   public ArrayList<int[]> getPath(int sy, int sx, int gy, int gx) {
      Iterator itr = closed.iterator();
      open.add(new Square(sy,sx));
     
      boolean found = false;
     
      while (!found) {
         
         Square current = null;
         int minF = 0;
         int ind = 0;
         
         if (open.size() > 0) {
            for (Square s : open) {
               if (minF == 0)
                  s.F = minF;
               
               if (s.F <= minF) {
                  minF = s.F;
                  ind = open.indexOf(s);
                  current = open.get(ind);
                  closed.add(current);
                 
                  if (open.get(ind).x == gx && open.get(ind).y == gy)
                     found = true;
                 
                  open.remove(ind);
               }
            }
         
            //Get adjacent squares
           ArrayList<Square> adjectant = map.getAdjacent(current);
           
            //Parse adjacent squares
           for (Square s: adjectant) {
               
               if (map.getAt(s.y, s.x) == 1 || containsNode(closed,s)) {
                  if (!containsNode(open,s)) {
                 
                     s.parent = current;
                     s.H = (int) ( 10*( Math.sqrt(current.x-gx) + Math.sqrt(current.y-gy)));
                     s.getF();
                     open.add(s);
                  } else {
                     if (s.G < current.G) {
                        s.parent = current;
                        s.H = (int) ( 10*( Math.sqrt(current.x-gx) + Math.sqrt(current.y-gy)));
                        s.getF();
                     }
                  }
                     
               }
               
               
            }
           
         } else {
            System.out.println("Openlist empty. No Path found.");
            return null;
         }
         
      }
     
      //Save Path (first backwards then forwards)
     ArrayList<int[]> nodes_backwards = new ArrayList<int[]>();
      ArrayList<int[]> nodes = new ArrayList<int[]>();
     
      while (itr.hasNext()) {
         Square s = (Square) itr.next();
         int[] tmp = {s.y,s.x};
         nodes_backwards.add(tmp);
         s = s.parent;
         
         if (s.x == sx && s.y == sy) {
            break;
         }
         
         
      }
     
      for (int i=nodes_backwards.size()-1; i >= 0; i--) {
         nodes.add(nodes_backwards.get(i));
      }
     
     
      return nodes;
   }
   
   private boolean containsNode(ArrayList<Square> alist, Square s) {
     
      for (Square aSq: alist) {
         if (aSq.x == s.x && aSq.y == s.y)
            return true;
         
      }
     
      return false;
   }
   
   private boolean containsNode(HashSet<Square> alist, Square s) {
      Iterator<Square> itr = closed.iterator();
     
      while (itr.hasNext()) {
         Square aSq = itr.next();
         if (aSq.x == s.x && aSq.y == s.y)
            return true;
         
      }
     
      return false;
   }
   
}


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  
public class Map {

   int[][] grid = new int[10][10];
   int HEIGHT = 10;
   int WIDTH = 10;
   
   Map() {
     
      for (int i=0; i < HEIGHT; i++) {
           for (int f=0; f < WIDTH; f++) {
              grid[i][f] = 0;
           }
           
        }
     
      grid[0][0] = 1;
     
     
      grid[5][5] = 1;
      grid[4][5] = 1;
      grid[3][5] = 1;
     
      grid[3][6] = 1;
      grid[3][7] = 1;
      grid[3][8] = 1;
     
      grid[7][7] = 1;
      grid[7][8] = 1;
     
     
   }
   
   public int getAt(int y,int x) {
      return grid[x][y];
   }
   
   public int[] getNode(int y,int x) {
      int[] pos = {64*(x-1),64*y};
      System.out.println(pos[0] +","+pos[1]);
      return pos;
   }
   
   public ArrayList<Square> getAdjacent(Square current) {
     
      ArrayList<Square> adjacent = new ArrayList<Square>();
      int x = current.x;
      int y = current.y;
     
      for (int i=-1; i<=1;i++) {
         for (int f=-1; f<=1;f++) {
           
            try {
               getAt(i,f);
               //No Errors continue:
              adjacent.add(new Square(y+i,x+f));
               
            }catch (Exception e) {
               //No adjacent found.
           }
           
         }
      }
     
     
   return adjacent;
   }
   
}


Quote
Exception in thread "LWJGL Application" java.util.ConcurrentModificationException
   at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
   at java.util.AbstractList$Itr.next(Unknown Source)
   at de.pariapluto.apps.Pathfinder.getPath(Pathfinder.java:31)
   at de.pariapluto.apps.MainGame.create(MainGame.java:35)
   at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:102)
   at com.badlogic.gdx.backends.lwjgl.LwjglApplication.access$000(LwjglApplication.java:42)
   at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:89)

(Line 25 in the HTML Version of Pathfinder in this Post)

this is the Error I'm getting. It has something to with the iteration and synchronisation. But I'm irritated since the open list, is an ArrayList and shouldn't have anything todo with the iterator I'm using for the HashSet.
2  Game Development / Newbie & Debugging Questions / Re: A* Pathfinding Problem...like big one. on: 2011-10-05 23:18:19
That did not seem to fix my problem, it still freezes on loading.
Here is my new Pathfinder class, are you sure that that was the only problem, I wouldn't be confused if there are some more mistakes I made. I followed the instructions, but maybe I missed or missunderstood something.

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  
public class Pathfinder {

   Map map;
   ArrayList<Square> open = new ArrayList<Square>();
   HashSet<Square> closed = new HashSet<Square>();
   
   
   Pathfinder(Map map) {
      this.map = map;
   }
   
   public ArrayList<int[]> getPath(int sy, int sx, int gy, int gx) {
      Iterator itr = closed.iterator();
      open.add(new Square(sy,sx));
     
      boolean found = false;
     
      while (!found) {
         
         Square current;
         int minF = 0;
         int ind = 0;
         
         if (open.size() > 0) {
            for (Square s : open) {
               if (minF == 0)
                  s.F = minF;
               
               if (s.F < minF) {
                  minF = s.F;
                  ind = open.indexOf(s);
               }
            }
         } else {
            System.out.println("Openlist empty. No Path found.");
            return null;
         }
         
         
         closed.add(open.get(ind));
         
         
         if (open.get(ind).x == gx && open.get(ind).y == gy)
            found = true;
         
         current = open.get(ind);
         
         open.remove(ind);
         
         //Get adjacent squares
        ArrayList<Square> adjectant = getAdjacent(current);
         
         //Parse adjacent squares
        for (Square s: adjectant) {
           
            if (map.getAt(s.y, s.x) == 1 || closed.contains(s)) {
               if (!open.contains(s)) {
               
                  s.parent = current;
                  s.H = (int) ( 10*( Math.sqrt(current.x-gx) + Math.sqrt(current.y-gy)));
                  s.getF();
                  open.add(s);
               } else {
                  if (s.G < current.G) {
                     s.parent = current;
                     s.H = (int) ( 10*( Math.sqrt(current.x-gx) + Math.sqrt(current.y-gy)));
                     s.getF();
                  }
               }
                 
            }
           
           
         }
         
         
      }
     
      ArrayList<int[]> nodes_backwards = new ArrayList<int[]>();
      ArrayList<int[]> nodes = new ArrayList<int[]>();
     
      while (itr.hasNext()) {
         Square s = (Square) itr.next();
         int[] tmp = {s.y,s.x};
         nodes_backwards.add(tmp);
         s = s.parent;
         
         if (s.x == sx && s.y == sy) {
            break;
         }
         
         
      }
     
      for (int i=nodes_backwards.size()-1; i >= 0; i--) {
         nodes.add(nodes_backwards.get(i));
      }
     
     
      return nodes;
   }
   
   private ArrayList<Square> getAdjacent(Square current) {
     
      ArrayList<Square> adjacent = new ArrayList<Square>();
      int x = current.x;
      int y = current.y;
     
      if (y-1 >= 0) {
     
         if (x-1 >= 0)
            adjacent.add(new Square(y-1,x-1,true));            
         
         adjacent.add(new Square(y-1,x));
         
         if (x+1 <= 10)
            adjacent.add(new Square(y-1,x+1,true));            
         
      }
     
      if (x-1 >= 0)
         adjacent.add(new Square(y,x-1));
     
      if (x+1 <= 10)
         adjacent.add(new Square(y,x+1));            
     
      if (y+1 <= 10) {
         
         if (x-1 >= 0)
            adjacent.add(new Square(y+1,x-1,true));            
         
         adjacent.add(new Square(y+1,x));
         
         if (x+1 <= 10)
            adjacent.add(new Square(y+1,x+1,true));            
         
      }            
     
     
      return adjacent;
   }
   
}
3  Game Development / Newbie & Debugging Questions / Re: A* Pathfinding Problem...like big one. on: 2011-10-05 23:05:28
But how do I retrieve values from the Hashset? There is not .get() function.
Something, something iterator?

Oh and does the rest I programmed even look moderatly fine, or was the HashSet the first thing that u saw?
4  Game Development / Newbie & Debugging Questions / A* Pathfinding Problem...like big one. on: 2011-10-05 22:01:16
Hi, I tried to implement A*-Pathfinding, but it does not work.

It crashes on me, as in the Java-App freezes. It freezes before the render() method is called, so its stuck somewhere in my A*-Algorithm.

Short description of the Map, its a 2D-Array of Integers and a 1 is a wall, 0 is nothing.
Thats why I had to do everything in y,x not x,y.

This is my Pathfinding Class:

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  
public class Pathfinder {

   Map map;
   ArrayList<Square> open = new ArrayList<Square>();
   ArrayList<Square> closed = new ArrayList<Square>();
   
   
   Pathfinder(Map map) {
      this.map = map;
   }
   
   public ArrayList<int[]> getPath(int sy, int sx, int gy, int gx) {
     
      open.add(new Square(sy,sx));
     
      boolean found = false;
     
      while (!found) {
         
         Square current;
         int minF = 0;
         int ind = 0;
         
         if (open.size() > 0) {
            for (Square s : open) {
               if (minF == 0)
                  s.F = minF;
               
               if (s.F < minF) {
                  minF = s.F;
                  ind = open.indexOf(s);
               }
            }
         } else {
            System.out.println("Openlist empty. No Path found.");
            return null;
         }
         
         
         closed.add(open.get(ind));
         
         
         if (open.get(ind).x == gx && open.get(ind).y == gy)
            found = true;
         
         open.remove(ind);
         
         current = closed.get(closed.size()-1);
         
         //Get adjacent squares
        ArrayList<Square> adjectant = getAdjacent(current);
         
         //Parse adjacent squares
        for (Square s: adjectant) {
           
            if (map.getAt(s.y, s.x) == 1 || closed.contains(s)) {
               if (!open.contains(s)) {
               
                  s.parent = current;
                  s.H = (int) ( 10*( Math.sqrt(current.x-gx) + Math.sqrt(current.y-gy)));
                  s.getF();
                  open.add(s);
               } else {
                  if (s.G < current.G) {
                     s.parent = current;
                     s.H = (int) ( 10*( Math.sqrt(current.x-gx) + Math.sqrt(current.y-gy)));
                     s.getF();
                  }
               }
                 
            }
           
           
         }
         
         
      }
     
      boolean saved = false;
      ArrayList<int[]> nodes_backwards = new ArrayList<int[]>();
      ArrayList<int[]> nodes = new ArrayList<int[]>();
     
      Square s = closed.get(closed.size()-1);
     
     
      while (!saved) {
         
         int[] tmp = {s.y,s.x};
         nodes_backwards.add(tmp);
         s = s.parent;
         
         if (s.x == sx && s.y == sy) {
            saved = true;
         }
         
         
      }
     
      for (int i=nodes_backwards.size()-1; i >= 0; i--) {
         nodes.add(nodes_backwards.get(i));
      }
     
     
      return nodes;
   }
   
   private ArrayList<Square> getAdjacent(Square current) {
     
      ArrayList<Square> adjacent = new ArrayList<Square>();
      int x = current.x;
      int y = current.y;
     
      if (y-1 >= 0) {
     
         if (x-1 >= 0)
            adjacent.add(new Square(y-1,x-1,true));            
         
         adjacent.add(new Square(y-1,x));
         
         if (x+1 <= 10)
            adjacent.add(new Square(y-1,x+1,true));            
         
      }
     
      if (x-1 >= 0)
         adjacent.add(new Square(y,x-1));
     
      if (x+1 <= 10)
         adjacent.add(new Square(y,x+1));            
     
      if (y+1 <= 10) {
         
         if (x-1 >= 0)
            adjacent.add(new Square(y+1,x-1,true));            
         
         adjacent.add(new Square(y+1,x));
         
         if (x+1 <= 10)
            adjacent.add(new Square(y+1,x+1,true));            
         
      }            
     
     
      return adjacent;
   }
   
}


My adjacent finding method is pretty shitty. Because I didn't know how else to get all the adjacent tiles, without running into problems at the borders.

Square class:
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  
public class Square {

   int F,G;
   int H;
   int x,y;
   Square parent;
   boolean diag;

   Square(int y, int x) {
      this.x = x;
      this.y = y;
      G = 10;
   }
   
   Square(int y, int x, boolean diag) {
      this.x = x;
      this.y = y;
     
      if (diag)
         G = 14;
   }
   
   public void getF() {
      F = G + H;
   }
   
}


And my Map class:

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  
public class Map {

   int[][] gitter = new int[10][10];
   
   Map() {
     
      for (int i=0; i < gitter.length; i++) {
           for (int f=0; f < gitter.length; f++) {
              gitter[i][f] = 0;
           }
           
        }
     
      gitter[0][0] = 1;
     
     
      gitter[5][5] = 1;
      gitter[4][5] = 1;
      gitter[3][5] = 1;
     
      gitter[3][6] = 1;
      gitter[3][7] = 1;
      gitter[3][8] = 1;
     
      gitter[7][7] = 1;
      gitter[7][8] = 1;
     
     
   }
   
   public int getAt(int y,int x) {
      return gitter[x][y];
   }
   
   public int[] getNode(int y,int x) {
      int[] pos = {64*(x-1),64*y};
      System.out.println(pos[0] +","+pos[1]);
      return pos;
   }
   
   
}


(gitter is german for grid)
5  Game Development / Game Play & Game Design / Re: Risk type game on: 2011-10-04 18:45:45
You can check our what I posted on HoI / Total War style maps in this forum and think about your Risk game again.

I'm just saying, go with something like Pong (make your own version, trippy, stylish etc.) or even better a simple 2D Plattformer.

Don't go overboard with yourself, you will only become frustrated. And 3 months of programming experience is basically nothing.
I'd say come back when you mastered GUIs and all the important aspects, especially OOP (Object-oriented Programming).

I'm just saying, but I can't stop you, if you feel confident go for it.
6  Game Development / Game Mechanics / Re: HoI/Total War Style Map on: 2011-09-27 23:19:48
I'm currently bound to libgdx, because thats the library that supports Java for Android.
I'd rather use Slick but slick doesnt have a android library yet.

I think I'm going for the image splitting and colorcode version. Let's hope that works.

I'm still open for new attempts at solving this problem.
7  Game Development / Game Mechanics / Re: HoI/Total War Style Map on: 2011-09-27 22:47:20
Oh wow. Thats exactly what I need.
I'm not great in JS so I can't really translate this to java.

How does this work, it seems to not use a colorcoded image and a metadata file like Orangy explained, only the baseimage. This would save alot of time if there was something similiar to this in Java.

I'm really getting worried this game will be more time consuming than I imagined it to be...not that that would be bad, its just with the more work I put into it the bigger the danger gets that I get frustrated and abandon the game at some point, which would actually be too bad, because I really like my idea and would most defintitly want to play it myself  Clueless

And I thought the KI will be hard to code  Roll Eyes
8  Game Development / Game Mechanics / Re: HoI/Total War Style Map on: 2011-09-27 08:14:30
Yeah^^ I just noticed that when I went to bed
9  Game Development / Game Mechanics / Re: HoI/Total War Style Map on: 2011-09-26 22:14:18
If I understood correctly:

1.Grind through all pixels.
2.Find the most upperleft pixel and the most lowerright pixel.
3.Create a bounding box.
4.Then copy the pixels from the worldmap onto a new image, leaving all other pixels translucent.
5.The offset is the most most upperleft pixel
6.Store the new image in the province object.

Sounds like a plan and I will most defintily make an offline tool. Thats crazy amount of grinding. And since this game is supposed to be for android. I rather not give the droid a huge amount of data to go through.

Also that seals how I will make my map editor Cheesy
10  Game Development / Game Mechanics / Re: HoI/Total War Style Map on: 2011-09-26 22:02:11
Thats a pretty good idea. I like that alot. Picking that way should be rather easy that way.

I'm only irritated on how you would actually slice them up.
11  Game Development / Game Mechanics / Re: HoI/Total War Style Map on: 2011-09-26 21:43:08
Okay, I haven't gotten very far, mainly because I still tried it with vectors. I now trashed that idea, because its too hard for me.

After starting with your idea of every province a image, I soon got confronted with a problem: aligning the images to one big map.
e.g. making them fit snuggly together so there is no gap between them.

Maybe there is a trick to this world map...hexagons or some kind of hidden tiling.

Any ideas, even the most weirdest are welcome. No answer is wrong...i guess ^^

12  Game Development / Game Mechanics / Re: HoI/Total War Style Map on: 2011-09-26 15:51:35
Thanks, that should work. The whole polygon thing didnt want to work anyways...dont know i guess my math wasnt to great.
13  Game Development / Game Mechanics / Re: HoI/Total War Style Map on: 2011-09-26 13:04:32
But how would you pick an image? I understand, that you would technically pick and check if the pixel is not transparent. But how would you know what image you are picking. Also how would you tint the image with the corrospondent owner color.

I just talked to my father, who programms in graphics and has done alot of picking in them. He said I could make them all meshes (he has the point data) and fill them via opengl. (would look rather fugly though.) and then pick them.

My thought was to make the armys and cities as seperate images ontop of one big worldmap and only make those clickable.
I would make armys and cities as seperate images with a hitzone so I can pick them. Id still have the problem of not being able to tint the provinces.
14  Game Development / Game Mechanics / HoI/Total War Style Map on: 2011-09-25 18:03:44
Hello Everyone,

for my new project I need a Campaignmap in the style of Hearts of Iron or Total War.
Heres a pic if you dont know what it is:


I got a basic idea:

A province is a object holding all the information, e.g. position (x,y), id, adjectant province ids, sprite and a polygon for collision detection.
I would then render the images with the x,y position and if the player clicks on one of the provinces I know which one, via point in polygon.

Is this a good idea? Or does anybody have a better idea? Especially with the adjectant provinces or map managment?
With polygons it would get very tedious to actually make the map, as I would have to get a map with point data and then manually create a polygon for each province.
15  Game Development / Newbie & Debugging Questions / Re: Networking on: 2011-06-06 13:57:08
I actually did that^^
The class shown in my first post is older.

Thats why I am confused.
16  Game Development / Newbie & Debugging Questions / Re: Level Editor on: 2011-06-05 17:26:51
Terraria has basically made a huge grid. Each "block" in the grit has a id (e.g. stone,silver etc. or maybe numbers like 02,05).

The game then starts to render the gameblocks according to the grid. The player in Terraria moves freely, and only interacts with the blocks on the grid, not the grid itself. 
17  Game Development / Newbie & Debugging Questions / Re: Networking on: 2011-06-05 17:14:10
I've got a new Problem.

In my Player Class I have implemented a ArrayList<Missile>. Missile is a serializable Class and is just the missile.
I can add missiles client sided, but when I send the Player class containing the ArrayList with missiles the arraylist ist empty.

This is fairly weird. Since I can send ArrayList with no problem on its own. Is there a certain method I have to use here?
18  Game Development / Newbie & Debugging Questions / Re: Networking on: 2011-05-25 10:08:47
EDIT: Found the problem. I needed to oos.reset(); the OutputStream, so it would disregard the object if it has been sent.
19  Game Development / Game Mechanics / Re: Customizable avatars in your RPG on: 2011-05-24 18:47:36
A skeleton would only make sense, if youd have a ragdoll. Because in 2D you  are only moving images, and can't morph them, without getting very intense with math and it wouldn't look good Wink
20  Game Development / Newbie & Debugging Questions / Networking on: 2011-05-24 18:38:53
Hello all together Cheesy

I'm currently trying to get a network game going.
I'm using Sockets and Objekt Serialization, because thats what we are doing in class and I need to use them later in exams.
After alot of working and trying out, I got the Server to send an Object and the Client to recieve it.

But for some miraculous reason, it will send the Object and recieve it, but will not update it.
So its something client sided. Because the server only recieves the old object.

So it will send the Player Object: with x 100 and y 100. Eventhough I updated it on the client side to eg x 140 and y 132

Oh and when your checking it, maybe someone could tell me why I get two connections when connecting to the server?

Thanks alot Cheesy

Heres the code:

Server:

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  
import java.io.*;
import java.net.*;

public class Server extends Thread {

   private ServerSocket arrayServer;
 

   public static void main(String argv[]) throws Exception {
     new Server();
   }

   public Server() throws Exception {
     arrayServer = new ServerSocket(9999);
     System.out.println("Server listening on port 4000.");
     this.start();
   }

   public void run() {
     while(true) {
       try {
        System.out.println("Waiting for connections.");
        Socket client = arrayServer.accept();
        System.out.println("Accepted a connection from: "+
client.getInetAddress());
        Connect c = new Connect(client);
       } catch(Exception e) {}
     }
   }
}

class Connect extends Thread {
   private Socket client = null;
   private ObjectInputStream ois = null;
   private ObjectOutputStream oos = null;
   
   public Connect() {}

   public Connect(Socket clientSocket) {
     client = clientSocket;
     try {
      ois = new ObjectInputStream(client.getInputStream());
      oos = new ObjectOutputStream(client.getOutputStream());
     } catch(Exception e1) {
         try {
            client.close();
         }catch(Exception e) {
           System.out.println(e.getMessage());
         }
         return;
     }
     this.start();
   }

   public void run() {
     
      while (true) {
     
      Object obj = null;
       
      try {
         obj = ois.readObject();
         System.out.println((Player)obj);
         
         if (obj instanceof Player) {
            oos.writeObject((Player)obj);
            //oos.flush();
        }
         
      } catch (IOException e) {
         // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (ClassNotFoundException e) {
         // TODO Auto-generated catch block
        e.printStackTrace();
      }
       
       /*// close connections
         ois.close();
         oos.close();
         client.close(); */

      }    
   }
}


Client (with Slick2D):

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  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
import java.awt.Point;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.geom.Circle;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.particles.ConfigurableEmitter;
import org.newdawn.slick.particles.ParticleEmitter;
import org.newdawn.slick.particles.ParticleIO;
import org.newdawn.slick.particles.ParticleSystem;
import org.newdawn.slick.particles.effects.FireEmitter;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
 
public class GameplayState extends BasicGameState {
 
    int stateID = -1;
 
    GameplayState( int stateID )
    {
       this.stateID = stateID;
    }
 
    @Override
    public int getID() {
        return stateID;
    }
 
    //Attribute
   GameClient client = null;
   
    Player me;
    ArrayList<Player> players = new ArrayList<Player>();
    String debugStr = "";

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
 
       // Objekt client erzeugen
       client = new GameClient();
        //client.starteClient("127.0.0.1", 9999);
       
        me = new Player(0,100,100);
        me.nick = "ad";
       
    }
 
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
       
       for (Player p: players) {
          g.drawRect(p.x, p.y, p.width, p.height);
       }
       
       g.drawString(debugStr, 10, 20);
    }
   
   
   
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
       
       //Handle Keyinputs Clientsided
      Input input = gc.getInput();
       
       if (input.isKeyDown(input.KEY_W)) {
          me.moveUp();
       }
       
       if (input.isKeyDown(input.KEY_S)) {
          me.moveDown();
       }
       
       if (input.isKeyDown(input.KEY_A)) {
          me.moveLeft();
       }
       
       if (input.isKeyDown(input.KEY_D)) {
          me.moveRight();
       }
       
       debugStr = me.toString();
       
       client.sendTCP(me);
    }
   
 
   
 // innere Klasse GameClient
   public class GameClient {
      private String       host;
      private int          port;
      private ReaderThread reader;
      private Socket       socket;
      ObjectOutputStream oos = null;
      ObjectInputStream ois = null;
     
      public GameClient(){
         starteClient("127.0.0.1",9999);
      }

      public boolean starteClient(String host, int port){
          try {
            socket = new Socket(host, port);
           
            // connect war erfolgreich
           this.host= host;
            this.port= port;
           
            oos = new ObjectOutputStream(socket.getOutputStream());
            ois = new ObjectInputStream(socket.getInputStream());
           
            // Lese Thread starten
           reader = new ReaderThread();
            reader.start();
            return true;
          }
          catch (IOException e){
            System.err.println(e);
            return false;
          }
         
         
      }

      public void sendTCP(Object o){
         try {
              oos.writeObject(o);
              //oos.flush();
         } catch (IOException ex) {
              //Logger.getLogger(JChatView.class.getName()).log(Level.SEVERE, null, ex);
         }

  }
     

        // innere Klasse um parallel eingehende Nachrichten zu lesen
         class ReaderThread extends Thread {
           public void run(){
               
               while (true) {
                 
                    try {
                  Player obj = (Player)ois.readObject();
                 
                     boolean b = false;
                     int i = 0;
                     
                     for (Player p: players) {
                       
                       
                        if (p == obj) {
                           b = true;
                           players.set(i, obj);
                           //System.out.println(obj);
                          //System.out.println("Recieved Player "+i+"["+players.size()+"]");
                       }
                       
                        i++;
                     }
                     
                     if (!b) {
                        players.add(obj);
                        System.out.println("Recieved New Player");
                     }
                     
                 
                 
                  //System.out.println("Recieved Player: "+obj.toString());
                 
               } catch (IOException e) {
                  // TODO Auto-generated catch block
                 e.printStackTrace();
               } catch (ClassNotFoundException e) {
                  // TODO Auto-generated catch block
                 e.printStackTrace();
               }
                   
                   
                   
               }
               
               
            }
          } // Ende innere Klasse ClientReader


      public void beendeClient() {
         try {
           oos.close();
           ois.close();
           socket.close();
         }
         catch (IOException e){
           System.err.println(e);
         }
      }
 
    }// innere Klasse Client

}


And the Player Class:

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  
import java.awt.Rectangle;
import java.io.Serializable;

public class Player implements Serializable {

   
   int id;
   String nick;

   float x,y;
   double aimAngle;

   int width = 50, height = 50;

   Rectangle r;

   public Player(int id, float x, float y) {
      this.id = id;
      this.x = x;
      this.y = y;
   }

   
   
   public int getId() {
      return id;
   }



   public void setId(int id) {
      this.id = id;
   }



   public String getNick() {
      return nick;
   }



   public void setNick(String nick) {
      this.nick = nick;
   }



   public float getX() {
      return x;
   }



   public void setX(float x) {
      this.x = x;
   }



   public float getY() {
      return y;
   }



   public void setY(float y) {
      this.y = y;
   }



   public double getAimAngle() {
      return aimAngle;
   }



   public void setAimAngle(double aimAngle) {
      this.aimAngle = aimAngle;
   }



   public int getWidth() {
      return width;
   }



   public void setWidth(int width) {
      this.width = width;
   }



   public int getHeight() {
      return height;
   }



   public void setHeight(int height) {
      this.height = height;
   }


   public void moveUp() {
      y -= 1;
   }
   
   public void moveDown() {
      y += 1;
   }

   public void moveLeft() {
      x -= 1;
   }
   
   public void moveRight() {
      x += 1;
   }
   
   public Rectangle getRect() {
      return new Rectangle((int)x,(int)y,width,height);
   }

   public String toString() {
      return this.nick +";"+ this.x +";"+ this.y +";"+ this.aimAngle +";"+ this.width +";"+ this.height +";"+ this.id;

   }
}

21  Game Development / Newbie & Debugging Questions / Re: Gui problem on: 2011-04-11 17:25:00
Sorry I forgot to say that I fixed the problem.

It seems that you need to give the JFrame you open, the JFrame Object that is opening the JFrame, so it doesn't create another jFrame.
22  Game Development / Newbie & Debugging Questions / Re: Gui problem on: 2011-04-08 16:43:22
The problem is that the Dialog Object is not added to the arraylist.

23  Game Development / Newbie & Debugging Questions / Gui problem on: 2011-04-07 20:51:12
Okay, I'm trying to make a mission editor.

And I can't get this extremely easy task to work. I don't understand why.

A Mission, has tasks, tasks have dialogs.

Now I open the new tasks window and want to add a Dialog. Which I do by pressing a button.
The Frame opens and I choose whos speaking and the type in the dialog.

I then click finish.

This fires this method:

1  
2  
3  
4  
5  
6  
    @Action
    public void addDialog() {
        newAufgabe na = new newAufgabe();
        na.addDialog(txtText.getText(),cbWho.getSelectedItem()+"");
        this.setVisible(false);
    }


And then back in the tasks frame this method is fired.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
  public void addDialog(String Text, String Who) {

        dialogs.add(new Dialog(Who,Text));

        listdata = new String[dialogs.size()];

        for (int i=0; i<listdata.length; i++){
            listdata[i] = dialogs.get(i).getWho();
        }

        lblTest.setText(listdata[0]); // this also doesn't do anything. Eventough it should change the Label. The Label doesn't change
       listDialog.setListData(listdata);
     }


Now normally the jlist should be updated. But it seems like java completely ignores the fact that this method exists.
I debugged it and it went though the whole process, it seems to be working just fine. It adds the dialog to the arraylist. it goes into the for-loop. and does what it does.

But the frame does not update not single bit. I hate guis. Or maybe this is just some netbeans related stuff. I'm always having weird simple problems with java in guis.
24  Game Development / Newbie & Debugging Questions / Re: Detect just ctrl key pressed? on: 2011-04-02 23:40:18
Wouldn't it be KeyEvent.VK_CTRL ?
25  Game Development / Newbie & Debugging Questions / Re: shoot interval on: 2011-04-01 22:28:56
You actually were right with the second one.

Ive got so much code , that I totally didn't see that I was using the x,y pos of the player and not the center, to calculate the angle.

Thanks Cheesy

So so tired.
26  Game Development / Newbie & Debugging Questions / Re: Drawing images on: 2011-04-01 18:45:47
Well its technically impossible to draw with so-called double precission. Because you can't split pixels.

What you are experiencing ist that the "double precision" values are being rounded.
So if the rectangle x is : 0.5 and the x of the img is 0.

the rectangle will be placed at 1 and the img at 0.

Could you maybe post the code where you draw the rectangle and the image?
27  Game Development / Newbie & Debugging Questions / Re: shoot interval on: 2011-04-01 18:07:38
Can nobody help? Haven't fixed the problem yet. Sad
28  Game Development / Newbie & Debugging Questions / Re: shoot interval on: 2011-03-31 20:41:59
1  
2  
3  
4  
5  
6  
7  
Graphics2D gRV = (Graphics2D) bufferStrategy.getDrawGraphics();
        if (player.isvisible()) {
           gRV.rotate(Math.toRadians(player.getAngle()),player.getX()+player.getImage().getWidth(null)/2,player.getY()+player.getImage().getHeight(null)/2);  // Rotate the image by 1 radian.
          gRV.drawImage(player.getImage(),(int)player.getX(),(int)player.getY(), canvas);
           //gRV.drawOval((int)player.getCenterX()-5,(int)player.getCenterY()-5,10,10);
       }
       gRV.dispose();


1  
2  
3  
4  
5  
6  
7  
for (int i=0; i<mPlayer.size(); i++) {
           Graphics2D gRV = (Graphics2D) bufferStrategy.getDrawGraphics();
           Missile m = (Missile)mPlayer.get(i);
           gRV.rotate(Math.toRadians(m.getAngle()),m.getCenterX(),m.getCenterY());
           gRV.drawImage(m.getImage(),(int)m.getX(),(int)m.getY(),canvas);
           gRV.dispose();
        }
29  Game Development / Newbie & Debugging Questions / Re: shoot interval on: 2011-03-31 20:13:59
The center of the player seems to be slightly not the same all the time. Though this irritates me since the player is 40x40 px so there is no weird center when rotating.

But it seems that when you rotate the player via mouse. the center is not the same center as the players center.

Heres pic for reference:


as you can see the missile is displaced from the beginning. The oval represents the center of the player. (the center of the oval would be the exact center of the player.)
30  Game Development / Newbie & Debugging Questions / Re: shoot interval on: 2011-03-31 19:17:22
EDIT:

I actually noticed that the missile does not leave the player correctly sometimes. I will look into this.
Pages: [1] 2
 

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

The first screenshot will be displayed as a thumbnail.

pw (11 views)
2014-07-24 01:59:36

Riven (10 views)
2014-07-23 21:16:32

Riven (11 views)
2014-07-23 21:07:15

Riven (12 views)
2014-07-23 20:56:16

ctomni231 (42 views)
2014-07-18 06:55:21

Zero Volt (38 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (34 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!