ObjectLayerPairFilterMask.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Physics/Collision/ObjectLayer.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// Filter class to test if two objects can collide based on their object layer. Used while finding collision pairs.
  8. /// Uses group bits and mask bits. Two layers can collide if Object1.Group & Object2.Mask is non-zero and Object2.Group & Object1.Mask is non-zero.
  9. /// The behavior is similar to that in e.g. Bullet.
  10. /// This implementation works together with BroadPhaseLayerInterfaceMask and ObjectVsBroadPhaseLayerFilterMask
  11. class ObjectLayerPairFilterMask : public ObjectLayerPairFilter
  12. {
  13. public:
  14. JPH_OVERRIDE_NEW_DELETE
  15. /// Number of bits for the group and mask bits
  16. static constexpr uint32 cNumBits = JPH_OBJECT_LAYER_BITS / 2;
  17. static constexpr uint32 cMask = (1 << cNumBits) - 1;
  18. /// Construct an ObjectLayer from a group and mask bits
  19. static ObjectLayer sGetObjectLayer(uint32 inGroup, uint32 inMask = cMask)
  20. {
  21. JPH_ASSERT((inGroup & ~cMask) == 0);
  22. JPH_ASSERT((inMask & ~cMask) == 0);
  23. return ObjectLayer((inGroup & cMask) | (inMask << cNumBits));
  24. }
  25. /// Get the group bits from an ObjectLayer
  26. static inline uint32 sGetGroup(ObjectLayer inObjectLayer)
  27. {
  28. return uint32(inObjectLayer) & cMask;
  29. }
  30. /// Get the mask bits from an ObjectLayer
  31. static inline uint32 sGetMask(ObjectLayer inObjectLayer)
  32. {
  33. return uint32(inObjectLayer) >> cNumBits;
  34. }
  35. /// Returns true if two layers can collide
  36. virtual bool ShouldCollide(ObjectLayer inObject1, ObjectLayer inObject2) const override
  37. {
  38. return (sGetGroup(inObject1) & sGetMask(inObject2)) != 0
  39. && (sGetGroup(inObject2) & sGetMask(inObject1)) != 0;
  40. }
  41. };
  42. JPH_NAMESPACE_END