예제 #1
0
  /**
   * Gets the point of collision between a BoundingSphere and a Ray.
   *
   * @param a
   * @param b
   * @return
   */
  public static Vector3 getCollision(BoundingSphere a, Ray b) {
    Vector3 m = b.origin.subtract(a.center);
    float e = m.dot(b.direction);
    float f = (float) (m.dot(m) - a.radius * a.radius);

    // Exit if r’s origin outside s (c > 0) and r pointing away from s (b > 0)
    if (f > 0.0f && e > 0.0f) {
      return null;
    }
    float discr = e * e - f;

    // A negative discriminant corresponds to ray missing sphere
    if (discr < 0.0f) {
      return null;
    }

    // Ray now found to intersect sphere, compute smallest t value of intersection
    float t = (float) (-e - MathHelper.sqrt(discr));

    // If t is negative, ray started inside sphere so clamp t to zero
    if (t < 0.0f) {
      t = 0.0f;
    }
    return b.origin.add(b.direction.multiply(t));
  }
예제 #2
0
  /**
   * Checks collision between a BoundingSphere and a Ray.
   *
   * @param a
   * @param b
   * @return
   */
  public static boolean checkCollision(BoundingSphere a, Ray b) {
    Vector3 m = b.origin.subtract(a.center);
    float e = m.dot(b.direction);
    float f = (float) (m.dot(m) - a.radius * a.radius);

    // Exit if r’s origin outside s (c > 0) and r pointing away from s (b > 0)
    if (f > 0.0f && e > 0.0f) {
      return false;
    }
    float discr = e * e - f;

    // A negative discriminant corresponds to ray missing sphere
    if (discr < 0.0f) {
      return false;
    }

    // Ray now found to intersect sphere
    return true;
  }
예제 #3
0
  /**
   * Checks the collision between a BoundingSphere and a Segment.
   *
   * @param a
   * @param b
   * @return
   */
  public static boolean checkCollision(BoundingSphere a, Segment b) {
    Vector3 m = b.origin.subtract(a.center);
    Vector3 l = b.endpoint.subtract(b.origin);
    float lnorm = l.fastLength();
    Vector3 d = l.multiply(1f / lnorm);

    float e = m.dot(d);
    float f = (float) (m.dot(m) - a.radius * a.radius);

    // Exit if r’s origin outside s (c > 0) and r pointing away from s (b > 0)
    if (f > 0.0f && e > 0.0f) {
      return false;
    }
    float discr = e * e - f;

    // A negative discriminant corresponds to ray missing sphere
    if (discr < 0.0f) {
      return false;
    }

    // Check that the intersection is not past the segment
    return -e - MathHelper.sqrt(discr) <= lnorm;
  }