Пример #1
0
  @Override
  public void create() {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();

    Gdx.input.setInputProcessor(this);

    mB2Controllers = new Array<B2Controller>();

    mCamPos = new Vector3();
    mCurrentPos = new Vector3();

    camera = new OrthographicCamera(100, 100 * h / w);
    camera.position.set(50, 50, 0);
    camera.zoom = 1.8f;
    camera.update();

    loader = new RubeSceneLoader();

    scene = loader.loadScene(Gdx.files.internal("data/palmcontrollers.json"));

    debugRender = new Box2DDebugRenderer();

    batch = new SpriteBatch();
    polygonBatch = new PolygonSpriteBatch();

    textureMap = new HashMap<String, Texture>();
    textureRegionMap = new HashMap<Texture, TextureRegion>();

    createSpatialsFromRubeImages(scene);
    createPolySpatialsFromRubeFixtures(scene);

    mWorld = scene.getWorld();
    // configure simulation settings
    mVelocityIter = scene.velocityIterations;
    mPositionIter = scene.positionIterations;
    if (scene.stepsPerSecond != 0) {
      mSecondsPerStep = 1f / scene.stepsPerSecond;
    }
    mWorld.setContactListener(this);

    //
    // example of custom property handling
    //
    Array<Body> bodies = scene.getBodies();
    if ((bodies != null) && (bodies.size > 0)) {
      for (int i = 0; i < bodies.size; i++) {
        Body body = bodies.get(i);
        String gameInfo = (String) scene.getCustom(body, "GameInfo", null);
        if (gameInfo != null) {
          System.out.println("GameInfo custom property: " + gameInfo);
        }
      }
    }

    // Instantiate any controllers that are in the scene
    Array<Fixture> fixtures = scene.getFixtures();
    if ((fixtures != null) && (fixtures.size > 0)) {
      for (int i = 0; i < fixtures.size; i++) {
        Fixture fixture = fixtures.get(i);
        int controllerType = (Integer) scene.getCustom(fixture, "ControllerType", 0);
        switch (controllerType) {
          case B2Controller.BUOYANCY_CONTROLLER:
            // only allow polygon buoyancy controllers for now..
            if (fixture.getShape().getType() == Shape.Type.Polygon) {
              float bodyHeight = fixture.getBody().getPosition().y;
              // B2BuoyancyController b2c = new B2BuoyancyController();

              // need to calculate the fluid surface height for the buoyancy controller
              PolygonShape shape = (PolygonShape) fixture.getShape();
              shape.getVertex(0, mTmp);
              float maxHeight =
                  mTmp.y + bodyHeight; // initialize the height, transforming to 'world'
              // coordinates

              // find the maxHeight
              for (int j = 1; j < shape.getVertexCount(); j++) {
                shape.getVertex(j, mTmp);
                maxHeight =
                    Math.max(maxHeight, mTmp.y + bodyHeight); // transform to world coordinates
              }
              B2BuoyancyController b2c =
                  new B2BuoyancyController(
                      B2BuoyancyController.DEFAULT_SURFACE_NORMAL, // assume up
                      (Vector2)
                          scene.getCustom(
                              fixture,
                              "ControllerVelocity",
                              B2BuoyancyController.DEFAULT_FLUID_VELOCITY),
                      mWorld.getGravity(),
                      maxHeight,
                      fixture.getDensity(),
                      (Float)
                          scene.getCustom(
                              fixture, "LinearDrag", B2BuoyancyController.DEFAULT_LINEAR_DRAG),
                      (Float)
                          scene.getCustom(
                              fixture, "AngularDrag", B2BuoyancyController.DEFAULT_ANGULAR_DRAG));
              fixture.setUserData(b2c); // reference back to the controller from the fixture (see
              // beginContact/endContact)
              mB2Controllers.add(b2c); // add it to the list so it can be stepped later
            }
            break;

          case B2Controller.GRAVITY_CONTROLLER:
            {
              B2GravityController b2c = new B2GravityController();
              b2c =
                  new B2GravityController(
                      (Vector2)
                          scene.getCustom(
                              fixture, "ControllerVelocity", B2GravityController.DEFAULT_GRAVITY));
              fixture.setUserData(b2c);
              mB2Controllers.add(b2c);
            }
            break;
        }
      }
    }
    scene.printStats();
    scene.clear(); // no longer need any scene references
  }
Пример #2
0
 /**
  * adjust the density so that the mass has the given value
  *
  * @param mass
  */
 public void changeMass(float mass) {
   fixture.setDensity(mass / body.getMass() * fixture.getDensity());
   body.resetMassData();
 }
  /**
   * @param f - fixture that is affected
   * @return true if force was applied, false otherwise.
   */
  private boolean ApplyToFixture(Fixture f) {
    float shapeDensity = mUseDensity ? f.getDensity() : mFluidDensity;

    // don't bother with buoyancy on sensors or fixtures with no density
    if (f.isSensor() || (shapeDensity == 0)) {
      return false;
    }
    Body body = f.getBody();
    mAreac.set(Vector2.Zero);
    mMassc.set(Vector2.Zero);
    float area = 0;

    // Get shape for displacement area calculations
    Shape shape = f.getShape();

    mSC.set(Vector2.Zero);
    float sarea;
    switch (shape.getType()) {
      case Circle:
        sarea =
            B2ShapeExtensions.ComputeSubmergedArea(
                (CircleShape) shape, mSurfaceNormal, mSurfaceHeight, body.getTransform(), mSC);
        break;

      case Chain:
        sarea =
            B2ShapeExtensions.ComputeSubmergedArea(
                (ChainShape) shape, mSurfaceNormal, mSurfaceHeight, body.getTransform(), mSC);
        break;

      case Edge:
        sarea =
            B2ShapeExtensions.ComputeSubmergedArea(
                (EdgeShape) shape, mSurfaceNormal, mSurfaceHeight, body.getTransform(), mSC);
        break;

      case Polygon:
        sarea =
            B2ShapeExtensions.ComputeSubmergedArea(
                (PolygonShape) shape, mSurfaceNormal, mSurfaceHeight, body.getTransform(), mSC);
        break;

      default:
        sarea = 0;
        break;
    }

    area += sarea;
    mAreac.x += sarea * mSC.x;
    mAreac.y += sarea * mSC.y;
    float mass = sarea * shapeDensity;
    mMassc.x += sarea * mSC.x * shapeDensity;
    mMassc.y += sarea * mSC.y * shapeDensity;

    mAreac.x /= area;
    mAreac.y /= area;
    mMassc.x /= mass;
    mMassc.y /= mass;
    if (area < Float.MIN_VALUE) {
      return false;
    }

    if (DEBUG_BUOYANCY) {
      // Run debug w/HCR to see the effects of different fluid densities / linear drag
      mFluidDensity = 2f;
      mLinearDrag = 5;
      mAngularDrag = 2;
    }

    // buoyancy force.
    mTmp.set(mGravity).scl(-mFluidDensity * area);
    body.applyForce(mTmp, mMassc, true); // multiply by -density to invert gravity

    // linear drag.
    mTmp.set(
        body.getLinearVelocityFromWorldPoint(mAreac).sub(mFluidVelocity).mul(-mLinearDrag * area));
    body.applyForce(mTmp, mAreac, true);

    // angular drag.
    float bodyMass = body.getMass();
    if (bodyMass < 1) // prevent a huge torque from being generated...
    {
      bodyMass = 1;
    }
    float torque = -body.getInertia() / bodyMass * area * body.getAngularVelocity() * mAngularDrag;
    body.applyTorque(torque, true);
    return true;
  }