I have this java function that should return a true or false for any sphere passed to it if they intersect. However it seems to end being true a lot of the time when it shouldn't and false sometimes when it should be true.

It is possible however that i am passing incorrect data to the function. This was done with jPCT so the object SimpleVector is a vector with 3 variables.

public Boolean sphereIntersect(SimpleVector rayOrigin,SimpleVector rayDirection, SimpleVector sphereOrigin, float sphereRadius )
        SimpleVector Q = new SimpleVector(sphereOrigin);

          float c = sphereOrigin.length();
          float v = Q.calcDot(rayDirection);
          float d = sphereRadius*sphereRadius - (c*c - v*v);

          if (d < 0.0){
          return false;
          return true;  


1 Answer 1



float c = sphereOrigin.length();


float c = Q.length();
  • \$\begingroup\$ Yup, that was it, I made a change and forgot it required subsequent changes. \$\endgroup\$ Commented Mar 10, 2014 at 19:01
  • \$\begingroup\$ Hey, I found this question very useful but I cannot seem to be able to properly use OP's function: "sphereIntersect(a, b.calcSub(a); new SimpleVector(0, 0, 0), MY_RADIUS);" Any pointers? \$\endgroup\$
    – N3sh
    Commented May 10, 2016 at 20:16
  • \$\begingroup\$ I don't understand. Do you have a problem with OPs code or with my fix? \$\endgroup\$ Commented May 12, 2016 at 15:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .