Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (581) Games in Android Showcase (163) games submitted by our members Games in WIP (632) 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 477 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

You cannot reply to this message, because it is very, very old.

 CopyableCougar4 (14 views) 2015-05-27 19:24:50 MrMapcom (24 views) 2015-05-23 20:26:16 MrMapcom (32 views) 2015-05-23 20:23:34 Waterwolf (37 views) 2015-05-20 15:01:45 chrislo27 (44 views) 2015-05-20 03:42:21 BurntPizza (79 views) 2015-05-10 15:53:18 FrozenShade (63 views) 2015-05-07 09:11:21 TheLopais (226 views) 2015-05-06 13:36:48 TheLopais (207 views) 2015-05-06 13:35:14 TheLopais (213 views) 2015-05-06 13:33:39
 Spasi 31x Rayvolution 21x Riven 16x theagentd 15x Drenius 15x BurntPizza 15x ra4king 13x opiop65 12x EgonOlsen 11x princec 11x DavidBVal 11x Husk 11x KevinWorkman 10x scanevaro 8x orangepascal 8x SauronWatchesYou 8x
 List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00
 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