Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/BroadPhase/BroadPhaseLayerInterfaceMask.h
9918 views
1
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2
// SPDX-FileCopyrightText: 2023 Jorrit Rouwe
3
// SPDX-License-Identifier: MIT
4
5
#pragma once
6
7
#include <Jolt/Physics/Collision/BroadPhase/BroadPhaseLayer.h>
8
#include <Jolt/Physics/Collision/ObjectLayerPairFilterMask.h>
9
10
JPH_NAMESPACE_BEGIN
11
12
/// BroadPhaseLayerInterface implementation.
13
/// This defines a mapping between object and broadphase layers.
14
/// This implementation works together with ObjectLayerPairFilterMask and ObjectVsBroadPhaseLayerFilterMask.
15
/// A broadphase layer is suitable for an object if its group & inGroupsToInclude is not zero and its group & inGroupsToExclude is zero.
16
/// The broadphase layers are iterated from lowest to highest value and the first one that matches is taken. If none match then it takes the last layer.
17
class BroadPhaseLayerInterfaceMask : public BroadPhaseLayerInterface
18
{
19
public:
20
JPH_OVERRIDE_NEW_DELETE
21
22
explicit BroadPhaseLayerInterfaceMask(uint inNumBroadPhaseLayers)
23
{
24
JPH_ASSERT(inNumBroadPhaseLayers > 0);
25
mMapping.resize(inNumBroadPhaseLayers);
26
27
#if defined(JPH_EXTERNAL_PROFILE) || defined(JPH_PROFILE_ENABLED)
28
mBroadPhaseLayerNames.resize(inNumBroadPhaseLayers, "Undefined");
29
#endif // JPH_EXTERNAL_PROFILE || JPH_PROFILE_ENABLED
30
}
31
32
// Configures a broadphase layer.
33
void ConfigureLayer(BroadPhaseLayer inBroadPhaseLayer, uint32 inGroupsToInclude, uint32 inGroupsToExclude)
34
{
35
JPH_ASSERT((BroadPhaseLayer::Type)inBroadPhaseLayer < (uint)mMapping.size());
36
Mapping &m = mMapping[(BroadPhaseLayer::Type)inBroadPhaseLayer];
37
m.mGroupsToInclude = inGroupsToInclude;
38
m.mGroupsToExclude = inGroupsToExclude;
39
}
40
41
virtual uint GetNumBroadPhaseLayers() const override
42
{
43
return (uint)mMapping.size();
44
}
45
46
virtual BroadPhaseLayer GetBroadPhaseLayer(ObjectLayer inLayer) const override
47
{
48
// Try to find the first broadphase layer that matches
49
uint32 group = ObjectLayerPairFilterMask::sGetGroup(inLayer);
50
for (const Mapping &m : mMapping)
51
if ((group & m.mGroupsToInclude) != 0 && (group & m.mGroupsToExclude) == 0)
52
return BroadPhaseLayer(BroadPhaseLayer::Type(&m - mMapping.data()));
53
54
// Fall back to the last broadphase layer
55
return BroadPhaseLayer(BroadPhaseLayer::Type(mMapping.size() - 1));
56
}
57
58
/// Returns true if an object layer should collide with a broadphase layer, this function is being called from ObjectVsBroadPhaseLayerFilterMask
59
inline bool ShouldCollide(ObjectLayer inLayer1, BroadPhaseLayer inLayer2) const
60
{
61
uint32 mask = ObjectLayerPairFilterMask::sGetMask(inLayer1);
62
const Mapping &m = mMapping[(BroadPhaseLayer::Type)inLayer2];
63
return &m == &mMapping.back() // Last layer may collide with anything
64
|| (m.mGroupsToInclude & mask) != 0; // Mask allows it to collide with objects that could reside in this layer
65
}
66
67
#if defined(JPH_EXTERNAL_PROFILE) || defined(JPH_PROFILE_ENABLED)
68
void SetBroadPhaseLayerName(BroadPhaseLayer inLayer, const char *inName)
69
{
70
mBroadPhaseLayerNames[(BroadPhaseLayer::Type)inLayer] = inName;
71
}
72
73
virtual const char * GetBroadPhaseLayerName(BroadPhaseLayer inLayer) const override
74
{
75
return mBroadPhaseLayerNames[(BroadPhaseLayer::Type)inLayer];
76
}
77
#endif // JPH_EXTERNAL_PROFILE || JPH_PROFILE_ENABLED
78
79
private:
80
struct Mapping
81
{
82
uint32 mGroupsToInclude = 0;
83
uint32 mGroupsToExclude = ~uint32(0);
84
};
85
Array<Mapping> mMapping;
86
87
#if defined(JPH_EXTERNAL_PROFILE) || defined(JPH_PROFILE_ENABLED)
88
Array<const char *> mBroadPhaseLayerNames;
89
#endif // JPH_EXTERNAL_PROFILE || JPH_PROFILE_ENABLED
90
};
91
92
JPH_NAMESPACE_END
93
94