Path: blob/master/thirdparty/jolt_physics/Jolt/Geometry/RayCapsule.h
9913 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Geometry/RayCylinder.h>7#include <Jolt/Geometry/RaySphere.h>89JPH_NAMESPACE_BEGIN1011/// Tests a ray starting at inRayOrigin and extending infinitely in inRayDirection12/// against a capsule centered around the origin with its axis along the Y axis and half height specified.13/// @return FLT_MAX if there is no intersection, otherwise the fraction along the ray.14/// @param inRayDirection Ray direction. Does not need to be normalized.15/// @param inRayOrigin Origin of the ray. If the ray starts inside the capsule, the returned fraction will be 0.16/// @param inCapsuleHalfHeight Distance from the origin to the center of the top sphere (or that of the bottom)17/// @param inCapsuleRadius Radius of the top/bottom sphere18JPH_INLINE float RayCapsule(Vec3Arg inRayOrigin, Vec3Arg inRayDirection, float inCapsuleHalfHeight, float inCapsuleRadius)19{20// Test infinite cylinder21float cylinder = RayCylinder(inRayOrigin, inRayDirection, inCapsuleRadius);22if (cylinder == FLT_MAX)23return FLT_MAX;2425// If this hit is in the finite cylinder we have our fraction26if (abs(inRayOrigin.GetY() + cylinder * inRayDirection.GetY()) <= inCapsuleHalfHeight)27return cylinder;2829// Test upper and lower sphere30Vec3 sphere_center(0, inCapsuleHalfHeight, 0);31float upper = RaySphere(inRayOrigin, inRayDirection, sphere_center, inCapsuleRadius);32float lower = RaySphere(inRayOrigin, inRayDirection, -sphere_center, inCapsuleRadius);33return min(upper, lower);34}3536JPH_NAMESPACE_END373839