Making polygons out of rectangles

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:

https://dl.dropboxusercontent.com/u/19432607/rectanglesIntoPolygons.jpg

I got the road tiles using this code:

	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:

//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”:

	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 :slight_smile:

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.

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;

https://dl.dropboxusercontent.com/u/19432607/rects.png