/*
   * (non-Javadoc)
   * @see com.jme.bounding.BoundingVolume#intersectsWhere(com.jme.math.Ray)
   */
  public int collideWithRay(Ray ray, CollisionResults results) {
    Vector3f vect1 = Vector3f.newInstance();
    Vector3f diff = vect1.set(ray.getOrigin()).subtractLocal(center);
    float a = diff.dot(diff) - (getRadius() * getRadius());
    float a1, discr, root;
    if (a <= 0.0) {
      // inside sphere
      a1 = ray.direction.dot(diff);
      discr = (a1 * a1) - a;
      root = FastMath.sqrt(discr);

      float distance = root - a1;
      Vector3f point = new Vector3f(ray.direction).multLocal(distance).addLocal(ray.origin);

      CollisionResult result = new CollisionResult(point, distance);
      results.addCollision(result);
      Vector3f.recycle(vect1);
      return 1;
    }

    a1 = ray.direction.dot(diff);
    if (a1 >= 0.0) {
      Vector3f.recycle(vect1);
      return 0;
    }

    discr = a1 * a1 - a;
    if (discr < 0.0) {
      Vector3f.recycle(vect1);
      return 0;
    } else if (discr >= FastMath.ZERO_TOLERANCE) {
      root = FastMath.sqrt(discr);
      float dist = -a1 - root;
      Vector3f point = new Vector3f(ray.direction).multLocal(dist).addLocal(ray.origin);
      results.addCollision(new CollisionResult(point, dist));

      dist = -a1 + root;
      point = new Vector3f(ray.direction).multLocal(dist).addLocal(ray.origin);
      results.addCollision(new CollisionResult(point, dist));
      Vector3f.recycle(vect1);
      return 2;
    } else {
      float dist = -a1;
      Vector3f point = new Vector3f(ray.direction).multLocal(dist).addLocal(ray.origin);
      results.addCollision(new CollisionResult(point, dist));
      Vector3f.recycle(vect1);
      return 1;
    }
  }
  // public boolean intersectsOrientedBoundingBox(OrientedBoundingBox obb) {
  // return obb.intersectsSphere(this);
  // }
  public boolean intersects(Ray ray) {
    assert Vector3f.isValidVector(center);

    Vector3f vect1 = Vector3f.newInstance();
    Vector3f diff = vect1.set(ray.getOrigin()).subtractLocal(center);
    float radiusSquared = getRadius() * getRadius();
    float a = diff.dot(diff) - radiusSquared;
    if (a <= 0.0) {
      // in sphere
      Vector3f.recycle(vect1);
      return true;
    }

    // outside sphere
    float b = ray.getDirection().dot(diff);
    if (b >= 0.0) {
      Vector3f.recycle(vect1);
      return false;
    }
    Vector3f.recycle(vect1);
    return b * b >= a;
  }