title: How to create a body with Polygon and Rectangle attached together in Libgdx
body: <p>I am a newbie in Libgdx. I am working on detecting collisions for two bodies. Have created two bodies using "polygonshape" but unfortunately there isn't any method to detect collision among "PolygonShapes" but for "rectangles" .So I came up with an idea of attaching the rectangle at the front of each of the polygons and then detect collision for rectangles but when I run the code, I don't see any rectangles but only polygons.</p>
  
  <p>Here is my code,</p>
  
  <pre><code>public class Player implements Screen, InputProcessor {
  
  private Body polybody;
  private Player player;
  private World world;
  Array&lt;Rectangle&gt; 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;
     }
    }
  }
  </code></pre>
  
  <p>Collision Code:</p>
  
  <pre><code> 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);
  </code></pre>
  
  <p>//2nd body</p>
  
  <pre><code>   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");
   }
  </code></pre>
  
  <p>This is my code. I have created 2 bodies using "polygonshape" and "rectangle" but rectangle isn't created. I can't find what is the mistake. Please help!! Thanks in advance</p>