RaySphere.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Math/FindRoot.h>
  5. JPH_NAMESPACE_BEGIN
  6. /// Tests a ray starting at inRayOrigin and extending infinitely in inRayDirection against a sphere,
  7. /// @return FLT_MAX if there is no intersection, otherwise the fraction along the ray.
  8. /// @param inRayOrigin Ray origin. If the ray starts inside the sphere, the returned fraction will be 0.
  9. /// @param inRayDirection Ray direction. Does not need to be normalized.
  10. /// @param inSphereCenter Position of the center of the sphere
  11. /// @param inSphereRadius Radius of the sphere
  12. JPH_INLINE float RaySphere(Vec3Arg inRayOrigin, Vec3Arg inRayDirection, Vec3Arg inSphereCenter, float inSphereRadius)
  13. {
  14. // Solve: |RayOrigin + fraction * RayDirection - SphereCenter|^2 = SphereRadius^2 for fraction
  15. Vec3 center_origin = inRayOrigin - inSphereCenter;
  16. float a = inRayDirection.LengthSq();
  17. float b = 2.0f * inRayDirection.Dot(center_origin);
  18. float c = center_origin.LengthSq() - inSphereRadius * inSphereRadius;
  19. float fraction1, fraction2;
  20. if (FindRoot(a, b, c, fraction1, fraction2) == 0)
  21. return c <= 0.0f? 0.0f : FLT_MAX; // Return if origin is inside the sphere
  22. // Sort so that the smallest is first
  23. if (fraction1 > fraction2)
  24. swap(fraction1, fraction2);
  25. // Test solution with lowest fraction, this will be the ray entering the sphere
  26. if (fraction1 >= 0.0f)
  27. return fraction1; // Sphere is before the ray start
  28. // Test solution with highest fraction, this will be the ray leaving the sphere
  29. if (fraction2 >= 0.0f)
  30. return 0.0f; // We start inside the sphere
  31. // No solution
  32. return FLT_MAX;
  33. }
  34. /// Tests a ray starting at inRayOrigin and extending infinitely in inRayDirection against a sphere.
  35. /// Outputs entry and exit points (outMinFraction and outMaxFraction) along the ray (which could be negative if the hit point is before the start of the ray).
  36. /// @param inRayOrigin Ray origin. If the ray starts inside the sphere, the returned fraction will be 0.
  37. /// @param inRayDirection Ray direction. Does not need to be normalized.
  38. /// @param inSphereCenter Position of the center of the sphere.
  39. /// @param inSphereRadius Radius of the sphere.
  40. /// @param outMinFraction Returned lowest intersection fraction
  41. /// @param outMaxFraction Returned highest intersection fraction
  42. /// @return The amount of intersections with the sphere.
  43. /// If 1 intersection is returned outMinFraction will be equal to outMaxFraction
  44. JPH_INLINE int RaySphere(Vec3Arg inRayOrigin, Vec3Arg inRayDirection, Vec3Arg inSphereCenter, float inSphereRadius, float &outMinFraction, float &outMaxFraction)
  45. {
  46. // Solve: |RayOrigin + fraction * RayDirection - SphereCenter|^2 = SphereRadius^2 for fraction
  47. Vec3 center_origin = inRayOrigin - inSphereCenter;
  48. float a = inRayDirection.LengthSq();
  49. float b = 2.0f * inRayDirection.Dot(center_origin);
  50. float c = center_origin.LengthSq() - inSphereRadius * inSphereRadius;
  51. float fraction1, fraction2;
  52. switch (FindRoot(a, b, c, fraction1, fraction2))
  53. {
  54. case 0:
  55. if (c <= 0.0f)
  56. {
  57. // Origin inside sphere
  58. outMinFraction = outMaxFraction = 0.0f;
  59. return 1;
  60. }
  61. else
  62. {
  63. // Origin outside of the sphere
  64. return 0;
  65. }
  66. break;
  67. case 1:
  68. // Ray is touching the sphere
  69. outMinFraction = outMaxFraction = fraction1;
  70. return 1;
  71. default:
  72. // Ray enters and exits the sphere
  73. // Sort so that the smallest is first
  74. if (fraction1 > fraction2)
  75. swap(fraction1, fraction2);
  76. outMinFraction = fraction1;
  77. outMaxFraction = fraction2;
  78. return 2;
  79. }
  80. }
  81. JPH_NAMESPACE_END