Ejemplo n.º 1
0
  @Override
  public void create() {
    {
      PolygonDef sd = new PolygonDef();
      sd.setAsBox(50.0f, 10.0f);

      BodyDef bd = new BodyDef();
      bd.position = new Vec2(0.0f, -10.0f);
      m_world.createStaticBody(bd).createShape(sd);
    }

    {
      PolygonDef sd = new PolygonDef();
      float w = 4.0f;
      float h = 0.25f;
      sd.setAsBox(w, h);
      sd.density = 1.0f;
      sd.friction = 0.3f;
      sd.restitution = 0.0f;

      BodyDef bd = new BodyDef();

      int numSlats = 8;
      float lastCMX = 0.0f;
      float eps = 0.14f;
      for (int i = 0; i < numSlats; ++i) {
        float newX = lastCMX + w - eps;
        lastCMX = (i * lastCMX + newX) / (i + 1);
        bd.position = new Vec2(newX, .25f + 2 * h * (numSlats - i - 1));
        Body myBody = m_world.createDynamicBody(bd);
        myBody.createShape(sd);
        myBody.setMassFromShapes();
      }
    }
  }
Ejemplo n.º 2
0
  public void create() {
    if (firstTime) {
      setCamera(0f, 10f, 10f);
      firstTime = false;
    }

    {
      PolygonDef sd = new PolygonDef();
      sd.setAsBox(50.0f, 10.0f, new Vec2(0.0f, -10.0f), 0.0f);

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

      sd.setAsBox(0.1f, 10.0f, new Vec2(20.0f, 10.0f), 0.0f);
      ground.createShape(sd);
    }

    float[] xs = {0.0f, -10.0f, -5.0f, 5.0f, 10.0f};

    for (int j = 0; j < xs.length; ++j) {
      PolygonDef sd = new PolygonDef();
      sd.setAsBox(0.5f, 0.5f);
      sd.density = 1.0f;
      sd.friction = 0.3f;

      for (int i = 0; i < 12; ++i) {
        BodyDef bd = new BodyDef();

        // For this test we are using continuous physics for all boxes.
        // This is a stress test, you normally wouldn't do this for
        // performance reasons.
        bd.isBullet = true;
        bd.allowSleep = true;

        // float32 x = b2Random(-0.1f, 0.1f);
        // float32 x = i % 2 == 0 ? -0.025f : 0.025f;
        bd.position.set(xs[j] + parent.random(-.05f, .05f), 0.752f + 1.54f * i);
        // bd.position.Set(xs[j], 2.51f + 4.02f * i);
        Body body = m_world.createDynamicBody(bd);

        body.createShape(sd);
        body.setMassFromShapes();
      }
    }
  }
  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();
    }
  }
Ejemplo n.º 4
0
  @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();
  }