mSphere.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "math/mSphere.h"
  24. #include "math/mMatrix.h"
  25. bool SphereF::intersectsRay( const Point3F &start, const Point3F &end ) const
  26. {
  27. MatrixF worldToObj( true );
  28. worldToObj.setPosition( center );
  29. worldToObj.inverse();
  30. VectorF dir = end - start;
  31. dir.normalize();
  32. Point3F tmpStart = start;
  33. worldToObj.mulP( tmpStart );
  34. //Compute A, B and C coefficients
  35. F32 a = mDot(dir, dir);
  36. F32 b = 2 * mDot(dir, tmpStart);
  37. F32 c = mDot(tmpStart, tmpStart) - (radius * radius);
  38. //Find discriminant
  39. F32 disc = b * b - 4 * a * c;
  40. // if discriminant is negative there are no real roots, so return
  41. // false as ray misses sphere
  42. if ( disc < 0 )
  43. return false;
  44. // compute q as described above
  45. F32 distSqrt = mSqrt( disc );
  46. F32 q;
  47. if ( b < 0 )
  48. q = (-b - distSqrt)/2.0;
  49. else
  50. q = (-b + distSqrt)/2.0;
  51. // compute t0 and t1
  52. F32 t0 = q / a;
  53. F32 t1 = c / q;
  54. // make sure t0 is smaller than t1
  55. if ( t0 > t1 )
  56. {
  57. // if t0 is bigger than t1 swap them around
  58. F32 temp = t0;
  59. t0 = t1;
  60. t1 = temp;
  61. }
  62. // This function doesn't use it
  63. // but t would be the interpolant
  64. // value for getting the exact
  65. // intersection point, by interpolating
  66. // start to end by t.
  67. /*
  68. F32 t = 0;
  69. TORQUE_UNUSED(t);
  70. */
  71. // if t1 is less than zero, the object is in the ray's negative direction
  72. // and consequently the ray misses the sphere
  73. if ( t1 < 0 )
  74. return false;
  75. // if t0 is less than zero, the intersection point is at t1
  76. if ( t0 < 0 ) // t = t1;
  77. return true;
  78. else // else the intersection point is at t0
  79. return true; // t = t0;
  80. }