From 8e90e87bc83774d767cda5124871dd40df8c8642 Mon Sep 17 00:00:00 2001 From: greerdv Date: Tue, 11 May 2021 16:17:23 +0100 Subject: [PATCH 1/2] fixing bug with shape collider editor body not being deleted when it is recreated --- .../Source/EditorShapeColliderComponent.cpp | 34 ++++++++++++++----- .../Source/EditorShapeColliderComponent.h | 1 - 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/Gems/PhysX/Code/Source/EditorShapeColliderComponent.cpp b/Gems/PhysX/Code/Source/EditorShapeColliderComponent.cpp index 0a8bca33ea..2c86a18301 100644 --- a/Gems/PhysX/Code/Source/EditorShapeColliderComponent.cpp +++ b/Gems/PhysX/Code/Source/EditorShapeColliderComponent.cpp @@ -267,8 +267,14 @@ namespace PhysX if (m_sceneInterface) { + //remove the previous body if any + if (m_editorBodyHandle != AzPhysics::InvalidSimulatedBodyHandle) + { + m_sceneInterface->RemoveSimulatedBody(m_editorSceneHandle, m_editorBodyHandle); + m_editorBodyHandle = AzPhysics::InvalidSimulatedBodyHandle; + } + m_editorBodyHandle = m_sceneInterface->AddSimulatedBody(m_editorSceneHandle, &configuration); - m_editorBody = azdynamic_cast(m_sceneInterface->GetSimulatedBodyFromHandle(m_editorSceneHandle, m_editorBodyHandle)); } AzPhysics::SimulatedBodyComponentRequestsBus::Handler::BusConnect(GetEntityId()); @@ -676,7 +682,6 @@ namespace PhysX { m_sceneInterface->RemoveSimulatedBody(m_editorSceneHandle, m_editorBodyHandle); m_editorBodyHandle = AzPhysics::InvalidSimulatedBodyHandle; - m_editorBody = nullptr; } } @@ -747,21 +752,31 @@ namespace PhysX bool EditorShapeColliderComponent::IsPhysicsEnabled() const { - return m_editorBody != nullptr && m_editorBody->m_simulating; + return m_editorBodyHandle != AzPhysics::InvalidSimulatedBodyHandle; } AZ::Aabb EditorShapeColliderComponent::GetAabb() const { - if (m_editorBody) + if (m_sceneInterface && m_editorBodyHandle != AzPhysics::InvalidSimulatedBodyHandle) { - return m_editorBody->GetAabb(); + if (auto* body = m_sceneInterface->GetSimulatedBodyFromHandle(m_editorSceneHandle, m_editorBodyHandle)) + { + return body->GetAabb(); + } } return AZ::Aabb::CreateNull(); } AzPhysics::SimulatedBody* EditorShapeColliderComponent::GetSimulatedBody() { - return m_editorBody; + if (m_sceneInterface && m_editorBodyHandle != AzPhysics::InvalidSimulatedBodyHandle) + { + if (auto* body = m_sceneInterface->GetSimulatedBodyFromHandle(m_editorSceneHandle, m_editorBodyHandle)) + { + return body; + } + } + return nullptr; } AzPhysics::SimulatedBodyHandle EditorShapeColliderComponent::GetSimulatedBodyHandle() const @@ -771,9 +786,12 @@ namespace PhysX AzPhysics::SceneQueryHit EditorShapeColliderComponent::RayCast(const AzPhysics::RayCastRequest& request) { - if (m_editorBody) + if (m_sceneInterface && m_editorBodyHandle != AzPhysics::InvalidSimulatedBodyHandle) { - return m_editorBody->RayCast(request); + if (auto* body = m_sceneInterface->GetSimulatedBodyFromHandle(m_editorSceneHandle, m_editorBodyHandle)) + { + return body->RayCast(request); + } } return AzPhysics::SceneQueryHit(); } diff --git a/Gems/PhysX/Code/Source/EditorShapeColliderComponent.h b/Gems/PhysX/Code/Source/EditorShapeColliderComponent.h index bcf5ac4eba..7b7fab789a 100644 --- a/Gems/PhysX/Code/Source/EditorShapeColliderComponent.h +++ b/Gems/PhysX/Code/Source/EditorShapeColliderComponent.h @@ -143,7 +143,6 @@ namespace PhysX DebugDraw::Collider m_colliderDebugDraw; //!< Handles drawing the collider based on global and local AzPhysics::SceneInterface* m_sceneInterface = nullptr; AzPhysics::SceneHandle m_editorSceneHandle = AzPhysics::InvalidSceneHandle; - StaticRigidBody* m_editorBody = nullptr; //!< Body in the editor physics scene if there is no rigid body component. AzPhysics::SimulatedBodyHandle m_editorBodyHandle = AzPhysics::InvalidSimulatedBodyHandle; //!< Handle to the body in the editor physics scene if there is no rigid body component. bool m_shapeTypeWarningIssued = false; //!< Records whether a warning about unsupported shapes has been previously issued. PolygonPrismMeshUtils::Mesh2D m_mesh; //!< Used for storing decompositions of the polygon prism. From dff8de94a5f4c235fc14587c58cfe146640947cb Mon Sep 17 00:00:00 2001 From: greerdv Date: Tue, 11 May 2021 16:57:23 +0100 Subject: [PATCH 2/2] making both colliders and shape colliders check m_simulating in IsPhysicsEnabled --- Gems/PhysX/Code/Source/EditorColliderComponent.cpp | 9 ++++++++- Gems/PhysX/Code/Source/EditorShapeColliderComponent.cpp | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Gems/PhysX/Code/Source/EditorColliderComponent.cpp b/Gems/PhysX/Code/Source/EditorColliderComponent.cpp index 2785ab19e1..2cf5835a1f 100644 --- a/Gems/PhysX/Code/Source/EditorColliderComponent.cpp +++ b/Gems/PhysX/Code/Source/EditorColliderComponent.cpp @@ -1057,7 +1057,14 @@ namespace PhysX bool EditorColliderComponent::IsPhysicsEnabled() const { - return m_editorBodyHandle != AzPhysics::InvalidSimulatedBodyHandle; + if (m_sceneInterface && m_editorBodyHandle != AzPhysics::InvalidSimulatedBodyHandle) + { + if (auto* body = m_sceneInterface->GetSimulatedBodyFromHandle(m_editorSceneHandle, m_editorBodyHandle)) + { + return body->m_simulating; + } + } + return false; } AZ::Aabb EditorColliderComponent::GetAabb() const diff --git a/Gems/PhysX/Code/Source/EditorShapeColliderComponent.cpp b/Gems/PhysX/Code/Source/EditorShapeColliderComponent.cpp index 2c86a18301..b71bd47288 100644 --- a/Gems/PhysX/Code/Source/EditorShapeColliderComponent.cpp +++ b/Gems/PhysX/Code/Source/EditorShapeColliderComponent.cpp @@ -752,7 +752,14 @@ namespace PhysX bool EditorShapeColliderComponent::IsPhysicsEnabled() const { - return m_editorBodyHandle != AzPhysics::InvalidSimulatedBodyHandle; + if (m_sceneInterface && m_editorBodyHandle != AzPhysics::InvalidSimulatedBodyHandle) + { + if (auto* body = m_sceneInterface->GetSimulatedBodyFromHandle(m_editorSceneHandle, m_editorBodyHandle)) + { + return body->m_simulating; + } + } + return false; } AZ::Aabb EditorShapeColliderComponent::GetAabb() const