Contact Listener在Libgdx中没有任何效果

时间:2015-04-01 13:26:48

标签: java android libgdx collision-detection

我正在处理机体之间的Libgdx碰撞检测,我尝试使用Contact Listener实现它,但它对我的代码没有影响。

这是我的代码

public class FirstLevel implements Screen{
......
private Player player;
......
...
Gdx.input.setInputProcessor(player);
world.setContactListener(player);
....
}

"播放"是一个实现contactListener和InputProcessor的类。

这是Player类

public class Player实现了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");
}

   }
   }

这是我的代码" polybody和polybodys"是两个身体。我想检测两个身体之间的碰撞。当身体碰撞时,我无法发现任何碰撞。请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您要将userData的{​​{1}}设为polybody

polysprite

但是,在碰撞检测代码中,您正在检查其他类型的polybody.setUserData(polysprite);

userData

因此条件始终为if(fixtureB.getUserData() !=null && fixtureB.getUserData().equals(polybodys) && fixtureA.getUserData() !=null && fixtureA.getUserData().equals(polybody)) 。这似乎是这个条件:

false

应该是这样的:

fixtureA.getUserData() !=null && fixtureA.getUserData().equals(polybody))