RaySphere.h 3.5 KB

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