Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/BroadPhase/BroadPhaseLayer.h
9918 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/Core/NonCopyable.h>
8
#include <Jolt/Physics/Collision/ObjectLayer.h>
9
10
JPH_NAMESPACE_BEGIN
11
12
/// An object layer can be mapped to a broadphase layer. Objects with the same broadphase layer will end up in the same sub structure (usually a tree) of the broadphase.
13
/// When there are many layers, this reduces the total amount of sub structures the broad phase needs to manage. Usually you want objects that don't collide with each other
14
/// in different broad phase layers, but there could be exceptions if objects layers only contain a minor amount of objects so it is not beneficial to give each layer its
15
/// own sub structure in the broadphase.
16
/// Note: This class requires explicit casting from and to Type to avoid confusion with ObjectLayer
17
class BroadPhaseLayer
18
{
19
public:
20
using Type = uint8;
21
22
JPH_INLINE BroadPhaseLayer() = default;
23
JPH_INLINE explicit constexpr BroadPhaseLayer(Type inValue) : mValue(inValue) { }
24
JPH_INLINE constexpr BroadPhaseLayer(const BroadPhaseLayer &) = default;
25
JPH_INLINE BroadPhaseLayer & operator = (const BroadPhaseLayer &) = default;
26
27
JPH_INLINE constexpr bool operator == (const BroadPhaseLayer &inRHS) const
28
{
29
return mValue == inRHS.mValue;
30
}
31
32
JPH_INLINE constexpr bool operator != (const BroadPhaseLayer &inRHS) const
33
{
34
return mValue != inRHS.mValue;
35
}
36
37
JPH_INLINE constexpr bool operator < (const BroadPhaseLayer &inRHS) const
38
{
39
return mValue < inRHS.mValue;
40
}
41
42
JPH_INLINE explicit constexpr operator Type() const
43
{
44
return mValue;
45
}
46
47
JPH_INLINE Type GetValue() const
48
{
49
return mValue;
50
}
51
52
private:
53
Type mValue;
54
};
55
56
/// Constant value used to indicate an invalid broad phase layer
57
static constexpr BroadPhaseLayer cBroadPhaseLayerInvalid(0xff);
58
59
/// Interface that the application should implement to allow mapping object layers to broadphase layers
60
class JPH_EXPORT BroadPhaseLayerInterface : public NonCopyable
61
{
62
public:
63
/// Destructor
64
virtual ~BroadPhaseLayerInterface() = default;
65
66
/// Return the number of broadphase layers there are
67
virtual uint GetNumBroadPhaseLayers() const = 0;
68
69
/// Convert an object layer to the corresponding broadphase layer
70
virtual BroadPhaseLayer GetBroadPhaseLayer(ObjectLayer inLayer) const = 0;
71
72
#if defined(JPH_EXTERNAL_PROFILE) || defined(JPH_PROFILE_ENABLED)
73
/// Get the user readable name of a broadphase layer (debugging purposes)
74
virtual const char * GetBroadPhaseLayerName(BroadPhaseLayer inLayer) const = 0;
75
#endif // JPH_EXTERNAL_PROFILE || JPH_PROFILE_ENABLED
76
};
77
78
/// Class to test if an object can collide with a broadphase layer. Used while finding collision pairs.
79
class JPH_EXPORT ObjectVsBroadPhaseLayerFilter : public NonCopyable
80
{
81
public:
82
/// Destructor
83
virtual ~ObjectVsBroadPhaseLayerFilter() = default;
84
85
/// Returns true if an object layer should collide with a broadphase layer
86
virtual bool ShouldCollide([[maybe_unused]] ObjectLayer inLayer1, [[maybe_unused]] BroadPhaseLayer inLayer2) const
87
{
88
return true;
89
}
90
};
91
92
/// Filter class for broadphase layers
93
class JPH_EXPORT BroadPhaseLayerFilter : public NonCopyable
94
{
95
public:
96
/// Destructor
97
virtual ~BroadPhaseLayerFilter() = default;
98
99
/// Function to filter out broadphase layers when doing collision query test (return true to allow testing against objects with this layer)
100
virtual bool ShouldCollide([[maybe_unused]] BroadPhaseLayer inLayer) const
101
{
102
return true;
103
}
104
};
105
106
/// Default filter class that uses the pair filter in combination with a specified layer to filter layers
107
class JPH_EXPORT DefaultBroadPhaseLayerFilter : public BroadPhaseLayerFilter
108
{
109
public:
110
/// Constructor
111
DefaultBroadPhaseLayerFilter(const ObjectVsBroadPhaseLayerFilter &inObjectVsBroadPhaseLayerFilter, ObjectLayer inLayer) :
112
mObjectVsBroadPhaseLayerFilter(inObjectVsBroadPhaseLayerFilter),
113
mLayer(inLayer)
114
{
115
}
116
117
// See BroadPhaseLayerFilter::ShouldCollide
118
virtual bool ShouldCollide(BroadPhaseLayer inLayer) const override
119
{
120
return mObjectVsBroadPhaseLayerFilter.ShouldCollide(mLayer, inLayer);
121
}
122
123
private:
124
const ObjectVsBroadPhaseLayerFilter &mObjectVsBroadPhaseLayerFilter;
125
ObjectLayer mLayer;
126
};
127
128
/// Allows objects from a specific broad phase layer only
129
class JPH_EXPORT SpecifiedBroadPhaseLayerFilter : public BroadPhaseLayerFilter
130
{
131
public:
132
/// Constructor
133
explicit SpecifiedBroadPhaseLayerFilter(BroadPhaseLayer inLayer) :
134
mLayer(inLayer)
135
{
136
}
137
138
// See BroadPhaseLayerFilter::ShouldCollide
139
virtual bool ShouldCollide(BroadPhaseLayer inLayer) const override
140
{
141
return mLayer == inLayer;
142
}
143
144
private:
145
BroadPhaseLayer mLayer;
146
};
147
148
JPH_NAMESPACE_END
149
150