Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
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  
  Making polygons out of rectangles  (Read 373 times)
0 Members and 1 Guest are viewing this topic.
Offline kutucuk
« Posted 2013-06-24 17:39:29 »

Hi,
I am trying to go forward with my skills and I decided instead of trying stuff out just to try, I should make a game to learn the game concepts. No matter how primitive or buggy it turns out to be, it'd be a good experience.

I'm using Libgdx and Box2d.

So, I decided to make a simple tower defense game. For this, I needed a map with road. So far, I built the map using Tiled, here it is:

I got the road tiles using this code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   public void getTileId() {
      TiledMapTileLayer tileLayer = (TiledMapTileLayer) map.getLayers().get("road_layer");
      for (int x = 0; x < tileLayer.getWidth(); x++) {
         for (int y = 0; y < tileLayer.getHeight(); y++) {
            TiledMapTileLayer.Cell myCell = tileLayer.getCell(x, y);
            if (myCell.getTile().getProperties().get("tid") != null && myCell.getTile().getProperties().get("tid").equals("road")) {
               cells.add(myCell); //myCell is the Array of cells with a property I set, tid is road.
               cellLocations.add(new Vector2(x, y)); // This is the Array of the cell locations.
            }
         }
      }
   }


Then I drew rectangles around the "road" cells (tiles) using this method:
1  
2  
3  
4  
5  
6  
7  
8  
//tile_height and tile_width are from my map class. But we could also assume that they are both 32.
   public void getRoad() {
      for (int i = 0; i < tiles.size; i++) {
         System.out.println(tiles.get(i).getTile().getId() + " " + tileLocations.get(i));
         Rectangle rec = new Rectangle(tileLocations.get(i).x * tile_width, tileLocations.get(i).y * tile_height, tile_width, tile_height);
         roadRectangles.add(rec);
      }
   }


Then I tried to build two polygons so that I could use these polygons as the lines that limit the road. Maybe I could create static bodies in the same position as the polygons to use Box2D. As you can see from the screenshot, I use box2d (They had sprites, but removed them to explain my problem).

I tried this but failed, so funny "rectangles":
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
   public void getPolygon() {
      for (int i = 0; i < roadRectangles.size - 1; i++) {
         for (int j = 0; j < roadRectangles.size - 1; j++) {
            if (roadRectangles.get(i).x + 32 == roadRectangles.get(j).x && roadRectangles.get(i).y == roadRectangles.get(j).y) {
               System.out.println("touching x: " + roadRectangles.get(i).x + " " + roadRectangles.get(j).x);
               System.out.println("touching y: " + roadRectangles.get(i).y + " " + roadRectangles.get(j).y);
               roadRectangles.get(i).merge(roadRectangles.get(j));
            }
         }
      }
   }

That has some big problems: 1- How the road tiles are put into the Array. 2- The roads in the same x or y coordinates but not close are tried to be merged.

What can I do from here?

My original intention was to set the limits of the road to keep the enemies in the road, then I thought about this way. But I can work with anything that will make this possible.

Thanks in advance Smiley

EDIT: I know the road is not passable as is, but that's an easy fix. And if someone wants to see the whole code of my map class, road finding class and any other class, of course I can share it with you guys.
Offline kutucuk
« Reply #1 - Posted 2013-06-26 12:32:09 »

That took me a while but I managed to do it. I haven't tested it for different cases, and there are some things to be aware of, but I made it.
I'll explain what I did here and I am expecting some comments about how I could do it better and faster.

For now, all I have is a polyline and I still have to figure out using it as the route of the enemies. So a little help on this would be appreciated.

First problem is, in the x = 0, there should be only one road tile.
Second is, I don't know how it will respond if I have crossing roads.

I commented the class and explained variables etc. Here: http://www.java-gaming.org/?action=pastebin&id=624

Here is the result;


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.

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

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

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

lcass (34 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (54 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
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!