Debian Codegraph @

thirdparty/jolt_physics/Jolt/Geometry/RayCylinder.h

raw symbols SCIP
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/Math/FindRoot.h>
8
9 JPH_NAMESPACE_BEGIN
10
11 /// Tests a ray starting at inRayOrigin and extending infinitely in inRayDirection
12 /// against an infinite cylinder centered along the Y axis
13 /// @return FLT_MAX if there is no intersection, otherwise the fraction along the ray.
14 /// @param inRayDirection Direction of the ray. Does not need to be normalized.
15 /// @param inRayOrigin Origin of the ray. If the ray starts inside the cylinder, the returned fraction will be 0.
16 /// @param inCylinderRadius Radius of the infinite cylinder
17 JPH_INLINE float RayCylinder(Vec3Arg inRayOrigin, Vec3Arg inRayDirection, float inCylinderRadius)
18 {
19 // Remove Y component of ray to see of ray intersects with infinite cylinder
20 UVec4 mask_y = UVec4(0, 0xffffffff, 0, 0);
21 Vec3 origin_xz = Vec3::sSelect(inRayOrigin, Vec3::sZero(), mask_y);
22 float origin_xz_len_sq = origin_xz.LengthSq();
23 float r_sq = Square(inCylinderRadius);
24 if (origin_xz_len_sq > r_sq)
25 {
26 // Ray starts outside of the infinite cylinder
27 // Solve: |RayOrigin_xz + fraction * RayDirection_xz|^2 = r^2 to find fraction
28 Vec3 direction_xz = Vec3::sSelect(inRayDirection, Vec3::sZero(), mask_y);
29 float a = direction_xz.LengthSq();
30 float b = 2.0f * origin_xz.Dot(direction_xz);
31 float c = origin_xz_len_sq - r_sq;
32 float fraction1, fraction2;
33 if (FindRoot(a, b, c, fraction1, fraction2) == 0)
34 return FLT_MAX; // No intersection with infinite cylinder
35
36 // Get fraction corresponding to the ray entering the circle
37 float fraction = min(fraction1, fraction2);
38 if (fraction >= 0.0f)
39 return fraction;
40 }
41 else
42 {
43 // Ray starts inside the infinite cylinder
44 return 0.0f;
45 }
46
47 // No collision
48 return FLT_MAX;
49 }
50
51 /// Test a ray against a cylinder centered around the origin with its axis along the Y axis and half height specified.
52 /// @return FLT_MAX if there is no intersection, otherwise the fraction along the ray.
53 /// @param inRayDirection Ray direction. Does not need to be normalized.
54 /// @param inRayOrigin Origin of the ray. If the ray starts inside the cylinder, the returned fraction will be 0.
55 /// @param inCylinderRadius Radius of the cylinder
56 /// @param inCylinderHalfHeight Distance from the origin to the top (or bottom) of the cylinder
57 JPH_INLINE float RayCylinder(Vec3Arg inRayOrigin, Vec3Arg inRayDirection, float inCylinderHalfHeight, float inCylinderRadius)
58 {
59 // Test infinite cylinder
60 float fraction = RayCylinder(inRayOrigin, inRayDirection, inCylinderRadius);
61 if (fraction == FLT_MAX)
62 return FLT_MAX;
63
64 // If this hit is in the finite cylinder we have our fraction
65 if (abs(inRayOrigin.GetY() + fraction * inRayDirection.GetY()) <= inCylinderHalfHeight)
66 return fraction;
67
68 // Check if ray could hit the top or bottom plane of the cylinder
69 float direction_y = inRayDirection.GetY();
70 if (direction_y != 0.0f)
71 {
72 // Solving line equation: x = ray_origin + fraction * ray_direction
73 // and plane equation: plane_normal . x + plane_constant = 0
74 // fraction = (-plane_constant - plane_normal . ray_origin) / (plane_normal . ray_direction)
75 // when the ray_direction.y < 0:
76 // plane_constant = -cylinder_half_height, plane_normal = (0, 1, 0)
77 // else
78 // plane_constant = -cylinder_half_height, plane_normal = (0, -1, 0)
79 float origin_y = inRayOrigin.GetY();
80 float plane_fraction;
81 if (direction_y < 0.0f)
82 plane_fraction = (inCylinderHalfHeight - origin_y) / direction_y;
83 else
84 plane_fraction = -(inCylinderHalfHeight + origin_y) / direction_y;
85
86 // Check if the hit is in front of the ray
87 if (plane_fraction >= 0.0f)
88 {
89 // Test if this hit is inside the cylinder
90 Vec3 point = inRayOrigin + plane_fraction * inRayDirection;
91 float dist_sq = Square(point.GetX()) + Square(point.GetZ());
92 if (dist_sq <= Square(inCylinderRadius))
93 return plane_fraction;
94 }
95 }
96
97 // No collision
98 return FLT_MAX;
99 }
100
101 JPH_NAMESPACE_END