/**
  * This computes the "C" value in the geomipmapping paper. See section "2.3.1.2 Pre-calculating d"
  *
  * @param cam
  * @param pixelLimit
  * @return
  */
 private float getCameraConstant(Camera cam, float pixelLimit) {
   float n = cam.getFrustumNear();
   float t = FastMath.abs(cam.getFrustumTop());
   float A = n / t;
   float v_res = cam.getHeight();
   float T = (2f * pixelLimit) / v_res;
   return A / T;
 }
Exemplo n.º 2
0
  public float getHeadingAtWP(int index) {
    float heading = 0;
    Waypoint nextWayPoint = getNextWayPoint(index);

    // if next way point available, compute heading towards it
    if (nextWayPoint != null) {
      // compute driving direction by looking at next way point from current position
      Vector3f targetPosition = nextWayPoint.getPosition().clone();
      targetPosition.setY(0);

      Vector3f currentPosition = waypointList.get(index).getPosition().clone();
      currentPosition.setY(0);

      Vector3f drivingDirection = targetPosition.subtract(currentPosition).normalize();

      // compute heading (orientation) from driving direction vector for
      // angle between driving direction and heading "0"
      float angle0 = drivingDirection.angleBetween(new Vector3f(0, 0, -1));
      // angle between driving direction and heading "90"
      float angle90 = drivingDirection.angleBetween(new Vector3f(1, 0, 0));

      // get all candidates for heading
      // find the value from {heading1,heading2} which matches with one of {heading3,heading4}
      float heading1 = (2.0f * FastMath.PI + angle0) % FastMath.TWO_PI;
      float heading2 = (2.0f * FastMath.PI - angle0) % FastMath.TWO_PI;
      float heading3 = (2.5f * FastMath.PI + angle90) % FastMath.TWO_PI;
      float heading4 = (2.5f * FastMath.PI - angle90) % FastMath.TWO_PI;

      float diff_1_3 = FastMath.abs(heading1 - heading3);
      float diff_1_4 = FastMath.abs(heading1 - heading4);
      float diff_2_3 = FastMath.abs(heading2 - heading3);
      float diff_2_4 = FastMath.abs(heading2 - heading4);

      if ((diff_1_3 < diff_1_4 && diff_1_3 < diff_2_3 && diff_1_3 < diff_2_4)
          || (diff_1_4 < diff_1_3 && diff_1_4 < diff_2_3 && diff_1_4 < diff_2_4)) {
        // if diff_1_3 or diff_1_4 are smallest --> the correct heading is heading1
        heading = heading1;
      } else {
        // if diff_2_3 or diff_2_4 are smallest --> the correct heading is heading2
        heading = heading2;
      }
    }
    return heading;
  }
Exemplo n.º 3
0
 public Wall(Vector3f start, float xi, float zi, int spaces) {
   float x = start.getX();
   float z = start.getZ();
   float xl = 0;
   float zl = 0;
   Vector3f loc = new Vector3f(0, 0, 0);
   int i = 0;
   ends[0] = start.clone();
   while (i < spaces) {
     loc = new Vector3f(x + (i * xi), start.getY(), z + (i * zi));
     if (world.get(loc) != null
         && (world.get(loc).contains("h") || world.get(loc).contains("w"))) {
       break;
     } else {
       world.put(loc, "w");
     }
     xl += xi;
     zl += zi;
     i++;
   }
   i--;
   if (i > 0) {
     spaces -= i;
   } else {
     spaces -= 1;
   }
   if (spaces > 0) {
     walls.add(new Wall(loc.clone().add(xi, 0, zi), xi, zi, spaces));
   }
   if (xl == 0 && zl == 0) {
     return;
   }
   loc = new Vector3f(x + ((i / 2f) * xi), start.getY(), z + ((i / 2f) * zi));
   map.add(
       geoWall(
           loc,
           xl,
           zl,
           T.getMaterialPath("synthetic"),
           new Vector2f(Math.max(FastMath.abs(xl), FastMath.abs(zl)), 2),
           true));
 }
Exemplo n.º 4
0
 public static GeometryData geoWall(
     Vector3f loc, float xl, float zl, String tex, Vector2f scale, boolean phy) {
   if (xl == 0) {
     xl = WW;
   } else {
     xl *= WW;
   }
   xl = FastMath.abs(xl);
   if (zl == 0) {
     zl = WW;
   } else {
     zl *= WW;
   }
   zl = FastMath.abs(zl);
   return new GeometryData(
       "box",
       new Vector3f(xl, WW * 2, zl),
       new Vector3f(loc.x * ZS, (loc.y * ZS) + ZS, loc.z * ZS),
       null,
       tex,
       scale,
       phy);
 }
Exemplo n.º 5
0
    public static float turbulence(
        float x,
        float y,
        float z,
        float noiseSize,
        int noiseDepth,
        int noiseBasis,
        boolean isHard) {
      NoiseFunction abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(noiseBasis));
      if (abstractNoiseFunc == null) {
        abstractNoiseFunc = noiseFunctions.get(0);
        noiseBasis = 0;
      }

      if (noiseBasis == 0) {
        ++x;
        ++y;
        ++z;
      }
      if (noiseSize != 0.0) {
        noiseSize = 1.0f / noiseSize;
        x *= noiseSize;
        y *= noiseSize;
        z *= noiseSize;
      }

      float sum = 0, t, amp = 1, fscale = 1;
      for (int i = 0; i <= noiseDepth; ++i, amp *= 0.5, fscale *= 2) {
        t = abstractNoiseFunc.execute(fscale * x, fscale * y, fscale * z);
        if (isHard) {
          t = FastMath.abs(2.0f * t - 1.0f);
        }
        sum += t * amp;
      }

      sum *= (float) (1 << noiseDepth) / (float) ((1 << noiseDepth + 1) - 1);
      return sum;
    }
  /** @param t = physicsTick time */
  @Override
  public void update(float t) { // Updated from physicsTickListener
    float driveAngle =
        vehicleControl.getLinearVelocity().angleBetween(vehicleControl.getForwardVector(null));
    int direction = 2;
    float gasValue, brakeValue;

    if (throttleValue > 0) {
      gasValue = throttleValue;
      brakeValue = 0;
    } else if (throttleValue < 0) {
      brakeValue = Math.abs(throttleValue);
      gasValue = 0;
    } else gasValue = brakeValue = 0;
    // System.out.println(Math.abs(driveAngle));

    if (Math.abs(driveAngle - (FastMath.PI / 2f)) < .1f) {
      // System.out.println("Stopped");
      direction = 0;
    } else if ((FastMath.PI / 2f) - driveAngle > 0) {
      // System.out.println("Moving forwards");
      direction = 1;
    } else {
      // System.out.println("Moving backwards");
      direction = -1;
    }

    speed =
        FastMath.abs(
            vehicleControl
                .getLinearVelocity()
                .dot(vehicleControl.getForwardVector(null).normalize()));
    float Ftraction = 0, Fdrag = 0, Frr = 0, FdragMag, FrrMag, FtractionNet = 0, Fbrake = 0;
    // Vector3f forward = vehicleControl.getForwardVector(null).normalize();
    float gearRatio = gearBox.getRatioFromGear(currentGear);
    engineRPM = getDriveWheelRevPerSecAverage(speed) * gearRatio * 2.86f * 60f;
    if (engineRPM < engine.getIdleRpm()) engineRPM = engine.getIdleRpm();

    // float Cdrag = .1f * .3f * 2.2f * 1.29f;
    float Cdrag = .33f;
    float Crr = 0;
    FdragMag = Cdrag * (float) Math.pow(speed, 2);
    FrrMag = Crr * speed;

    torque = gasValue * engine.getTorqueAtRPM(engineRPM, accelerate);
    float radius = 0.2286f;
    Ftraction = (torque * gearRatio * 2.86f * .7f) / radius;
    Fbrake = 0;
    switch (shifter.getGear()) {
      case "DRIVE":
        // in Drive
        if (currentGear < 1) {
          currentGear = 1;
        }
        /*if (direction == 1) {
        }
        if (throttleValue > 0){
        Fbrake = 0;
        } else if (throttleValue < 0 && speed > .1f) {
        Fbrake = -3000;
        Ftraction = 0;
        } else if (throttleValue < 0){
        Fbrake = 0;
        Ftraction = 0;
        } else if (throttleValue == 0){
        Ftraction = Fbrake = 0;
        }*/
        if (engineRPM >= engine.getShiftUpRpsLimit() * 60f && currentGear > 0) {
          gearUp();
        }
        if (engineRPM <= 60f * engine.getShiftDownRpsLimit() && currentGear > 1) {
          gearDown();
        }
        break;
      case "REVERSE":
        if (currentGear > 0) {
          currentGear = 0;
        }
        Ftraction *= -1;

        /*if (throttleValue > 0){
            Ftraction *= -1;
            Fbrake = 0;
        } else if (throttleValue < 0 && speed > .1f) {
            Fbrake = 3000;
            Ftraction = 0;
        } else if (throttleValue < 0){
            Fbrake = 0;
            Ftraction = 0;
            //speed = 0;
        } else if (throttleValue == 0){
            Ftraction = Fbrake = 0;
        }*/
        break;
      case "PARK":
        engineRPM = gasValue * (engine.getMaxRpm() - engine.getIdleRpm()) + engine.getIdleRpm();
        Ftraction = 0;
        // Fbrake += -10000;
        break;
      case "NEUTRAL":
        engineRPM = gasValue * (engine.getMaxRpm() - engine.getIdleRpm()) + engine.getIdleRpm();
        Ftraction = 0;
        break;
    }

    // System.out.println(direction);
    switch (direction) {
      case -1:
        if (shifter.getGear().equals("PARK")) Fbrake += 1000;

        if (brakeValue > 0) Fbrake += 3000f * brakeValue;

        Fdrag = FdragMag;
        Frr = FrrMag;

        break;
      case 0:
        if (speed < .05f)
          vehicleControl.setLinearVelocity(
              new Vector3f(0, vehicleControl.getLinearVelocity().getY(), 0));
        // System.out.println("STOPPED");
        break;
      case 1:
        if (shifter.getGear().equals("PARK")) Fbrake += -1000;

        // System.out.println(brakeValue);

        if (brakeValue > 0) Fbrake += -3000f * brakeValue;

        Fdrag = -FdragMag;
        Frr = -FrrMag;
        break;
      default:
        if (brakeValue > 0) Fbrake += -3000f * brakeValue;
        break;
    }

    FtractionNet = Ftraction + Fbrake + Fdrag + Frr;

    addVehicleTorque(FtractionNet, accelerate, speed);
  }
Exemplo n.º 7
0
    public Hallway(Vector3f start, float xi, float zi) {
      float x = start.getX() + xi;
      float z = start.getZ() + zi;
      float rng, dist;
      int len = 0;
      int spread = 0;
      int lenMax = FastMath.nextRandomInt(HALL_LENGTH_MIN, HALL_LENGTH_MAX);
      boolean b = false;
      // Make sure both xi and zi have an absolute value of 1:
      xi = A.sign(xi);
      zi = A.sign(zi);
      // Assign the first 2 corners:
      corners[0] =
          new Vector3f(
              (zi * (HALL_WIDTH - 1)) + x,
              start.getY(),
              (xi * (HALL_WIDTH - 1)) + z); // Bottom Left
      corners[1] =
          new Vector3f(
              (-zi * (HALL_WIDTH - 1)) + x,
              start.getY(),
              (-xi * (HALL_WIDTH - 1)) + z); // Bottom Right
      Vector3f left = corners[0].clone();
      Vector3f right = corners[1].clone();
      ArrayList<HallData> newHalls = new ArrayList(1);
      while (len <= lenMax) {
        if (world.get(left) != null && world.get(left).contains("h")) {
          b = true;
        }
        if (world.get(right) != null && world.get(right).contains("h")) {
          b = true;
        }
        // Check each of the spaces in this step for hallway:
        if (b) {
          right.addLocal(-xi, 0, -zi);
          left.addLocal(-xi, 0, -zi);
          break;
        }
        world.put(left.clone(), "h");
        world.put(right.add(zi, 0, xi), "h");
        world.put(right.clone(), "h");
        // Check distance & random to see if more hallways should be created:
        dist = left.distance(Vector3f.ZERO);
        rng = FastMath.nextRandomFloat();
        if (dist < HALL_MAX_RADIUS && spread > HALL_SPREAD && len < lenMax) {
          if (rng < 0.13f) {
            newHalls.add(new HallData(left.clone(), zi, xi));
            spread = 0;
          } else if (rng < 0.26f) {
            newHalls.add(new HallData(right.clone(), -zi, -xi));
            spread = 0;
          } else if (rng < 0.33f) {
            newHalls.add(new HallData(left.clone(), zi, xi));
            newHalls.add(new HallData(right.clone(), -zi, -xi));
            spread = 0;
          }
        }
        x += xi;
        z += zi;
        spread++;
        len++;
        left.addLocal(xi, 0, zi);
        right.addLocal(xi, 0, zi);
      }
      corners[2] = right.clone(); // Top Right
      corners[3] = left.clone(); // Top Left

      // Generate hallways:
      int j = 0;
      while (j < newHalls.size()) {
        hallways.add(new Hallway(newHalls.get(j).start, newHalls.get(j).xi, newHalls.get(j).zi));
        j++;
      }

      // Return if there's no hallway to generate (0 in size):
      float xs = FastMath.abs(corners[1].getX() - corners[3].getX()) + 1;
      float zs = FastMath.abs(corners[1].getZ() - corners[3].getZ()) + 1;
      if (Math.min(FastMath.abs(xs), FastMath.abs(zs)) < 1) {
        return;
      }

      // Generate the front wall:
      walls.add(new Wall(left.add(xi, 0, zi), -zi, -xi, HALL_WIDTH * 2 - 1));
      walls.add(new Wall(left.add(zi, 0, xi), -xi, -zi, len + 1));
      walls.add(new Wall(right.add(-zi, 0, -xi), -xi, -zi, len + 1));

      // Generate the actual floor:
      float xloc = (corners[3].getX() + corners[1].getX()) * 0.5f;
      float zloc = (corners[3].getZ() + corners[1].getZ()) * 0.5f;
      NPCManager.addNew("grunt", new Vector3f(xloc, start.getY(), zloc).mult(ZS).add(0, 5, 0));
      center = new Vector3f(xloc, start.getY() - 0.5f, zloc);
      floor = geoFloor(center, xs, zs, T.getMaterialPath("BC_Tex"), new Vector2f(zs, xs), true);
      map.add(floor);
    }
 private static boolean approxEqual(Vector2f u, Vector2f v) {
   float tolerance = 1E-4f;
   return (FastMath.abs(u.x - v.x) < tolerance) && (FastMath.abs(u.y - v.y) < tolerance);
 }