}
@Override
public double getHitPointDistance(Ray r) {
double dist;
Ray subRay;
Vect3 tmp = new Vect3();
Vect.add(position, model.getBoundingSphere().getPosition(), tmp);
if(RaySphere.isRayOriginatingInSphere(r.getOrigin(), r.getDirection(), tmp, model.getBoundingSphere().getRadius())){
Vect.subtract(r.getOrigin(), position, tmp);
subRay = new Ray(tmp, r.getDirection());
dist = 0;
}else{
dist=RaySphere.getHitPointRaySphereDistance(r.getOrigin(), r.getDirection(), tmp, model.getBoundingSphere().getRadius());
if(Double.isInfinite(dist))
return dist;
Vect.addMultiple(r.getOrigin(), r.getDirection(), dist, tmp);
Vect.subtract(tmp, position, tmp);
subRay = new Ray(tmp, r.getDirection());
}
CollisionData d = new CollisionData();
lastCollision.put(Thread.currentThread(), d);
d.d = RayPath.getFirstCollision(model.getTree(), subRay);
if(!Double.isInfinite(d.d.d)){
Vect3 hitPointLocal = new Vect3();
Vect.addMultiple(subRay.getOrigin(), subRay.getDirection(), d.d.d, hitPointLocal);
d.hitPointLocal = hitPointLocal;
Vect3 hitPointGlobal = new Vect3();
Vect.add(hitPointLocal, position, hitPointGlobal);
d.hitPointGlobal = hitPointGlobal;
return d.d.d + dist;