private void checkBounds() {
    for (int i = 0; i < liquid.length; ++i) {
      if (liquid[i].getMemberWorldCenter().y < -10.0f) {
        m_world.destroyBody(liquid[i]);
        float massPerParticle = totalMass / nParticles;
        CircleDef pd = new CircleDef();
        pd.density = 1.0f;
        pd.filter.groupIndex = -10;
        pd.radius = .05f;
        pd.restitution = 0.4f;
        pd.friction = 0.0f;
        float cx = 0.0f + parent.random(-0.6f, 0.6f);
        float cy = 15.0f + parent.random(-2.3f, 2.0f);
        BodyDef bd = new BodyDef();
        bd.position = new Vec2(cx, cy);
        bd.fixedRotation = true;
        Body b = m_world.createBody(bd);
        b.createShape(pd).setUserData(LIQUID_INT);
        MassData md = new MassData();
        md.mass = massPerParticle;
        md.I = 1.0f;
        b.setMass(md);
        b.allowSleeping(false);
        liquid[i] = b;
      }
    }

    if (bod.getMemberWorldCenter().y < -15.0f) {
      m_world.destroyBody(bod);
      PolygonDef polyDef = new PolygonDef();
      polyDef.setAsBox(parent.random(0.3f, 0.7f), parent.random(0.3f, 0.7f));
      polyDef.density = 1.0f;
      BodyDef bodyDef = new BodyDef();
      bodyDef.position = new Vec2(0.0f, 25.0f);
      bod = m_world.createBody(bodyDef);
      bod.createShape(polyDef);
      bod.setMassFromShapes();
    }
  }
  @Override
  public void create() {

    if (firstTime) {
      setCamera(0.0f, 10.0f, 20.0f);
      firstTime = false;
      // this.settings.drawJoints = false;
    }

    Body ground = null;
    {
      PolygonDef sd = new PolygonDef();
      sd.setAsBox(50.0f, 0.4f);

      BodyDef bd = new BodyDef();
      bd.position.set(0.0f, 0.0f);
      ground = m_world.createBody(bd);
      ground.createShape(sd);

      sd.setAsBox(0.4f, 50.0f, new Vec2(-10.0f, 0.0f), 0.0f);
      ground.createShape(sd);
      sd.setAsBox(0.4f, 50.0f, new Vec2(10.0f, 0.0f), 0.0f);
      ground.createShape(sd);
    }

    ConstantVolumeJointDef cvjd = new ConstantVolumeJointDef();

    float cx = 0.0f;
    float cy = 10.0f;
    float rx = 4.0f;
    float ry = 4.0f;
    int nBodies = 100;
    float bodyRadius = 0.4f;
    ArrayList<Body> bodies = new ArrayList<Body>();
    for (int i = 0; i < nBodies; ++i) {
      float angle = PApplet.map(i, 0, nBodies, 0, 2 * 3.1415f);
      BodyDef bd = new BodyDef();
      // bd.isBullet = true;
      bd.fixedRotation = true;

      float x =
          cx
              + (1.0f + .5f * (float) Math.cos(4 * (angle + .25f * 3.1415f)))
                  * rx
                  * (float) Math.sin(angle);
      float y =
          cy
              + (1.0f + .5f * (float) Math.cos(4 * (angle + .25f * 3.1415f)))
                  * ry
                  * (float) Math.cos(angle);
      bd.position.set(new Vec2(x, y));
      Body body = m_world.createBody(bd);
      CircleDef cd = new CircleDef();
      cd.radius = bodyRadius;
      cd.density = 1.0f;
      // cd.filter.groupIndex = -2;
      body.createShape(cd);
      cvjd.addBody(body);
      body.setMassFromShapes();
      bodies.add(body);
    }

    cvjd.frequencyHz = 10.0f;
    cvjd.dampingRatio = 10.0f;
    ((ConstantVolumeJoint) m_world.createJoint(cvjd)).inflate(1.5f);

    cvjd = new ConstantVolumeJointDef();
    for (int i = 0; i < nBodies / 4; ++i) {
      cvjd.addBody(bodies.get(i));
    }
    cvjd.frequencyHz = 10.0f;
    cvjd.dampingRatio = 10.0f;
    ((ConstantVolumeJoint) m_world.createJoint(cvjd)).inflate(1.5f);

    cvjd = new ConstantVolumeJointDef();
    for (int i = nBodies / 4; i < nBodies / 2; ++i) {
      cvjd.addBody(bodies.get(i));
    }
    cvjd.frequencyHz = 10.0f;
    cvjd.dampingRatio = 10.0f;
    ((ConstantVolumeJoint) m_world.createJoint(cvjd)).inflate(1.5f);

    cvjd = new ConstantVolumeJointDef();
    for (int i = nBodies / 2; i < 3 * nBodies / 4; ++i) {
      cvjd.addBody(bodies.get(i));
    }

    cvjd.frequencyHz = 10.0f;
    cvjd.dampingRatio = 10.0f;
    ((ConstantVolumeJoint) m_world.createJoint(cvjd)).inflate(1.5f);

    cvjd = new ConstantVolumeJointDef();
    for (int i = 3 * nBodies / 4; i < nBodies; ++i) {
      cvjd.addBody(bodies.get(i));
    }

    cvjd.frequencyHz = 10.0f;
    cvjd.dampingRatio = 10.0f;
    ((ConstantVolumeJoint) m_world.createJoint(cvjd)).inflate(1.5f);

    //		cvjd = new ConstantVolumeJointDef();
    //		for (int i=0+nBodies/8; i<nBodies/4+nBodies/8; ++i) {
    //			cvjd.addBody(bodies.get(i));
    //		}
    //		cvjd.frequencyHz = 10.0f;
    //		cvjd.dampingRatio = 10.0f;
    //		m_world.createJoint(cvjd);
    //
    //		cvjd = new ConstantVolumeJointDef();
    //		for (int i=nBodies/4+nBodies/8; i<nBodies/2+nBodies/8; ++i) {
    //			cvjd.addBody(bodies.get(i));
    //		}
    //		cvjd.frequencyHz = 10.0f;
    //		cvjd.dampingRatio = 10.0f;
    //		m_world.createJoint(cvjd);
    //
    //		cvjd = new ConstantVolumeJointDef();
    //		for (int i=nBodies/2+nBodies/8; i<3*nBodies/4+nBodies/8; ++i) {
    //			cvjd.addBody(bodies.get(i));
    //		}
    //		cvjd.frequencyHz = 10.0f;
    //		cvjd.dampingRatio = 10.0f;
    //		m_world.createJoint(cvjd);
    //		cvjd = new ConstantVolumeJointDef();
    //		for (int i=3*nBodies/4+nBodies/8; i<nBodies+nBodies/8; ++i) {
    //			int index = i;
    //			if (index >= bodies.size()) index -= bodies.size();
    //			cvjd.addBody(bodies.get(index));
    //		}
    //
    //		cvjd.frequencyHz = 10.0f;
    //		cvjd.dampingRatio = 10.0f;
    //		m_world.createJoint(cvjd);

    //		cvjd = new ConstantVolumeJointDef();
    //		cvjd.addBody(bodies.get(0));
    //		cvjd.addBody(bodies.get(nBodies/4));
    //		cvjd.addBody(bodies.get(nBodies/2));
    //		cvjd.addBody(bodies.get(3*nBodies/4));
    //		cvjd.frequencyHz = 10.0f;
    //		m_world.createJoint(cvjd);
    //
    //		cvjd = new ConstantVolumeJointDef();
    //		cvjd.addBody(bodies.get(0 + nBodies/8));
    //		cvjd.addBody(bodies.get(nBodies/4 + nBodies/8));
    //		cvjd.addBody(bodies.get(nBodies/2 + nBodies/8));
    //		cvjd.addBody(bodies.get(3*nBodies/4 + nBodies/8));
    //		cvjd.frequencyHz = 1.0f;
    //		m_world.createJoint(cvjd);

    BodyDef bd2 = new BodyDef();
    PolygonDef psd = new PolygonDef();
    psd.setAsBox(3.0f, 1.5f, new Vec2(cx, cy + 15.0f), 0.0f);
    psd.density = 1.0f;
    bd2.position = new Vec2(cx, cy + 15.0f);
    Body fallingBox = m_world.createBody(bd2);
    fallingBox.createShape(psd);
    fallingBox.setMassFromShapes();
  }
  @Override
  public void create() {
    if (firstTime) {
      setCamera(0f, 2f, 35f);
      firstTime = false;
    }

    // m_world.setGravity(new Vec2(0.0f,0.0f));

    Body ground = null;
    {
      PolygonDef sd = new PolygonDef();
      sd.setAsBox(5.0f, 0.5f);

      BodyDef bd = new BodyDef();
      bd.position.set(0.0f, 0.0f);
      ground = m_world.createBody(bd);
      ground.createShape(sd);
      sd.setAsBox(1.0f, 0.2f, new Vec2(0.0f, 4.0f), -0.2f);
      ground.createShape(sd);
      sd.setAsBox(1.5f, 0.2f, new Vec2(-1.2f, 5.2f), -1.5f);
      ground.createShape(sd);
      sd.setAsBox(0.5f, 50.0f, new Vec2(5.0f, 0.0f), 0.0f);
      ground.createShape(sd);

      sd.setAsBox(0.5f, 3.0f, new Vec2(-8.0f, 0.0f), 0.0f);
      ground.createShape(sd);

      sd.setAsBox(2.0f, 0.1f, new Vec2(-6.0f, -2.8f), 0.1f);
      ground.createShape(sd);

      CircleDef cd = new CircleDef();
      cd.radius = 0.5f;
      cd.localPosition.set(-0.5f, -4.0f);
      ground.createShape(cd);
    }

    liquid = new Body[nParticles];
    float massPerParticle = totalMass / nParticles;
    //		PointDef pd = new PointDef();
    //		pd.mass = massPerParticle;
    //		pd.restitution = 0.0f;
    //		pd.filter.groupIndex = -10;
    CircleDef pd = new CircleDef();
    pd.density = 1.0f;
    pd.filter.groupIndex = -10;
    pd.radius = .05f;
    pd.restitution = 0.4f;
    pd.friction = 0.0f;
    float cx = 0.0f;
    float cy = 25.0f;
    for (int i = 0; i < nParticles; ++i) {
      BodyDef bd = new BodyDef();
      bd.position =
          new Vec2(
              parent.random(cx - boxWidth * .5f, cx + boxWidth * .5f),
              parent.random(cy - boxHeight * .5f, cy + boxHeight * .5f));
      bd.fixedRotation = true;
      Body b = m_world.createBody(bd);
      b.createShape(pd).setUserData(LIQUID_INT);
      MassData md = new MassData();
      md.mass = massPerParticle;
      md.I = 1.0f;
      b.setMass(md);
      b.allowSleeping(false);
      liquid[i] = b;
    }

    PolygonDef polyDef = new PolygonDef();
    polyDef.setAsBox(parent.random(0.3f, 0.7f), parent.random(0.3f, 0.7f));
    polyDef.density = 1.0f;
    BodyDef bodyDef = new BodyDef();
    bodyDef.position = new Vec2(0.0f, 25.0f);
    bod = m_world.createBody(bodyDef);
    bod.createShape(polyDef);
    bod.setMassFromShapes();
  }
示例#4
0
 @Override
 protected void polyDefB4CreationCallback(PolygonDef def) {
   super.polyDefB4CreationCallback(def);
   def.isSensor = true; // THIS AS SENSOR!
 }