Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Updated Box2d Tiled Map Per-Tile Collision  (Read 405 times)
0 Members and 1 Guest are viewing this topic.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 133
Projects: 4
Exp: 3 years



« Posted 2014-05-19 02:28:55 »

libGDX - assigning box2d bodies to tiles in a Tiled map, written by "dpk". You can find the editor used to create the file that this code parses here.

Anyways, dpk's code hasn't been updated since 2011, I've been meaning to update his parser class but didn't bother to until now, so here is an updated version of his code (new maps api and removing deprecated LineSegment).

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  
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.HashMap;

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.ChainShape;
import com.badlogic.gdx.physics.box2d.EdgeShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Array;

public class Box2DMapParser {

   public Box2DMapParser(FileHandle file, World world, TiledMap map, float pixm) {
      String fileS = file.readString();
      String[] lines = null;

      lines = fileS.split("\\r?\\n");

      TiledMapTileLayer tmtl = (TiledMapTileLayer) map.getLayers().get(0);
     
      HashMap<Integer, ArrayList<Line2D>> tileCollisionJoints = new HashMap<Integer, ArrayList<Line2D>>();

      for (int n = 0; n < lines.length; n++) {
         String cols[] = lines[n].split(" ");
         int tileNo = Integer.parseInt(cols[0]);

         ArrayList<Line2D> tmp = new ArrayList<Line2D>();

         for (int m = 1; m < cols.length; m++) {
            String coords[] = cols[m].split(",");

            String start[] = coords[0].split("x");
            for(int d = 0; d < start.length; d++){
            }
            String end[] = coords[1].split("x");
            for(int e = 0; e < end.length; e++){
            }
            tmp.add(new Line2D.Float(Integer.parseInt(start[0]), 64f - Integer.parseInt(start[1]),
                  Integer.parseInt(end[0]), 64f - Integer.parseInt(end[1])));
         }

         tileCollisionJoints.put(Integer.valueOf(tileNo), tmp);

      }
      Array<Line2D> lines2d = new Array<Line2D>();
      for (int i = 0; i < map.getProperties().get("width", Integer.class); i++) {
         for (int j = 0; j < map.getProperties().get("height", Integer.class); j++) {
            Cell cell = tmtl.getCell(i, j);
            try {
               int tileType = cell.getTile().getId();
               

               for (int k = 0; k < tileCollisionJoints.get(tileType).size(); k++) {
                  Line2D line = new Line2D.Float(i * 64f
                        + (float) tileCollisionJoints.get(tileType).get(k).getX1(), j * 64f
                        + (float) tileCollisionJoints.get(tileType).get(k).getY1(), i * 64f
                        + (float) tileCollisionJoints.get(tileType).get(k).getX2(), j * 64f
                        + (float) tileCollisionJoints.get(tileType).get(k).getY2());
                  lines2d.add(line);

                 
               }

               

            } catch (NullPointerException npe) {

            }

         }

      }

     
      for (int i = 0; i < lines2d.size; i++) {
         EdgeShape shape = new EdgeShape();
         lines2d.get(i).setLine((float) lines2d.get(i).getX1() / pixm, (float) lines2d.get(i).getY1() / pixm,
               (float) lines2d.get(i).getX2() / pixm, (float) lines2d.get(i).getY2() / pixm);
         shape.set((float) lines2d.get(i).getX1(), (float) lines2d.get(i).getY1(),
               (float) lines2d.get(i).getX2(), (float) lines2d.get(i).getY2());

     
      BodyDef edgeDef = new BodyDef();
      edgeDef.type = BodyType.StaticBody;
      Body edge = world.createBody(edgeDef);
      FixtureDef edgeFixtureDef = new FixtureDef();
      edgeFixtureDef.shape = shape;
      edge.createFixture(edgeFixtureDef);
      shape.dispose();
      }
   }
}

Just replace 64 with your tile width/height...too lazy to add variables Grin
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

Pippogeek (37 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (61 views)
2014-09-21 02:42:18

BurntPizza (30 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (52 views)
2014-09-19 03:14:18
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!