Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (855)
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  
  ContactListener doesn't work [Jbox2D]  (Read 1315 times)
0 Members and 1 Guest are viewing this topic.
Offline Jotask

Junior Newbie





« Posted 2015-07-18 21:52:17 »

Hi. First, sorry if I post this on the wrong place.
I am developing a platform game with slick and JBox2d, but I can't get the contactlistener (for collision) work. I create a class that implement ContactListener. When the world class is initialized, call the super class and create a map with tiledmap, and look for all the tiles on the map, for create static bodies for collision, but doesn't work and I can't see the problem. Any idea? Thanks

Game.class
1  
2  
      World world = new World(new Vec2(0, Util.GRAVITY));
      world.setContactListener(new Listener(this));

World.class
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  
public class World extends org.jbox2d.dynamics.World {

   public World(Vec2 gravity) {
      this(gravity, 0);
   }

   public World(Vec2 gravity, int lvl) {
      super(gravity);
      try {
         map = new TiledMap(Util._RES + path + maps[lvl]);
      } catch (SlickException e) {
         e.printStackTrace();
      }
      generateMap();
   }

   // Generate box2d bodies for all tiles
   private void generateMap() {
      int layerIndex = map.getLayerIndex("base");
      for (int tileX = 0; tileX < map.getWidth(); tileX++) {
         for (int tileY = 0; tileY < map.getHeight(); tileY++) {
            int tileID = map.getTileId(tileX, tileY, layerIndex);
            String type = map.getTileProperty(tileID, "type", null);
            if (type != null && type.equals("grass")) {
               // Create box2d bodie for this tile
               Vector2 position = tileToWorld(tileX, tileY);
               position.add(map.getTileWidth() / 2,
                     map.getTileHeight() / 2);

               Vec2 physicsPosition = toPhysicsVector(position);
               createBoxBody(physicsPosition, map.getTileWidth(),
                     map.getTileHeight(), BodyType.STATIC);
            }
         }
      }
   }

   private Body createBoxBody(Vec2 position, float width, float height,
         BodyType type) {
      BodyDef bodyDef = new BodyDef();
      bodyDef.position = position;
      bodyDef.type = type;
      bodyDef.linearDamping = 0.5f;

      FixtureDef fixture = new FixtureDef();
      PolygonShape shape = new PolygonShape();
      shape.setAsBox(width * 0.5f / PTM, height * 0.5f / PTM);
      fixture.shape = shape;

      Body body = createBody(bodyDef);
      body.createFixture(fixture);
      return body;
   }

   private Vec2 toPhysicsVector(Vector2 vector) {
      return new Vec2(vector.getX() / PTM, vector.getY() / PTM);
   }

   public Vector2 tileToWorld(int tileX, int tileY) {
      return new Vector2(MAP_X + map.getTileWidth() * tileX, MAP_Y
            + map.getTileHeight() * tileY);
   }

   public Vector2 tiledMapToWorld(float x, float y) {
      return new Vector2(MAP_X + x, MAP_Y + y);
   }

}


and Listener.class

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
public class Listener implements ContactListener {
   
   private Game game;

   public Listener(Game g) {
      this.game = g;
   }

   @Override
   public void beginContact(Contact contact) {
      System.out.println("Collision!!");
   }

   @Override
   public void endContact(Contact contact) { }

   @Override
   public void postSolve(Contact contact, ContactImpulse ci) { }

   @Override
   public void preSolve(Contact contact, Manifold mani) { }

}


PS: I removed some code for make more redeable the post
Offline Jotask

Junior Newbie





« Reply #1 - Posted 2015-07-19 18:18:58 »

All the code work's is just a type mistake on the .tmx map file, on the type. This things happens when you try to focus all the time for saw the error, and when you have a big break you see the stupid error.
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1572 views)
2018-06-10 19:43:48

EgonOlsen (1632 views)
2018-06-10 19:43:44

EgonOlsen (1144 views)
2018-06-10 19:43:20

DesertCoockie (1572 views)
2018-05-13 18:23:11

nelsongames (1173 views)
2018-04-24 18:15:36

nelsongames (1639 views)
2018-04-24 18:14:32

ivj94 (2395 views)
2018-03-24 14:47:39

ivj94 (1605 views)
2018-03-24 14:46:31

ivj94 (2691 views)
2018-03-24 14:43:53

Solater (883 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!