Exemplo n.º 1
0
  @Override
  public void nextFrame(StageGameState b) {
    // if animation is halfway through
    MeshAnimationController animControl =
        (MeshAnimationController) character.model.getController(0);
    if (phase == 0
        && animControl.getCurTime() + b.tpf >= animControl.getAnimationLength("cast") / 2) {

      phase++;

      // calculate transformations
      Vector3f rotation = character.rotationVector;
      float angle = FastMath.atan2(rotation.x, rotation.z);
      Vector3f translation = new Vector3f(2 * FastMath.sin(angle), 3.7f, 2 * FastMath.cos(angle));

      Echo e = (Echo) LoadingQueue.quickLoad(new Echo(getUsers(), target), b);

      e.rotate(rotation);
      e.setVelocity(rotation.mult(e.speed));
      e.model.setLocalTranslation(character.model.getWorldTranslation().add(translation));

      b.getRootNode().updateRenderState();

    } else if (character.play("cast", b.tpf)) {
      finish();
    }
  }
Exemplo n.º 2
0
  private static boolean coplanarTriTri(
      Vector3f n, Vector3f v0, Vector3f v1, Vector3f v2, Vector3f u0, Vector3f u1, Vector3f u2) {
    Vector3f a = new Vector3f();
    short i0, i1;
    a.x = FastMath.abs(n.x);
    a.y = FastMath.abs(n.y);
    a.z = FastMath.abs(n.z);

    if (a.x > a.y) {
      if (a.x > a.z) {
        i0 = 1; /* a[0] is greatest */
        i1 = 2;
      } else {
        i0 = 0; /* a[2] is greatest */
        i1 = 1;
      }
    } else /* a[0] <=a[1] */ {
      if (a.z > a.y) {
        i0 = 0; /* a[2] is greatest */
        i1 = 1;
      } else {
        i0 = 0; /* a[1] is greatest */
        i1 = 2;
      }
    }

    /* test all edges of triangle 1 against the edges of triangle 2 */
    float[] v0f = new float[3];
    v0.toArray(v0f);
    float[] v1f = new float[3];
    v1.toArray(v1f);
    float[] v2f = new float[3];
    v2.toArray(v2f);
    float[] u0f = new float[3];
    u0.toArray(u0f);
    float[] u1f = new float[3];
    u1.toArray(u1f);
    float[] u2f = new float[3];
    u2.toArray(u2f);
    if (edgeAgainstTriEdges(v0f, v1f, u0f, u1f, u2f, i0, i1)) {
      return true;
    }

    if (edgeAgainstTriEdges(v1f, v2f, u0f, u1f, u2f, i0, i1)) {
      return true;
    }

    if (edgeAgainstTriEdges(v2f, v0f, u0f, u1f, u2f, i0, i1)) {
      return true;
    }

    /* finally, test if tri1 is totally contained in tri2 or vice versa */
    pointInTri(v0f, u0f, u1f, u2f, i0, i1);
    pointInTri(u0f, v0f, v1f, v2f, i0, i1);

    return false;
  }
Exemplo n.º 3
0
 private void readNormal(Vector3f norm) {
   int zenith = file.readByte();
   int azimuth = file.readByte();
   float lat = (zenith * 2 * FastMath.PI) / 255;
   float lng = (azimuth * 2 * FastMath.PI) / 255;
   norm.x = FastMath.cos(lat) * FastMath.sin(lng);
   norm.y = FastMath.sin(lat) * FastMath.sin(lng);
   norm.z = FastMath.cos(lng);
 }
  public float getHeight(float x, float z, float time) {
    float zval = z * scaleybig * 4f + time * speedbig * 4f;
    float height = FastMath.sin(zval);
    height *= heightbig;

    if (octaves > 0) {
      float height2 =
          (float) ImprovedNoise.noise(x * scaleybig, z * scalexbig, time * speedbig) * heightbig;
      height = height * 0.4f + height2 * 0.6f;
    }
    if (octaves > 1)
      height +=
          ImprovedNoise.noise(x * scaleysmall, z * scalexsmall, time * speedsmall) * heightsmall;
    if (octaves > 2)
      height +=
          ImprovedNoise.noise(
                  x * scaleysmall * 2.0f, z * scalexsmall * 2.0f, time * speedsmall * 1.5f)
              * heightsmall
              * 0.5f;
    if (octaves > 3)
      height +=
          ImprovedNoise.noise(
                  x * scaleysmall * 4.0f, z * scalexsmall * 4.0f, time * speedsmall * 2.0f)
              * heightsmall
              * 0.25f;

    return height; // + waterHeight
  }
  public void updateRotationMatrix() {

    if (oldEmit.equals(worldEmit)) return;

    float upDotEmit = upVector.dot(worldEmit);
    if (FastMath.abs(upDotEmit) > 1.0d - FastMath.DBL_EPSILON) {
      absUpVector.x = upVector.x <= 0.0f ? -upVector.x : upVector.x;
      absUpVector.y = upVector.y <= 0.0f ? -upVector.y : upVector.y;
      absUpVector.z = upVector.z <= 0.0f ? -upVector.z : upVector.z;
      if (absUpVector.x < absUpVector.y) {
        if (absUpVector.x < absUpVector.z) {
          absUpVector.x = 1.0f;
          absUpVector.y = absUpVector.z = 0.0f;
        } else {
          absUpVector.z = 1.0f;
          absUpVector.x = absUpVector.y = 0.0f;
        }
      } else if (absUpVector.y < absUpVector.z) {
        absUpVector.y = 1.0f;
        absUpVector.x = absUpVector.z = 0.0f;
      } else {
        absUpVector.z = 1.0f;
        absUpVector.x = absUpVector.y = 0.0f;
      }
      absUpVector.subtract(upVector, abUpMinUp);
      absUpVector.subtract(worldEmit, upXemit);
      float f4 = 2.0f / abUpMinUp.dot(abUpMinUp);
      float f6 = 2.0f / upXemit.dot(upXemit);
      float f8 = f4 * f6 * abUpMinUp.dot(upXemit);
      float af1[] = {abUpMinUp.x, abUpMinUp.y, abUpMinUp.z};
      float af2[] = {upXemit.x, upXemit.y, upXemit.z};
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
          matData[i][j] = (-f4 * af1[i] * af1[j] - f6 * af2[i] * af2[j]) + f8 * af2[i] * af1[j];
        }
        matData[i][i]++;
      }

    } else {
      upVector.cross(worldEmit, upXemit);
      float f2 = 1.0f / (1.0f + upDotEmit);
      float f5 = f2 * upXemit.x;
      float f7 = f2 * upXemit.z;
      float f9 = f5 * upXemit.y;
      float f10 = f5 * upXemit.z;
      float f11 = f7 * upXemit.y;
      matData[0][0] = upDotEmit + f5 * upXemit.x;
      matData[0][1] = f9 - upXemit.z;
      matData[0][2] = f10 + upXemit.y;
      matData[1][0] = f9 + upXemit.z;
      matData[1][1] = upDotEmit + f2 * upXemit.y * upXemit.y;
      matData[1][2] = f11 - upXemit.x;
      matData[2][0] = f10 - upXemit.y;
      matData[2][1] = f11 + upXemit.x;
      matData[2][2] = upDotEmit + f7 * upXemit.z;
    }
    rotMatrix.set(matData);
    oldEmit.set(worldEmit);
  }
Exemplo n.º 6
0
  @Override
  public void nextFrame(StageGameState b) {
    if (phase == 0 && character.play("raiseUp", b.tpf)) {
      // allow to animate
      character.animationLock = false;
      phase++;

    } else if (phase >= 1 && phase < numFires + 1 && frameCount % delay == 0) {

      // determine spawn point
      float a = FastMath.nextRandomFloat() * FastMath.PI * 2;
      float p = FastMath.nextRandomFloat() * FastMath.PI * 2;
      Vector3f translation =
          new Vector3f(
              radius * FastMath.sin(p) * FastMath.sin(a),
              radius * FastMath.cos(p),
              radius * FastMath.sin(p) * FastMath.cos(a));

      Fire fire = (Fire) LoadingQueue.quickLoad(new Fire(getUsers()), b);
      fire.setVelocity(translation.mult(.2f / (float) radius));
      fire.model.setLocalTranslation(character.model.getLocalTranslation().add(translation));

      b.getRootNode().updateRenderState();
      phase++;

    } else if (phase >= numFires + 1) {
      finish();
    }
  }
Exemplo n.º 7
0
 /**
  * Initialize a chase camera with the given dynamic view as target.
  *
  * @param view The target <code>DynamicView</code> instance.
  */
 public void initializeCameraHandler(DynamicView view) {
   view.updateGeometricState(0, false);
   this.cameraHandler =
       new SnowmanCameraHandler(
           DisplaySystem.getDisplaySystem().getRenderer().getCamera(), view, this.game);
   Vector3f targetOffset = new Vector3f(0, 0, 0);
   targetOffset.setY(((BoundingBox) view.getWorldBound()).yExtent * 1.5f);
   this.cameraHandler.setTargetOffset(targetOffset);
   Vector3f dir = view.getLocalRotation().mult(new Vector3f(0, 0, -1));
   Vector3f store = new Vector3f();
   this.cameraHandler.setAzimuth(FastMath.cartesianToSpherical(dir, store).y);
 }
 /**
  * Generate a random velocity within the parameters of max angle and the rotation matrix.
  *
  * @param pSpeed a vector to store the results in.
  */
 protected Vector3f getRandomVelocity(Vector3f pSpeed) {
   float randDir = FastMath.TWO_PI * FastMath.nextRandomFloat();
   float randAngle = getRandomAngle();
   if (pSpeed == null) pSpeed = new Vector3f();
   pSpeed.x = FastMath.cos(randDir) * FastMath.sin(randAngle);
   pSpeed.y = FastMath.cos(randAngle);
   pSpeed.z = FastMath.sin(randDir) * FastMath.sin(randAngle);
   rotateVectorSpeed(pSpeed);
   pSpeed.multLocal(getInitialVelocity());
   return pSpeed;
 }
 /**
  * generate a random lifespan between the min and max lifespan of the particle system.
  *
  * @return the generated lifespan value
  */
 public float getRandomLifeSpan() {
   return getMinimumLifeTime()
       + ((getMaximumLifeTime() - getMinimumLifeTime()) * FastMath.nextRandomFloat());
 }
 /**
  * Returns a random angle between the min and max angles.
  *
  * @return the random angle.
  */
 public float getRandomAngle() {
   return getMinimumAngle() + FastMath.nextRandomFloat() * (getMaximumAngle() - getMinimumAngle());
 }
Exemplo n.º 11
0
  /**
   * Calculates an object's distance from the 0,0,0 location
   *
   * @param s The object who's distance is being calculated.
   * @return The distance wrapped into a Vector3f
   */
  public static Vector3f getDistanceFromZero(Spatial s) {
    logger.debug("Examining " + s.getName());

    Vector3f closest = null;
    Vector3f temp = new Vector3f(0, 0, 0);
    int faceCount;

    if (s instanceof TriMesh) {
      faceCount = ((TriMesh) s).getTriangleCount();

      FloatBuffer vb = ((TriMesh) s).getVertexBuffer();
      vb.rewind();

      for (int i = 0; i < faceCount; i++) {

        // If we are on the first run, then the first
        // vertex is the closest.
        if (i == 0) {
          closest = new Vector3f(vb.get(0), vb.get(1), vb.get(2));
          continue;
        }

        // if this isn't the first run, set the temp vertex
        temp.x = vb.get(i * 3);
        temp.y = vb.get(i * 3 + 1);
        temp.z = vb.get(i * 3 + 2);

        // compare i, all three values need to be closer to 0
        if (FastMath.abs(temp.x) < FastMath.abs(closest.x)
            && FastMath.abs(temp.y) < FastMath.abs(closest.y)
            && FastMath.abs(temp.z) < FastMath.abs(closest.z)) {
          closest = temp.clone();
        }
      }

      return closest;
    }
    if (s instanceof QuadMesh) {
      faceCount = ((QuadMesh) s).getQuadCount();

      FloatBuffer vb = ((QuadMesh) s).getVertexBuffer();
      vb.rewind();

      for (int i = 0; i < faceCount; i++) {

        // If we are on the first run, then the first
        // vertex is the closest.
        if (i == 0) {
          closest = new Vector3f(vb.get(0), vb.get(1), vb.get(2));
          continue;
        }

        // if this isn't the first run, set the temp vertex
        temp.x = vb.get(i * 4);
        temp.y = vb.get(i * 4 + 1);
        temp.z = vb.get(i * 4 + 2);

        // compare i, all three values need to be closer to 0
        if (FastMath.abs(temp.x) < FastMath.abs(closest.x)
            && FastMath.abs(temp.y) < FastMath.abs(closest.y)
            && FastMath.abs(temp.z) < FastMath.abs(closest.z)) {
          closest = temp.clone();
        }
      }

      return closest;
    }
    if (s instanceof Node) {
      if (((Node) s).getChildren() == null) {
        logger.warn("initial node null");
        return new Vector3f(0, 0, 0);
      }

      for (Spatial child : ((Node) s).getChildren()) {

        // if the child is a node with no children, don't pass it
        if (child instanceof Node) {
          if (((Node) child).getChildren() == null) {
            logger.debug("Ignoring Node: " + child.getName() + " with no children.");
            continue;
          }
        }

        Vector3f childLocation = getDistanceFromZero(child);

        // check if  distance was returned null.  This should only happen
        // if a Node with no children was checked against.
        if (childLocation == null) continue;

        // closest will be null on the first run
        if (closest == null) {
          closest = childLocation.clone();
          continue;
        }

        if (FastMath.abs(childLocation.x) < FastMath.abs(closest.x)
            && FastMath.abs(childLocation.y) < FastMath.abs(closest.y)
            && FastMath.abs(childLocation.z) < FastMath.abs(closest.z)) {
          closest = childLocation.clone();
        }
      }

      if (closest == null) return new Vector3f(0, 0, 0);
      else return closest;
    }
    logger.warn("Un-Implemented Geometry Type Given");
    throw new IllegalArgumentException("Un-Implemented Geometry Type Given");
  }
Exemplo n.º 12
0
  /**
   * This method tests for the intersection between two triangles defined by their vertexes.
   * Converted to java from C code found at http://www.acm.org/jgt/papers/Moller97/tritri.html
   *
   * @param v0 First triangle's first vertex.
   * @param v1 First triangle's second vertex.
   * @param v2 First triangle's third vertex.
   * @param u0 Second triangle's first vertex.
   * @param u1 Second triangle's second vertex.
   * @param u2 Second triangle's third vertex.
   * @return True if the two triangles intersect, false otherwise.
   */
  public static boolean intersection(
      Vector3f v0, Vector3f v1, Vector3f v2, Vector3f u0, Vector3f u1, Vector3f u2) {
    Vector3f e1 = tempVa;
    Vector3f e2 = tempVb;
    Vector3f n1 = tempVc;
    Vector3f n2 = tempVd;
    float d1, d2;
    float du0, du1, du2, dv0, dv1, dv2;
    Vector3f d = tempVe;
    float[] isect1 = tempFa;
    float[] isect2 = tempFb;
    float du0du1, du0du2, dv0dv1, dv0dv2;
    short index;
    float vp0, vp1, vp2;
    float up0, up1, up2;
    float bb, cc, max;
    float xx, yy, xxyy, tmp;

    /* compute plane equation of triangle(v0,v1,v2) */
    v1.subtract(v0, e1);
    v2.subtract(v0, e2);
    e1.cross(e2, n1);
    d1 = -n1.dot(v0);
    /* plane equation 1: n1.X+d1=0 */

    /*
     * put u0,u1,u2 into plane equation 1 to compute signed distances to the
     * plane
     */
    du0 = n1.dot(u0) + d1;
    du1 = n1.dot(u1) + d1;
    du2 = n1.dot(u2) + d1;

    /* coplanarity robustness check */
    if (FastMath.abs(du0) < EPSILON) du0 = 0.0f;
    if (FastMath.abs(du1) < EPSILON) du1 = 0.0f;
    if (FastMath.abs(du2) < EPSILON) du2 = 0.0f;
    du0du1 = du0 * du1;
    du0du2 = du0 * du2;

    if (du0du1 > 0.0f && du0du2 > 0.0f) {
      return false;
    }

    /* compute plane of triangle (u0,u1,u2) */
    u1.subtract(u0, e1);
    u2.subtract(u0, e2);
    e1.cross(e2, n2);
    d2 = -n2.dot(u0);
    /* plane equation 2: n2.X+d2=0 */

    /* put v0,v1,v2 into plane equation 2 */
    dv0 = n2.dot(v0) + d2;
    dv1 = n2.dot(v1) + d2;
    dv2 = n2.dot(v2) + d2;

    if (FastMath.abs(dv0) < EPSILON) dv0 = 0.0f;
    if (FastMath.abs(dv1) < EPSILON) dv1 = 0.0f;
    if (FastMath.abs(dv2) < EPSILON) dv2 = 0.0f;

    dv0dv1 = dv0 * dv1;
    dv0dv2 = dv0 * dv2;

    if (dv0dv1 > 0.0f && dv0dv2 > 0.0f) {
        /*
         * same sign on all of them + not
         * equal 0 ?
         */
      return false; /* no intersection occurs */
    }

    /* compute direction of intersection line */
    n1.cross(n2, d);

    /* compute and index to the largest component of d */
    max = FastMath.abs(d.x);
    index = 0;
    bb = FastMath.abs(d.y);
    cc = FastMath.abs(d.z);
    if (bb > max) {
      max = bb;
      index = 1;
    }
    if (cc > max) {
      max = cc;
      vp0 = v0.z;
      vp1 = v1.z;
      vp2 = v2.z;

      up0 = u0.z;
      up1 = u1.z;
      up2 = u2.z;

    } else if (index == 1) {
      vp0 = v0.y;
      vp1 = v1.y;
      vp2 = v2.y;

      up0 = u0.y;
      up1 = u1.y;
      up2 = u2.y;
    } else {
      vp0 = v0.x;
      vp1 = v1.x;
      vp2 = v2.x;

      up0 = u0.x;
      up1 = u1.x;
      up2 = u2.x;
    }

    /* compute interval for triangle 1 */
    Vector3f abc = tempVa;
    Vector2f x0x1 = tempV2a;
    if (newComputeIntervals(vp0, vp1, vp2, dv0, dv1, dv2, dv0dv1, dv0dv2, abc, x0x1)) {
      return coplanarTriTri(n1, v0, v1, v2, u0, u1, u2);
    }

    /* compute interval for triangle 2 */
    Vector3f def = tempVb;
    Vector2f y0y1 = tempV2b;
    if (newComputeIntervals(up0, up1, up2, du0, du1, du2, du0du1, du0du2, def, y0y1)) {
      return coplanarTriTri(n1, v0, v1, v2, u0, u1, u2);
    }

    xx = x0x1.x * x0x1.y;
    yy = y0y1.x * y0y1.y;
    xxyy = xx * yy;

    tmp = abc.x * xxyy;
    isect1[0] = tmp + abc.y * x0x1.y * yy;
    isect1[1] = tmp + abc.z * x0x1.x * yy;

    tmp = def.x * xxyy;
    isect2[0] = tmp + def.y * xx * y0y1.y;
    isect2[1] = tmp + def.z * xx * y0y1.x;

    sort(isect1);
    sort(isect2);

    if (isect1[1] < isect2[0] || isect2[1] < isect1[0]) {
      return false;
    }

    return true;
  }