Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
RaySphere.h
Go to the documentation of this file.
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
8
10
17JPH_INLINE float RaySphere(Vec3Arg inRayOrigin, Vec3Arg inRayDirection, Vec3Arg inSphereCenter, float inSphereRadius)
18{
19 // Solve: |RayOrigin + fraction * RayDirection - SphereCenter|^2 = SphereRadius^2 for fraction
20 Vec3 center_origin = inRayOrigin - inSphereCenter;
21 float a = inRayDirection.LengthSq();
22 float b = 2.0f * inRayDirection.Dot(center_origin);
23 float c = center_origin.LengthSq() - inSphereRadius * inSphereRadius;
24 float fraction1, fraction2;
25 if (FindRoot(a, b, c, fraction1, fraction2) == 0)
26 return c <= 0.0f? 0.0f : FLT_MAX; // Return if origin is inside the sphere
27
28 // Sort so that the smallest is first
29 if (fraction1 > fraction2)
30 swap(fraction1, fraction2);
31
32 // Test solution with lowest fraction, this will be the ray entering the sphere
33 if (fraction1 >= 0.0f)
34 return fraction1; // Sphere is before the ray start
35
36 // Test solution with highest fraction, this will be the ray leaving the sphere
37 if (fraction2 >= 0.0f)
38 return 0.0f; // We start inside the sphere
39
40 // No solution
41 return FLT_MAX;
42}
43
54JPH_INLINE int RaySphere(Vec3Arg inRayOrigin, Vec3Arg inRayDirection, Vec3Arg inSphereCenter, float inSphereRadius, float &outMinFraction, float &outMaxFraction)
55{
56 // Solve: |RayOrigin + fraction * RayDirection - SphereCenter|^2 = SphereRadius^2 for fraction
57 Vec3 center_origin = inRayOrigin - inSphereCenter;
58 float a = inRayDirection.LengthSq();
59 float b = 2.0f * inRayDirection.Dot(center_origin);
60 float c = center_origin.LengthSq() - inSphereRadius * inSphereRadius;
61 float fraction1, fraction2;
62 switch (FindRoot(a, b, c, fraction1, fraction2))
63 {
64 case 0:
65 if (c <= 0.0f)
66 {
67 // Origin inside sphere
68 outMinFraction = outMaxFraction = 0.0f;
69 return 1;
70 }
71 else
72 {
73 // Origin outside of the sphere
74 return 0;
75 }
76 break;
77
78 case 1:
79 // Ray is touching the sphere
80 outMinFraction = outMaxFraction = fraction1;
81 return 1;
82
83 default:
84 // Ray enters and exits the sphere
85
86 // Sort so that the smallest is first
87 if (fraction1 > fraction2)
88 swap(fraction1, fraction2);
89
90 outMinFraction = fraction1;
91 outMaxFraction = fraction2;
92 return 2;
93 }
94}
95
#define JPH_NAMESPACE_END
Definition: Core.h:354
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:348
JPH_NAMESPACE_BEGIN int FindRoot(const T inA, const T inB, const T inC, T &outX1, T &outX2)
Definition: FindRoot.h:13
JPH_NAMESPACE_BEGIN JPH_INLINE float RaySphere(Vec3Arg inRayOrigin, Vec3Arg inRayDirection, Vec3Arg inSphereCenter, float inSphereRadius)
Definition: RaySphere.h:17
Definition: Vec3.h:16
JPH_INLINE float Dot(Vec3Arg inV2) const
Dot product.
Definition: Vec3.inl:637
JPH_INLINE float LengthSq() const
Squared length of vector.
Definition: Vec3.inl:653