Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Making polygons out of rectangles  (Read 1021 times) 0 Members and 1 Guest are viewing this topic.
kutucuk

Senior Devvie

Medals: 5
Exp: 3 years

 « 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.

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.
kutucuk

Senior Devvie

Medals: 5
Exp: 3 years

 « 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

 nelsongames (13 views) 2018-04-24 18:15:36 nelsongames (11 views) 2018-04-24 18:14:32 ivj94 (586 views) 2018-03-24 14:47:39 ivj94 (49 views) 2018-03-24 14:46:31 ivj94 (383 views) 2018-03-24 14:43:53 Solater (63 views) 2018-03-17 05:04:08 nelsongames (110 views) 2018-03-05 17:56:34 Gornova (159 views) 2018-03-02 22:15:33 buddyBro (707 views) 2018-02-28 16:59:18 buddyBro (93 views) 2018-02-28 16:45:17
 KaiHH 13x ByerN 11x SHC 10x NuclearPixels 10x Zemlaynin 10x Guerra2442 6x Damocles 6x VaTTeRGeR 5x ags1 4x Spasi 4x orangepascal 4x philfrei 4x mesterh 3x princec 3x ndnwarrior15 3x Springrbua 2x
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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