Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
GroupFilterTable.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
9
11
36{
38
39private:
40 using GroupID = CollisionGroup::GroupID;
41 using SubGroupID = CollisionGroup::SubGroupID;
42
44 int GetBit(SubGroupID inSubGroup1, SubGroupID inSubGroup2) const
45 {
46 JPH_ASSERT(inSubGroup1 != inSubGroup2);
47
48 // We store the lower left half only, so swap the inputs when trying to access the top right half
49 if (inSubGroup1 > inSubGroup2)
50 std::swap(inSubGroup1, inSubGroup2);
51
52 JPH_ASSERT(inSubGroup2 < mNumSubGroups);
53
54 // Calculate at which bit the entry for this pair resides
55 // We use the fact that a row always starts at inSubGroup2 * (inSubGroup2 - 1) / 2
56 // (this is the amount of bits needed to store a table of inSubGroup2 entries)
57 return (inSubGroup2 * (inSubGroup2 - 1)) / 2 + inSubGroup1;
58 }
59
60public:
62 explicit GroupFilterTable(uint inNumSubGroups = 0) :
63 mNumSubGroups(inNumSubGroups)
64 {
65 // By default everything collides
66 int table_size = ((inNumSubGroups * (inNumSubGroups - 1)) / 2 + 7) / 8;
67 mTable.resize(table_size, 0xff);
68 }
69
71 GroupFilterTable(const GroupFilterTable &inRHS) : mNumSubGroups(inRHS.mNumSubGroups), mTable(inRHS.mTable) { }
72
74 void DisableCollision(SubGroupID inSubGroup1, SubGroupID inSubGroup2)
75 {
76 int bit = GetBit(inSubGroup1, inSubGroup2);
77 mTable[bit >> 3] &= (0xff ^ (1 << (bit & 0b111)));
78 }
79
81 void EnableCollision(SubGroupID inSubGroup1, SubGroupID inSubGroup2)
82 {
83 int bit = GetBit(inSubGroup1, inSubGroup2);
84 mTable[bit >> 3] |= 1 << (bit & 0b111);
85 }
86
88 inline bool IsCollisionEnabled(SubGroupID inSubGroup1, SubGroupID inSubGroup2) const
89 {
90 // Test if the bit is set for this group pair
91 int bit = GetBit(inSubGroup1, inSubGroup2);
92 return (mTable[bit >> 3] & (1 << (bit & 0b111))) != 0;
93 }
94
96 virtual bool CanCollide(const CollisionGroup &inGroup1, const CollisionGroup &inGroup2) const override
97 {
98 // If one of the groups is cInvalidGroup the objects will collide (note that the if following this if will ensure that group2 is not cInvalidGroup)
100 return true;
101
102 // If the objects are in different groups, they collide
103 if (inGroup1.GetGroupID() != inGroup2.GetGroupID())
104 return true;
105
106 // If the collision filters do not match, but they're in the same group we ignore the collision
107 if (inGroup1.GetGroupFilter() != inGroup2.GetGroupFilter())
108 return false;
109
110 // If they are in the same sub group, they don't collide
111 if (inGroup1.GetSubGroupID() == inGroup2.GetSubGroupID())
112 return false;
113
114 // Check the bit table
115 return IsCollisionEnabled(inGroup1.GetSubGroupID(), inGroup2.GetSubGroupID());
116 }
117
118 // See: GroupFilter::SaveBinaryState
119 virtual void SaveBinaryState(StreamOut &inStream) const override;
120
121protected:
122 // See: GroupFilter::RestoreBinaryState
123 virtual void RestoreBinaryState(StreamIn &inStream) override;
124
125private:
126 uint mNumSubGroups;
127 Array<uint8> mTable;
128};
129
#define JPH_EXPORT
Definition Core.h:271
unsigned int uint
Definition Core.h:481
#define JPH_NAMESPACE_END
Definition Core.h:414
#define JPH_NAMESPACE_BEGIN
Definition Core.h:408
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
#define JPH_DECLARE_SERIALIZABLE_VIRTUAL(linkage, class_name)
Definition SerializableObject.h:109
Definition Array.h:36
Definition CollisionGroup.h:20
GroupID GetGroupID() const
Definition CollisionGroup.h:54
SubGroupID GetSubGroupID() const
Definition CollisionGroup.h:65
static const GroupID cInvalidGroup
Definition CollisionGroup.h:27
uint32 GroupID
Definition CollisionGroup.h:24
const GroupFilter * GetGroupFilter() const
Get the collision group filter.
Definition CollisionGroup.h:43
uint32 SubGroupID
Definition CollisionGroup.h:25
Abstract class that checks if two CollisionGroups collide.
Definition GroupFilter.h:18
virtual void SaveBinaryState(StreamOut &inStream) const
Saves the contents of the group filter in binary form to inStream.
Definition GroupFilter.cpp:17
virtual void RestoreBinaryState(StreamIn &inStream)
This function should not be called directly, it is used by sRestoreFromBinaryState.
Definition GroupFilter.cpp:22
Definition GroupFilterTable.h:36
void DisableCollision(SubGroupID inSubGroup1, SubGroupID inSubGroup2)
Disable collision between two sub groups.
Definition GroupFilterTable.h:74
void EnableCollision(SubGroupID inSubGroup1, SubGroupID inSubGroup2)
Enable collision between two sub groups.
Definition GroupFilterTable.h:81
GroupFilterTable(const GroupFilterTable &inRHS)
Copy constructor.
Definition GroupFilterTable.h:71
virtual bool CanCollide(const CollisionGroup &inGroup1, const CollisionGroup &inGroup2) const override
Checks if two CollisionGroups collide.
Definition GroupFilterTable.h:96
GroupFilterTable(uint inNumSubGroups=0)
Constructs the table with inNumSubGroups subgroups, initially all collision pairs are enabled except ...
Definition GroupFilterTable.h:62
bool IsCollisionEnabled(SubGroupID inSubGroup1, SubGroupID inSubGroup2) const
Check if the collision between two subgroups is enabled.
Definition GroupFilterTable.h:88
Simple binary input stream.
Definition StreamIn.h:13
Simple binary output stream.
Definition StreamOut.h:13