Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/RayCast.h
9912 views
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/Physics/Collision/BackFaceMode.h>
8
9
JPH_NAMESPACE_BEGIN
10
11
/// Structure that holds a single ray cast
12
template <class Vec, class Mat, class RayCastType>
13
struct RayCastT
14
{
15
JPH_OVERRIDE_NEW_DELETE
16
17
/// Constructors
18
RayCastT() = default; // Allow raycast to be created uninitialized
19
RayCastT(typename Vec::ArgType inOrigin, Vec3Arg inDirection) : mOrigin(inOrigin), mDirection(inDirection) { }
20
RayCastT(const RayCastT<Vec, Mat, RayCastType> &) = default;
21
22
/// Transform this ray using inTransform
23
RayCastType Transformed(typename Mat::ArgType inTransform) const
24
{
25
Vec ray_origin = inTransform * mOrigin;
26
Vec3 ray_direction(inTransform * (mOrigin + mDirection) - ray_origin);
27
return { ray_origin, ray_direction };
28
}
29
30
/// Translate ray using inTranslation
31
RayCastType Translated(typename Vec::ArgType inTranslation) const
32
{
33
return { inTranslation + mOrigin, mDirection };
34
}
35
36
/// Get point with fraction inFraction on ray (0 = start of ray, 1 = end of ray)
37
inline Vec GetPointOnRay(float inFraction) const
38
{
39
return mOrigin + inFraction * mDirection;
40
}
41
42
Vec mOrigin; ///< Origin of the ray
43
Vec3 mDirection; ///< Direction and length of the ray (anything beyond this length will not be reported as a hit)
44
};
45
46
struct RayCast : public RayCastT<Vec3, Mat44, RayCast>
47
{
48
using RayCastT<Vec3, Mat44, RayCast>::RayCastT;
49
};
50
51
struct RRayCast : public RayCastT<RVec3, RMat44, RRayCast>
52
{
53
using RayCastT<RVec3, RMat44, RRayCast>::RayCastT;
54
55
/// Convert from RayCast, converts single to double precision
56
explicit RRayCast(const RayCast &inRay) :
57
RRayCast(RVec3(inRay.mOrigin), inRay.mDirection)
58
{
59
}
60
61
/// Convert to RayCast, which implies casting from double precision to single precision
62
explicit operator RayCast() const
63
{
64
return RayCast(Vec3(mOrigin), mDirection);
65
}
66
};
67
68
/// Settings to be passed with a ray cast
69
class RayCastSettings
70
{
71
public:
72
JPH_OVERRIDE_NEW_DELETE
73
74
/// Set the backfacing mode for all shapes
75
void SetBackFaceMode(EBackFaceMode inMode) { mBackFaceModeTriangles = mBackFaceModeConvex = inMode; }
76
77
/// How backfacing triangles should be treated (should we report back facing hits for triangle based shapes, e.g. MeshShape/HeightFieldShape?)
78
EBackFaceMode mBackFaceModeTriangles = EBackFaceMode::IgnoreBackFaces;
79
80
/// How backfacing convex objects should be treated (should we report back facing hits for convex shapes?)
81
EBackFaceMode mBackFaceModeConvex = EBackFaceMode::IgnoreBackFaces;
82
83
/// If convex shapes should be treated as solid. When true, a ray starting inside a convex shape will generate a hit at fraction 0.
84
bool mTreatConvexAsSolid = true;
85
};
86
87
JPH_NAMESPACE_END
88
89