From 636ff587c3b10ae024f8cf1e597c6ad6d4450c44 Mon Sep 17 00:00:00 2001 From: amzn-sean <75276488+amzn-sean@users.noreply.github.com> Date: Mon, 5 Jul 2021 18:06:20 +0100 Subject: [PATCH] Shape casts correctly report positions of colliders that intersect or in contact of the initial pose (#1848) Signed-off-by: amzn-sean <75276488+amzn-sean@users.noreply.github.com> --- .../Physics/Common/PhysicsSceneQueries.h | 2 +- .../Code/Source/Common/PhysXSceneQueryHelpers.cpp | 14 ++++++++++---- Gems/PhysX/Code/Source/Scene/PhysXScene.cpp | 2 ++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Code/Framework/AzFramework/AzFramework/Physics/Common/PhysicsSceneQueries.h b/Code/Framework/AzFramework/AzFramework/Physics/Common/PhysicsSceneQueries.h index af1f5947a4..443315ce92 100644 --- a/Code/Framework/AzFramework/AzFramework/Physics/Common/PhysicsSceneQueries.h +++ b/Code/Framework/AzFramework/AzFramework/Physics/Common/PhysicsSceneQueries.h @@ -188,7 +188,7 @@ namespace AzPhysics AZ::Transform m_start = AZ::Transform::CreateIdentity(); //!< World space start position. Assumes only rotation + translation (no scaling). AZ::Vector3 m_direction = AZ::Vector3::CreateZero(); //!< World space direction (Should be normalized) AZStd::shared_ptr m_shapeConfiguration; //!< Shape information. - SceneQuery::HitFlags m_hitFlags = SceneQuery::HitFlags::Default; //!< Query behavior flags + SceneQuery::HitFlags m_hitFlags = SceneQuery::HitFlags::Default | SceneQuery::HitFlags::MTD; //!< Query behavior flags. MTD Is On by default to correctly report objects that are initially in contact with the start pose. SceneQuery::FilterCallback m_filterCallback = nullptr; //!< Hit filtering function bool m_reportMultipleHits = false; //!< flag to have the cast stop after the first hit or return all hits along the query. }; diff --git a/Gems/PhysX/Code/Source/Common/PhysXSceneQueryHelpers.cpp b/Gems/PhysX/Code/Source/Common/PhysXSceneQueryHelpers.cpp index 37e37d07e3..c157f14871 100644 --- a/Gems/PhysX/Code/Source/Common/PhysXSceneQueryHelpers.cpp +++ b/Gems/PhysX/Code/Source/Common/PhysXSceneQueryHelpers.cpp @@ -45,11 +45,17 @@ namespace PhysX hit.m_distance = pxHit.distance; hit.m_resultFlags |= AzPhysics::SceneQuery::ResultFlags::Distance; - hit.m_position = PxMathConvert(pxHit.position); - hit.m_resultFlags |= AzPhysics::SceneQuery::ResultFlags::Position; + if (pxHit.flags & physx::PxHitFlag::ePOSITION) + { + hit.m_position = PxMathConvert(pxHit.position); + hit.m_resultFlags |= AzPhysics::SceneQuery::ResultFlags::Position; + } - hit.m_normal = PxMathConvert(pxHit.normal); - hit.m_resultFlags |= AzPhysics::SceneQuery::ResultFlags::Normal; + if (pxHit.flags & physx::PxHitFlag::eNORMAL) + { + hit.m_normal = PxMathConvert(pxHit.normal); + hit.m_resultFlags |= AzPhysics::SceneQuery::ResultFlags::Normal; + } const ActorData* actorData = Utils::GetUserData(pxHit.actor); hit.m_bodyHandle = actorData->GetBodyHandle(); diff --git a/Gems/PhysX/Code/Source/Scene/PhysXScene.cpp b/Gems/PhysX/Code/Source/Scene/PhysXScene.cpp index 6d3d7a2607..76d40ea5fb 100644 --- a/Gems/PhysX/Code/Source/Scene/PhysXScene.cpp +++ b/Gems/PhysX/Code/Source/Scene/PhysXScene.cpp @@ -334,6 +334,8 @@ namespace PhysX { const physx::PxTransform pose = PxMathConvert(shapecastRequest->m_start); const physx::PxVec3 dir = PxMathConvert(shapecastRequest->m_direction.GetNormalized()); + AZ_Warning("PhysXScene", (static_cast(shapecastRequest->m_hitFlags & AzPhysics::SceneQuery::HitFlags::MTD) != 0), + "Not having MTD set for shape scene queries may result in incorrect reporting of colliders that are in contact or intersect the initial pose of the sweep."); const physx::PxHitFlags hitFlags = SceneQueryHelpers::GetPxHitFlags(shapecastRequest->m_hitFlags); bool status = false;