Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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  
  Box2D Map Collision  (Read 2004 times)
0 Members and 1 Guest are viewing this topic.
Offline Slyth2727
« Posted 2013-07-14 04:59:17 »

I would like to have a way to make maps in a map editor (tiled) and be able to add box2d collision detection to it. I have already seen Aurelion Ribon's program, however that doesn't seem to work with large map images. Is there a simple way to do this? If not, is there anyway you would suggest it? While looking around, I found that you can export a an xml file, so I was thinking that maybe in runtime I make my program look at the tiles and if the id equals one that is collidable add a box2d body around it? This is a topdown game as well. Anything helps! Thanks, -cMp

Check out Escaping Vox, a 3D
|\ _\  voxel based puzzler!
 \|_|
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 117
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2013-07-14 07:05:25 »

Define a box2d body for each map tile.
http://dpk.net/2011/05/08/libgdx-box2d-tiled-maps-full-working-example-part-23/#p1
>Uses the outdated map api but everything else is the same.
Offline Rayexar

Junior Member


Medals: 2



« Reply #2 - Posted 2013-08-10 10:09:56 »

That's what I was thinking, but would it cause performance or memory issues due to the large number of tiles on the map?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 117
Projects: 4
Exp: 3 years



« Reply #3 - Posted 2013-08-12 19:22:20 »

I doubt it, as long as the bodies are static.
Offline pploco1996

Senior Newbie


Exp: 1 year



« Reply #4 - Posted 2013-08-12 21:12:47 »

I manage to do that yesterday with this:

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  
BodyDef def = new BodyDef();
      PolygonShape shape = new PolygonShape();
      int current_body = 0;

      for (MapObject object : mapa.getLayers().get(1).getObjects()) {
         if (object instanceof RectangleMapObject) {
            Rectangle rect = ((RectangleMapObject) object).getRectangle();

            shape.setAsBox((rect.width / 2) / 10, (rect.height / 2) / 10);

            def.position.x = (rect.x / 10) + ((rect.width / 10) / 2);
            def.position.y = (rect.y / 10) + ((rect.height / 10) / 2);

            cuerpos.add(world.createBody(def));
            cuerpos.get(current_body).createFixture(shape, 1);
         }

         else if (object instanceof PolygonMapObject) {
            Polygon poly = ((PolygonMapObject) object).getPolygon();

            float vertices[] = poly.getTransformedVertices();

            for (int x = 0; x < vertices.length; x++) {
               vertices[x] /= 10;
            }
            System.out.println(vertices[0]);
            shape.set(vertices);
            def.position.x = poly.getOriginX() / 10;
            def.position.y = poly.getOriginY() / 10;

            cuerpos.add(world.createBody(def));
            cuerpos.get(current_body).createFixture(shape, 1);
         }

         current_body++;
      }


This code works only for rectangles and polygons but if think a bit you will be able to make it ellipse and polyline friendly
Offline dermetfan

Senior Member


Medals: 11



« Reply #5 - Posted 2013-09-06 03:34:23 »

Probably a bit too late but whatever: this (this)

Offline Rayexar

Junior Member


Medals: 2



« Reply #6 - Posted 2013-10-11 05:36:27 »

It's not too late Smiley, thank you very much for that, you've been really helpful!
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.

xsi3rr4x (64 views)
2014-04-15 18:08:23

BurntPizza (62 views)
2014-04-15 03:46:01

UprightPath (75 views)
2014-04-14 17:39:50

UprightPath (58 views)
2014-04-14 17:35:47

Porlus (76 views)
2014-04-14 15:48:38

tom_mai78101 (101 views)
2014-04-10 04:04:31

BurntPizza (161 views)
2014-04-08 23:06:04

tom_mai78101 (256 views)
2014-04-05 13:34:39

trollwarrior1 (209 views)
2014-04-04 12:06:45

CJLetsGame (216 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!