// PO


class PhysicsObject extends Sprite {
  // Properties
  // Components
  Body body;
  
  // This function adds the rectangle to the box2d world
  void makeBodyRect(Vec2 center, float w_, float h_) {
    // Define a polygon (this is what we use for a rectangle)
    PolygonDef sd = new PolygonDef();
    float box2dW = box2d.scalarPixelsToWorld(w_/2)/WS;
    float box2dH = box2d.scalarPixelsToWorld(h_/2)/WS;
    sd.setAsBox(box2dW, box2dH);
    // Parameters that affect physics
    sd.density = 1.0f;
    sd.friction = 0.0f; //friction will be done manually in my code
    sd.restitution = 0.0f;
    //Filter! (No interacting with aesthetic particles!)
    sd.filter.categoryBits = 0x0002;
    sd.filter.maskBits = 0x0002;
    // Define the body and make it from the shape
    BodyDef bd = new BodyDef();
    bd.userData = this;
    bd.position.set(box2d.coordPixelsToWorld(new Vec2(center.x, center.y)));
    body = box2d.createBody(bd);
    body.createShape(sd);
    body.setMassFromShapes();
    body.setLinearDamping(0.0f);
    // No initial velocity
    body.setLinearVelocity(new Vec2(0, 0));
    body.setAngularVelocity(0);
    //Stay awake!
    body.allowSleeping(false);
  }
  
  
  void goTo(float X,float Y) {
    x = X;
    y = Y;
    body.setXForm(box2d.coordPixelsToWorld(new Vec2(x/WS, y/WS)), angle);
  }

  // This function removes the body from the box2d world
  void killBody() {
    try {
      if (body!=null) box2d.destroyBody(body);
    }
    catch(Exception e) {
      println("Unable to destroy a body. =P");
    }
  }
}
