Adds findContact to regular physics bodies so that they can find contacting objects and surfaces in a way similar to players.

This commit is contained in:
Areloch 2016-05-13 23:57:48 -05:00
parent 1299b527f1
commit b64123a452
5 changed files with 125 additions and 0 deletions

View file

@ -417,3 +417,55 @@ void Px3Body::applyImpulse( const Point3F &origin, const Point3F &force )
}
void Px3Body::findContact(SceneObject **contactObject,
VectorF *contactNormal,
Vector<SceneObject*> *outOverlapObjects) const
{
// Calculate the sweep motion...
F32 halfCapSize = mOriginOffset;
F32 halfSmallCapSize = halfCapSize * 0.8f;
F32 diff = halfCapSize - halfSmallCapSize;
F32 distance = diff + mSkinWidth + 0.01f;
physx::PxVec3 dir(0, 0, -1);
physx::PxScene *scene = mWorld->getScene();
physx::PxHitFlags hitFlags(physx::PxHitFlag::eDEFAULT);
physx::PxQueryFilterData filterData(physx::PxQueryFlag::eDYNAMIC | physx::PxQueryFlag::eSTATIC);
filterData.data.word0 = PX3_DEFAULT;
physx::PxSweepHit sweepHit;
physx::PxRigidDynamic *actor = mController->getActor();
physx::PxU32 shapeIndex;
bool hit = physx::PxRigidBodyExt::linearSweepSingle(*actor, *scene, dir, distance, hitFlags, sweepHit, shapeIndex, filterData);
if (hit)
{
PhysicsUserData *data = PhysicsUserData::cast(sweepHit.actor->userData);
if (data)
{
*contactObject = data->getObject();
*contactNormal = px3Cast<Point3F>(sweepHit.normal);
}
}
// Check for overlapped objects ( triggers )
if (!outOverlapObjects)
return;
filterData.data.word0 = PX3_TRIGGER;
const physx::PxU32 bufferSize = 10;
physx::PxOverlapBufferN<bufferSize> hitBuffer;
hit = scene->overlap(mGeometry, actor->getGlobalPose(), hitBuffer, filterData);
if (hit)
{
for (U32 i = 0; i < hitBuffer.nbTouches; i++)
{
PhysicsUserData *data = PhysicsUserData::cast(hitBuffer.touches[i].actor->userData);
if (data)
outOverlapObjects->push_back(data->getObject());
}
}
}

View file

@ -117,6 +117,9 @@ public:
F32 staticFriction );
virtual void applyCorrection( const MatrixF &xfm );
virtual void applyImpulse( const Point3F &origin, const Point3F &force );
virtual void findContact(SceneObject **contactObject, VectorF *contactNormal,
Vector<SceneObject*> *outOverlapObjects) const;
};
#endif // _PX3BODY_H_