java - Contact Listener has no effect in Libgdx -


i working on libgdx collision detection between bodies , tried implementing using contact listener has no effect in code.

here code

public class firstlevel implements screen{ ...... private player player; ...... ... gdx.input.setinputprocessor(player); world.setcontactlistener(player); .... } 

"player" class implements contactlistener , inputprocessor.

here player class

public class player implements screen, inputprocessor,contactlistener {

private body polybody,polybodys; private player player; private world world; boolean colliding ; private body enemybody; private sprite polysprite; public final float width,height; private rectangle rectangle; private vector2 movement=new vector2();  private float speed=580;  private body rec;      public player(world world,float x,float y,float width)    {     this.width=width; //imp     height=width*2;     bodydef polygon=new bodydef();     polygon.type=bodytype.dynamicbody;     polygon.position.set(x,y); //     polygonshape poly =new polygonshape();     poly.setasbox(width/2,height/2); //     polygon.position.set(5,4);     fixturedef polyfixture=new fixturedef();     polyfixture.shape=poly;     polyfixture.friction=0.8f;       polyfixture.restitution=0.1f;      polyfixture.density=3;        //creating actual body      polybody=world.createbody(polygon);      polybody.createfixture(polyfixture).setuserdata(polygon);      polysprite=new sprite(new texture("img/car.jpg"));      polysprite.setsize(2, 3); //size of mario      polysprite.setorigin(polysprite.getwidth()/2, polysprite.getheight()/2);      polybody.setuserdata(polysprite);      bodydef polygons=new bodydef();      polygons.type=bodytype.dynamicbody;      polygonshape polys=new polygonshape();      polys.setasbox(2,2);      fixturedef polyxfixture=new fixturedef();      polyxfixture.shape=polys;      polyxfixture.friction=0.8f;      polyxfixture.restitution=0.1f;      polyxfixture.density=3;      polybodys=world.createbody(polygons);      polybodys.createfixture(polyxfixture).setuserdata(polygons);      poly.dispose();          }      public void update()       {      polybody.applyforcetocenter(movement, true);      polybodys.applyforcetocenter(movement,true);       }      public body getbody(){      {    return polybody;      }    }       @override      public boolean keydown(int keycode) { // todo auto-generated method stub      switch (keycode) {      case keys.w:        movement.y=speed;      break;      case keys.s:        movement.y=-speed;      break;      case keys.d:        movement.x=speed;      break;      case keys.a:        movement.x=-speed;     }    return true;    }      @override     public boolean keyup(int keycode) { // todo auto-generated method stub     switch (keycode) {     case keys.w:       movement.y=0;     break;     case keys.s:       movement.y=0;     break;     case keys.d:       movement.x=0;     break;  case keys.a:      movement.x=0; }     return true;      }    @override    public boolean touchdown(int screenx, int screeny, int pointer, int   button) { // todo auto-generated method stub  movement.x =speed;  gdx.app.log("example", "touch done ");      return true;      }     @override    public void begincontact(contact contact) { // todo auto-generated method stub  fixture fixturea=contact.getfixturea(); fixture fixtureb=contact.getfixtureb(); if(fixturea.getuserdata() != null && fixturea.getuserdata().equals(polybody) &&         fixtureb.getuserdata() !=null && fixtureb.getuserdata().equals(polybodys)){     gdx.app.log("contact","1");     system.out.println("its colliding");  }  if(fixtureb.getuserdata() !=null && fixtureb.getuserdata().equals(polybodys) &&         fixturea.getuserdata() !=null && fixturea.getuserdata().equals(polybody)) {     system.out.println("its colliding"); }     }    } 

this code , "polybody , polybodys" 2 bodies. want detect collision between 2 bodies. not able detect collision when body collides. please help. in advance.

you setting userdata of polybody polysprite:

polybody.setuserdata(polysprite); 

but, in collision detection code, checking different type of userdata:

if(fixtureb.getuserdata() !=null && fixtureb.getuserdata().equals(polybodys) &&         fixturea.getuserdata() !=null && fixturea.getuserdata().equals(polybody)) 

so condition false. seems condition:

fixturea.getuserdata() !=null && fixturea.getuserdata().equals(polybody)) 

should this:

fixturea.getuserdata() !=null && fixturea.getuserdata().equals(polysprite)) 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -