Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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 1139 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  
 
 

 
Ecumene (108 views)
2017-09-30 02:57:34

theagentd (135 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (240 views)
2017-08-02 12:19:43

cybrmynd (238 views)
2017-08-02 12:18:09

Sralse (252 views)
2017-07-25 17:13:48

Archive (864 views)
2017-04-27 17:45:51

buddyBro (1007 views)
2017-04-05 03:38:00

CopyableCougar4 (1566 views)
2017-03-24 15:39:42

theagentd (1373 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!