我正在Libgdx开发一款游戏,我正在处理2D框中两个物体之间的碰撞检测。我使用“Contact Listener”来检测碰撞,但是当身体发生碰撞时没有任何反应。
这是我的代码
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); //
polygon.fixedRotation=true;
PolygonShape poly =new PolygonShape();
poly.setAsBox(width/2,height/2); //
//fixture defn
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);
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);
poly.dispose();
}
@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("polybody") &&
fixtureA.getUserData() !=null && fixtureA.getUserData().equals("polybodys"))
{
System.out.println("its colliding");
}
}
我添加了world.setContactListener(播放器); “玩家”也是上述类别的名称。
这是我检测碰撞的代码。当我运行代码并测试实体之间的碰撞时,没有任何反应。我在控制台中使用“println”语句进行了测试。我不知道这里哪个错了。请帮忙。在此先感谢!!
答案 0 :(得分:0)
我认为这个链接应该是你想做的事情的一种方式,也许这不是最好的方法,但它可以帮助另一方面我会尝试调整它来编码你的样品,但如果它起作用,因为不完整,并且有点让我感到困惑
您的代码包含更改:
public Player(World world,float x,float y,float width){
this.width = width;
height =width*2;
BodyDef polygon = new BodyDef();
polygon.type = BodyType.DynamicBody;
polygon.position.set(x,y); //<-- you use position one
polygon.fixedRotation = true;
PolygonShape poly = new PolygonShape();
poly.setAsBox(width/2,height/2);
//fixture defn
polygon.position.set(5,4); //<-- you use position two, twice,
//this overrides the previous consider this
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);
//add.setUserData("YourBody1Name");
// polybody.createFixture(polyfixture); <-- this original code
// change for example:
polybody.createFixture(polyfixture).setUserData("YourBody1");
polysprite = new Sprite(new Texture("img/car.jpg"));
polysprite.setSize(2, 3); //size of mario
polysprite.setOrigin(polysprite.getWidth()/2, polysprite.getHeight()/2);
第二个身体&lt; - 你在玩家类中创造了两个身体,这是你的意图吗?
BodyDef polygons = new BodyDef();
polygons.type = BodyType.DynamicBody;
//you do not use it for poligons position?
//for example:
//polygons.position.set(yourPosX,yourPosY);
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);
//add.setUserData("YourBody2Name");
// polybodys.createFixture(polyxfixture); <-- this original code
// change for example:
polybodys.createFixture(polyxfixture).setUserData("YourBody2");
poly.dispose();
//polys.dispose(); //<-- I do not see this dispose in your code.
}
现在在你创建世界的课堂上。复制此代码:
private class CrearContactListener implements ContactListener {
public CrearContactListener(){
}
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
// TODO Auto-generated method stub
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
// TODO Auto-generated method stub
}
@Override
public void endContact(Contact contact) {
// TODO Auto-generated method stub
}
@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("YourBody1")
&& fixtureB.getUserData() != null
&& fixtureB.getUserData().equals("YourBody2")){
Gdx.app.log("Contact","1");
}
if(fixtureB.getUserData() != null
&& fixtureB.getUserData().equals("YourBody1")
&& fixtureA.getUserData() != null
&& fixtureA.getUserData().equals("YourBody2")){
Gdx.app.log("Contact","2");
}
}
}
注意这是一个私有类,你必须把它放在类的合适位置,你不知道在哪里找到一些关于如何在其他类中使用私有类的信息。< / p>
并使用此代码分配ContactListener:
YourWorld.setContactListener(new CrearContactListener());
在身体2看看什么位置,可能是必要的一个。
在此示例中,删除其实现
你的班级玩家public void beginContact (Contact contact)
的一个原因是我不知道你的玩家类是否实现了ContactListener,或者只是把它放在那里。
我这段代码没有经过测试,但我觉得它可行,考虑到你提到的,另一方面,为我的英语道歉,我希望你理解我。