Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
ObjectLayerPairFilterTable.h
Go to the documentation of this file.
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
8
10
14{
15private:
17 uint GetBit(ObjectLayer inLayer1, ObjectLayer inLayer2) const
18 {
19 // We store the lower left half only, so swap the inputs when trying to access the top right half
20 if (inLayer1 > inLayer2)
21 swap(inLayer1, inLayer2);
22
23 JPH_ASSERT(inLayer2 < mNumObjectLayers);
24
25 // Calculate at which bit the entry for this pair resides
26 // We use the fact that a row always starts at inLayer2 * (inLayer2 + 1) / 2
27 // (this is the amount of bits needed to store a table of inLayer2 entries)
28 return (inLayer2 * (inLayer2 + 1)) / 2 + inLayer1;
29 }
30
31public:
33
35 explicit ObjectLayerPairFilterTable(uint inNumObjectLayers) :
36 mNumObjectLayers(inNumObjectLayers)
37 {
38 // By default nothing collides
39 // For the first layer we only need to store 1 bit, for the second 2 bits, for the third 3 bits, etc.
40 // We use the formula Sum_i=1^N i = N * (N + 1) / 2 to calculate the size of the table
41 int table_size = (inNumObjectLayers * (inNumObjectLayers + 1) / 2 + 7) / 8;
42 mTable.resize(table_size, 0);
43 }
44
47 {
48 return mNumObjectLayers;
49 }
50
52 void DisableCollision(ObjectLayer inLayer1, ObjectLayer inLayer2)
53 {
54 uint bit = GetBit(inLayer1, inLayer2);
55 mTable[bit >> 3] &= (0xff ^ (1 << (bit & 0b111)));
56 }
57
59 void EnableCollision(ObjectLayer inLayer1, ObjectLayer inLayer2)
60 {
61 uint bit = GetBit(inLayer1, inLayer2);
62 mTable[bit >> 3] |= 1 << (bit & 0b111);
63 }
64
66 virtual bool ShouldCollide(ObjectLayer inObject1, ObjectLayer inObject2) const override
67 {
68 // Test if the bit is set for this group pair
69 uint bit = GetBit(inObject1, inObject2);
70 return (mTable[bit >> 3] & (1 << (bit & 0b111))) != 0;
71 }
72
73private:
74 uint mNumObjectLayers;
75 Array<uint8> mTable;
76};
77
unsigned int uint
Definition Core.h:448
#define JPH_NAMESPACE_END
Definition Core.h:377
#define JPH_NAMESPACE_BEGIN
Definition Core.h:371
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:31
uint16 ObjectLayer
Definition ObjectLayer.h:16
Definition Array.h:36
void resize(size_type inNewSize)
Resize array to new length.
Definition Array.h:118
Filter class to test if two objects can collide based on their object layer. Used while finding colli...
Definition ObjectLayer.h:50
Definition ObjectLayerPairFilterTable.h:14
uint GetNumObjectLayers() const
Get the number of object layers.
Definition ObjectLayerPairFilterTable.h:46
void DisableCollision(ObjectLayer inLayer1, ObjectLayer inLayer2)
Disable collision between two object layers.
Definition ObjectLayerPairFilterTable.h:52
JPH_OVERRIDE_NEW_DELETE ObjectLayerPairFilterTable(uint inNumObjectLayers)
Constructs the table with inNumObjectLayers Layers, initially all layer pairs are disabled.
Definition ObjectLayerPairFilterTable.h:35
virtual bool ShouldCollide(ObjectLayer inObject1, ObjectLayer inObject2) const override
Returns true if two layers can collide.
Definition ObjectLayerPairFilterTable.h:66
void EnableCollision(ObjectLayer inLayer1, ObjectLayer inLayer2)
Enable collision between two object layers.
Definition ObjectLayerPairFilterTable.h:59