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