Esempio 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();
      }
    }
  }
  public PhysicsRectangle(float x, float y, float w, float h, World world) {
    super(x, y, world);
    this.width = w;
    this.height = h;

    // Create collision rectangle
    PolygonDef shapeDef = new PolygonDef();
    // Make sure it has non-zero density, otherwise it will have 0 mass and be static
    shapeDef.density = 0.1f;
    shapeDef.setAsBox(getWidth() / 2, getHeight() / 2);
    getBody().createShape(shapeDef);

    // Set mass of body
    getBody().setMassFromShapes();
  }
Esempio n. 3
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();
      }
    }
  }