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;  



float c = sphereOrigin.length();


float c = Q.length();
  • \$\begingroup\$ Yup, that was it, I made a change and forgot it required subsequent changes. \$\endgroup\$ Mar 10 '14 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
    May 10 '16 at 20:16
  • \$\begingroup\$ I don't understand. Do you have a problem with OPs code or with my fix? \$\endgroup\$ May 12 '16 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.