如何在Libgdx中创建一个多边形和矩形连接在一起的主体

时间:2015-03-03 15:41:11

标签: java libgdx polygon

我是Libgdx的新手。我正在研究检测两具尸体的碰撞。使用“polygonshape”创建了两个实体但不幸的是没有任何方法可以检测“PolygonShapes”之间的碰撞,而是“矩形”。所以我想出了将矩形附加在每个多边形的前面然后然后检测矩形的碰撞但是当我运行代码时,我看不到任何矩形而只看到多边形。

这是我的代码,

public class Player implements Screen, InputProcessor {

private Body polybody;
private Player player;
private World world;
Array<Rectangle> raindrops;

private Body enemybody;
private Sprite polysprite;
public final float width,height;
private Vector2 movement=new Vector2();
    private float speed=580;


    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); //
    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);

  //disposing the body
    BodyDef rectangle=new BodyDef();
    rectangle.type=BodyType.DynamicBody;
    Rectangle rect=new Rectangle();

    rect.setWidth(50);
    rect.setHeight(50);
    rect.setPosition(x, y);
    enemybody=world.createBody(rectangle);

    polysprite=new Sprite(new Texture("img/car.jpg"));
    polysprite.setSize(0.5f, 1); 
    polysprite.setOrigin(polysprite.getWidth()/2, polysprite.getHeight()/2);
    polybody.setUserData(polysprite);
    poly.dispose();   

   }

   public void update()
   {
      polybody.applyForceToCenter(movement, true);
      enemybody.applyForceToCenter(movement,true);

   }


  public Body getBody(){
   {
  return polybody;
   }
  }
}

碰撞代码:

 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;
 //polygon shape
 //Rectangle poly=new Rectangle();
 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);
 // polybody.applyAngularImpulse(52, true);
 //disposing the body

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();
System.out.println("collides");
 }

这是我的代码。我使用“polygonshape”和“rectangle”创建了2个实体,但未创建矩形。我找不到什么错误。请帮忙!!提前致谢

1 个答案:

答案 0 :(得分:2)

看起来你有点困惑,你混合了两个绝对独立的东西:

  • Box2D,一个物理引擎,可以进行所有物理计算 (包括碰撞检测)。
  • libgdx的RectanglePolygonIntersector类。

我试着解释它们的作用以及为什么不能一起使用它们 PolygonshapeBox2D引擎的一部分。它用于给出Fixture它的形状,然后由Box2D的碰撞检测使用。
您可以阅读有关Box2D here的内容 Rectangle类是Libgdx中的某种“Helperclass”,它允许您定义具有给定位置,宽度和高度的Rectangle。它可以与Intersector类一起使用,它为碰撞检测和其他事情提供了一些有用的方法。
还有一个类Polygon。它同样是一个Libgdx类,不能与Box2D PolygonShape混合使用。 Libgdx Polygon可以与Intersector类一起使用,它提供了一种检查2 Polygon之间重叠的方法。 Polygonfloat[]定义,给出了所有角点的位置 这意味着,如果您想使用物理引擎Box2D,则需要使用由PolygonShape提供的Box2D和其他形状。
如果您不想使用物理引擎并自行进行碰撞检测,则可以使用RectanglePolygonLibgdx中的其他形状,您也可以使用Intersector用于重叠测试。

修改
如果您查看this link,则会有“联系人监听器”部分。在那里你可以看到,ContactListener提供了方法beginContact(当两个对象开始碰撞/重叠时调用)和endContact(当两个对象之间的联系结束时调用)。登记/> 您需要实现这些方法并将其设置为world s ContactListener 节点,Box2D通过分离碰撞对象(反弹)自动处理碰撞。如果那不是您想要的,那么您应该设置isSensor - FixtureDef的标记。这样,Box2D会通知你,发生碰撞(beginContact被调用),但不会为你处理。

编辑2
您在Contact - 方法中获得的ContactListener对象包含2个Fixture s:FixtrueAFixtureB
那是因为在每次接触或碰撞时都必须涉及2个物体,因此FixtureA就是其中之一而FixtureB就是其中之一。
为什么Contact包含2 Fixture而不是2 Body s的原因如下:
假设我们有Player,由Body定义。这个Body包含2个Fixture s:headFixture,一个代表它头部的圆圈和bodyFixture,一个矩形,代表Player身体和脚的命中框。
现在,如果BulletBody只有一个Fixture)点击我们的Player,如果它撞到它的身体或头部,它可能会有所不同。
基本上,Body代表我们的整个对象Player,而Fixture代表它的某些部分,用于物理计算。
如果您只想按Body处理合并,则可以通过调用Body获取fixtureA().getBody()