Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
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 2636 times)
0 Members and 1 Guest are viewing this topic.
Offline Slyth2727
« Posted 2013-07-14 02: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

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 136
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2013-07-14 05: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.
Online Rayexar
« Reply #2 - Posted 2013-08-10 08: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
Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 136
Projects: 4
Exp: 3 years



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

I doubt it, as long as the bodies are static.
Offline pploco1996
« Reply #4 - Posted 2013-08-12 19: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
« Reply #5 - Posted 2013-09-06 01:34:23 »

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

Online Rayexar
« Reply #6 - Posted 2013-10-11 03: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.

Longarmx (49 views)
2014-10-17 03:59:02

Norakomi (38 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (34 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (54 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!