Fix issue with filters being ignored for two controllers colliding

This commit is contained in:
xKamuna
2021-12-30 23:47:26 -08:00
parent 36a18a0b56
commit ef40ccbe14
4 changed files with 61 additions and 0 deletions

View File

@@ -6,6 +6,8 @@
#include "Actors/PhysicsColliderActor.h"
#include <ThirdParty/PhysX/PxScene.h>
#include <ThirdParty/PhysX/PxQueryFiltering.h>
#include <ThirdParty/PhysX/PxRigidDynamic.h>
#include <ThirdParty/PhysX/characterkinematic/PxController.h>
// Temporary result buffer size
#define HIT_BUFFER_SIZE 128
@@ -217,6 +219,52 @@ public:
}
};
class CharacterControllerFilter : public PxControllerFilterCallback
{
private:
PxShape* getShape(const PxController& controller)
{
PxRigidDynamic* actor = controller.getActor();
// Early out if no actor or no shapes
if (!actor || actor->getNbShapes() < 1)
return nullptr;
// Get first shape only.
PxShape* shape = nullptr;
actor->getShapes(&shape, 1);
return shape;
}
public:
bool filter(const PxController& a, const PxController& b) override
{
// Early out to avoid crashing
PxShape* shapeA = getShape(a);
if (!shapeA)
return false;
PxShape* shapeB = getShape(b);
if (!shapeB)
return false;
// Let triggers through
if (PxFilterObjectIsTrigger(shapeB->getFlags()))
return false;
// Trigger the contact callback for pairs (A,B) where the filtermask of A contains the ID of B and vice versa
const PxFilterData shapeFilterA = shapeA->getQueryFilterData();
const PxFilterData shapeFilterB = shapeB->getQueryFilterData();
if (shapeFilterA.word0 & shapeFilterB.word1)
return true;
return false;
}
};
PxQueryFilterCallback* Physics::GetQueryFilterCallback()
{
static QueryFilter Filter;
@@ -229,6 +277,12 @@ PxQueryFilterCallback* Physics::GetCharacterQueryFilterCallback()
return &Filter;
}
PxControllerFilterCallback* Physics::GetCharacterControllerFilterCallback()
{
static CharacterControllerFilter Filter;
return &Filter;
}
bool Physics::RayCast(const Vector3& origin, const Vector3& direction, const float maxDistance, uint32 layerMask, bool hitTriggers)
{
// Prepare data