Exemplo n.º 1
0
  /** Do all calculations, handle input, etc. */
  private static void logic() {

    if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
      finished = true;
    }

    int dx = Mouse.getDX(), dy = Mouse.getDY(), dz = Mouse.getDWheel();

    if (Mouse.isButtonDown(0) && Mouse.isInsideWindow()) {
      Vector3 d = new Vector3(dx, dy, 0);
      anchorL = anchorL.sub(d);
      anchorR = anchorR.sub(d);
    }

    if (Mouse.isButtonDown(1) && Mouse.isInsideWindow()) {
      Vector3 d = new Vector3(0, dy, 0);
      anchorL = anchorL.sub(d);
      anchorR = anchorR.add(d);
    }

    Vector3 zChange = new Vector3(0, 0, dz / 4);
    anchorL = anchorL.add(zChange);
    anchorR = anchorR.add(zChange);

    /*
     * Move those particles!
     */

    // Clear force accumulator
    for (Particle p : system) p.clearForces();

    // Accumulate forces
    for (Particle p : system) p.addForce(gravity);

    // Integrate
    for (Particle p : system) NewtonianIntegrators.verlet(p, stepSize);

    /*
     * Formal and informal constraint check.
     */

    for (int i = 0; i < integrationIterations; i++) {
      cloth[0][0][0].x = anchorL;
      cloth[cloth[0].length / 2][0][0].x = anchorL.add(anchorR).scale(.5f);
      cloth[cloth[0].length - 1][0][0].x = anchorR;
      // bounds check
      float dragFloat = 10;
      Vector3 zFloor = new Vector3(0, 0, 1);
      for (Particle p : system) {
        if (p.x.z <= 0 + 1) {
          p.x = p.x.add(zFloor);
          p.xOld = p.x.add(p.xOld.scale(dragFloat)).scale(1f / (dragFloat + 1));
        }
      }
      // for (Particle p : system) {
      // if (cube.isWithin(p.x)) {
      // cube.escape(p);
      // }
      // }
      for (Constraint c : constraints) c.satisfy();
    }
  }