1

我想用 Unity 中的图层过滤我的碰撞,但我真的不明白该怎么做。我正在关注本教程:http ://docs.nvidia.com/gameworks/content/gameworkslibrary/physx/guide/Manual/RigidBodyCollision.html#collision-filtering

我要做的就是禁用具有 Cube 和 Plane 层的对象之间的碰撞...

图.cpp:

bool Graph::Init()
{
/*...*/

cubeCollider->SetLayer(Physics::PhysicLayer::Cube);
planeCollider->SetLayer(Physics::PhysicLayer::Plane);
sphereCollider->SetLayer(Physics::PhysicLayer::Sphere);
capsuleCollider->SetLayer(Physics::PhysicLayer::Capsule);

_physX->SetCollisionFiltering(Physics::PhysicLayer::Cube, Physics::PhysicLayer::Plane);

/*...*/
}

这是我设置过滤器着色器的方法:

物理X.cpp:

void PhysX::SetCollisionFiltering(PhysicLayer p_one, PhysicLayer p_two)
{
 // I don't really know what to do here...

PxFilterData filterData;

filterData.word0 =  p_one;
filterData.word1 =  p_two;

// no collision between objects with layer ONE and objects with layer TWO ? 
for (unsigned int i = 0; i < _colliders.size(); ++i)
{
    if (_colliders[i]->GetLayer() == p_one || _colliders[i]->GetLayer() == p_two)
        _colliders[i]->GetShape()->setSimulationFilterData(filterData);
}
}

physx::PxFilterFlags CreateFilterShader(PxFilterObjectAttributes p_attributes0, PxFilterData p_filterData0, 
                                    PxFilterObjectAttributes p_attributes1, PxFilterData p_filterData1, 
                                    PxPairFlags& p_pairFlags, const void* p_constantBlock, PxU32 constantBlockSize)

{
    // Trigger
if (PxFilterObjectIsTrigger(p_attributes0) || PxFilterObjectIsTrigger(p_attributes1))
{
    p_pairFlags = PxPairFlag::eDETECT_DISCRETE_CONTACT
        | PxPairFlag::eSOLVE_CONTACT
        | PxPairFlag::eNOTIFY_TOUCH_FOUND
        | PxPairFlag::eNOTIFY_TOUCH_LOST;
}
    // Normal Collision
else
{
            //  Not sure
    if ((p_filterData0.word0 & p_filterData1.word1) && (p_filterData1.word0 & p_filterData0.word1))
    {
        p_pairFlags = PxPairFlag::eDETECT_DISCRETE_CONTACT
            | PxPairFlag::eSOLVE_CONTACT
            | PxPairFlag::eNOTIFY_CONTACT_POINTS
            | PxPairFlag::eNOTIFY_THRESHOLD_FORCE_FOUND
            | PxPairFlag::eNOTIFY_THRESHOLD_FORCE_LOST
            | PxPairFlag::eNOTIFY_THRESHOLD_FORCE_PERSISTS

            | PxPairFlag::eNOTIFY_TOUCH_FOUND
            | PxPairFlag::eNOTIFY_TOUCH_LOST
            | PxPairFlag::eNOTIFY_TOUCH_PERSISTS;
    }
}

return PxFilterFlag::eDEFAULT;
}

使用此代码,我的所有对象都不会发生碰撞...顺便说一下,我不太了解 PxFilter.word0、word1、word2 和 word3 是什么...

提前致谢 !

4

0 回答 0