【问题标题】:Contact Listener has no effect in LibgdxContact Listener 在 Libgdx 中没有作用
【发布时间】:2015-04-01 13:26:47
【问题描述】:

我正在研究物体之间的 Libgdx 碰撞检测,我尝试使用联系侦听器来实现它,但它在我的代码中没有效果。

这是我的代码

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

“Player”是一个实现contactListener和InputProcessor的类。

这是播放器类

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”是两个实体。我想检测两个物体之间的碰撞。当身体碰撞时,我无法检测到任何碰撞。请帮忙。提前致谢。

【问题讨论】:

    标签: java android libgdx collision-detection


    【解决方案1】:

    您将polybodyuserData 设置为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))
    

    应该是这样的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      相关资源
      最近更新 更多