2
0

RayCapsule.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Geometry/RayCylinder.h>
  5. #include <Jolt/Geometry/RaySphere.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// Tests a ray starting at inRayOrigin and extending infinitely in inRayDirection
  8. /// against a capsule centered around the origin with its axis along the Y axis and half height specified.
  9. /// @return FLT_MAX if there is no intersection, otherwise the fraction along the ray.
  10. /// @param inRayDirection Ray direction. Does not need to be normalized.
  11. /// @param inRayOrigin Origin of the ray. If the ray starts inside the capsule, the returned fraction will be 0.
  12. /// @param inCapsuleHalfHeight Distance from the origin to the center of the top sphere (or that of the bottom)
  13. /// @param inCapsuleRadius Radius of the top/bottom sphere
  14. JPH_INLINE float RayCapsule(Vec3Arg inRayOrigin, Vec3Arg inRayDirection, float inCapsuleHalfHeight, float inCapsuleRadius)
  15. {
  16. // Test infinite cylinder
  17. float cylinder = RayCylinder(inRayOrigin, inRayDirection, inCapsuleRadius);
  18. if (cylinder == FLT_MAX)
  19. return FLT_MAX;
  20. // If this hit is in the finite cylinder we have our fraction
  21. if (abs(inRayOrigin.GetY() + cylinder * inRayDirection.GetY()) <= inCapsuleHalfHeight)
  22. return cylinder;
  23. // Test upper and lower sphere
  24. Vec3 sphere_center(0, inCapsuleHalfHeight, 0);
  25. float upper = RaySphere(inRayOrigin, inRayDirection, sphere_center, inCapsuleRadius);
  26. float lower = RaySphere(inRayOrigin, inRayDirection, -sphere_center, inCapsuleRadius);
  27. return min(upper, lower);
  28. }
  29. JPH_NAMESPACE_END