From 02b21f2694bfcc6a99d3a33e30bab83ce00db884 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Sat, 4 Mar 2023 20:28:09 +0800 Subject: [PATCH] Fixed auto mass serialisation and colliders not recomputing mass on size change --- Assets/Scenes/SS_Playground.shade | 12 ++++++------ .../src/Physics/Collision/SHCollider.cpp | 11 +++++++++++ .../src/Physics/Collision/SHCollider.h | 12 ++++++++++++ .../src/Physics/Collision/Shapes/SHBox.cpp | 18 ++++++++++++------ .../src/Physics/Collision/Shapes/SHSphere.cpp | 18 ++++++++++++------ .../src/Physics/Dynamics/SHRigidBody.cpp | 2 -- .../Physics/Interface/SHRigidBodyComponent.cpp | 2 +- 7 files changed, 54 insertions(+), 21 deletions(-) diff --git a/Assets/Scenes/SS_Playground.shade b/Assets/Scenes/SS_Playground.shade index 7e8c8c8f..34bd82e3 100644 --- a/Assets/Scenes/SS_Playground.shade +++ b/Assets/Scenes/SS_Playground.shade @@ -50,8 +50,8 @@ IsActive: true RigidBody Component: Type: Dynamic - Auto Mass: false Mass: 1 + Auto Mass: false Drag: 0.00999999978 Angular Drag: 0.00999999978 Use Gravity: true @@ -188,8 +188,8 @@ IsActive: true RigidBody Component: Type: Dynamic - Auto Mass: false Mass: 1 + Auto Mass: false Drag: 0.00999999978 Angular Drag: 0.00999999978 Use Gravity: true @@ -228,8 +228,8 @@ IsActive: true RigidBody Component: Type: Dynamic - Auto Mass: false Mass: 1 + Auto Mass: false Drag: 0.00999999978 Angular Drag: 0.00999999978 Use Gravity: true @@ -268,8 +268,8 @@ IsActive: true RigidBody Component: Type: Dynamic - Auto Mass: false Mass: 1 + Auto Mass: false Drag: 0.00999999978 Angular Drag: 0.00999999978 Use Gravity: true @@ -308,8 +308,8 @@ IsActive: true RigidBody Component: Type: Dynamic - Auto Mass: false Mass: 1 + Auto Mass: false Drag: 0.00999999978 Angular Drag: 0.00999999978 Use Gravity: true @@ -328,7 +328,7 @@ - Is Trigger: false Collision Tag: 0 Type: Box - Half Extents: {x: 1, y: 1, z: 1} + Half Extents: {x: 1, y: 2, z: 1} Friction: 0.400000006 Bounciness: 0 Density: 1 diff --git a/SHADE_Engine/src/Physics/Collision/SHCollider.cpp b/SHADE_Engine/src/Physics/Collision/SHCollider.cpp index 2f1de000..aa28fd7f 100644 --- a/SHADE_Engine/src/Physics/Collision/SHCollider.cpp +++ b/SHADE_Engine/src/Physics/Collision/SHCollider.cpp @@ -326,6 +326,17 @@ namespace SHADE shape->Update(); } + void SHCollider::UpdateBroadphase() noexcept + { + flags |= MOVED_FLAG; + } + + void SHCollider::UpdateBody() noexcept + { + if (rigidBody) + rigidBody->ComputeMassData(); + } + /*-----------------------------------------------------------------------------------*/ /* Private Member Function Definitions */ /*-----------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Physics/Collision/SHCollider.h b/SHADE_Engine/src/Physics/Collision/SHCollider.h index a326215f..3626d7a7 100644 --- a/SHADE_Engine/src/Physics/Collision/SHCollider.h +++ b/SHADE_Engine/src/Physics/Collision/SHCollider.h @@ -149,6 +149,18 @@ namespace SHADE */ void Update () noexcept; + /** + * @brief + * Updates the broadphase proxy of this collider. + */ + void UpdateBroadphase() noexcept; + + /** + * @brief + * Recomputes the mass of the body if one exists. + */ + void UpdateBody() noexcept; + protected: /*---------------------------------------------------------------------------------*/ /* Data Members */ diff --git a/SHADE_Engine/src/Physics/Collision/Shapes/SHBox.cpp b/SHADE_Engine/src/Physics/Collision/Shapes/SHBox.cpp index 4a9d88bb..f11b875c 100644 --- a/SHADE_Engine/src/Physics/Collision/Shapes/SHBox.cpp +++ b/SHADE_Engine/src/Physics/Collision/Shapes/SHBox.cpp @@ -207,9 +207,11 @@ namespace SHADE // Recompute Relative radius relativeExtents = 2.0f * Extents / scale; - // Hack: Indicate that the collider needs to update the broadphase proxy if (collider) - collider->SetScale(collider->GetScale()); + { + collider->UpdateBroadphase(); + collider->UpdateBody(); + } } void SHBox::SetRelativeExtents(const SHVec3& newRelativeExtents) noexcept @@ -219,9 +221,11 @@ namespace SHADE // Recompute world radius Extents = relativeExtents * scale * 0.5f; - // Hack: Indicate that the collider needs to update the broadphase proxy if (collider) - collider->SetScale(collider->GetScale()); + { + collider->UpdateBroadphase(); + collider->UpdateBody(); + } } void SHBox::SetScale(const SHVec3& newScale) noexcept @@ -231,9 +235,11 @@ namespace SHADE // Recompute world radius Extents = relativeExtents * scale * 0.5f; - // Hack: Indicate that the collider needs to update the broadphase proxy if (collider) - collider->SetScale(collider->GetScale()); + { + collider->UpdateBroadphase(); + collider->UpdateBody(); + } } /*-----------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Physics/Collision/Shapes/SHSphere.cpp b/SHADE_Engine/src/Physics/Collision/Shapes/SHSphere.cpp index 3023ec68..130ccb42 100644 --- a/SHADE_Engine/src/Physics/Collision/Shapes/SHSphere.cpp +++ b/SHADE_Engine/src/Physics/Collision/Shapes/SHSphere.cpp @@ -168,9 +168,11 @@ namespace SHADE // Recompute Relative radius relativeRadius = 2.0f * Radius / scale; - // Hack: Indicate that the collider needs to update the broadphase proxy if (collider) - collider->SetScale(collider->GetScale()); + { + collider->UpdateBroadphase(); + collider->UpdateBody(); + } } void SHSphere::SetRelativeRadius(float newRelativeRadius) noexcept @@ -180,9 +182,11 @@ namespace SHADE // Recompute world radius Radius = relativeRadius * scale * 0.5f; - // Hack: Indicate that the collider needs to update the broadphase proxy if (collider) - collider->SetScale(collider->GetScale()); + { + collider->UpdateBroadphase(); + collider->UpdateBody(); + } } void SHSphere::SetScale(float maxScale) noexcept @@ -192,9 +196,11 @@ namespace SHADE // Recompute world radius Radius = relativeRadius * scale * 0.5f; - // Hack: Indicate that the collider needs to update the broadphase proxy if (collider) - collider->SetScale(collider->GetScale()); + { + collider->UpdateBroadphase(); + collider->UpdateBody(); + } } /*-----------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Physics/Dynamics/SHRigidBody.cpp b/SHADE_Engine/src/Physics/Dynamics/SHRigidBody.cpp index bdaaf230..43d655c3 100644 --- a/SHADE_Engine/src/Physics/Dynamics/SHRigidBody.cpp +++ b/SHADE_Engine/src/Physics/Dynamics/SHRigidBody.cpp @@ -40,8 +40,6 @@ namespace SHADE flags |= 1U << 0; // Body is active flags |= 1U << 2; // Sleeping is enabled flags |= 1U << 3; // Gravity is enabled - - // TODO: Compute inertia if body is dynamic } SHRigidBody::SHRigidBody(const SHRigidBody& rhs) noexcept diff --git a/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.cpp b/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.cpp index 04bb5b6b..d80a988b 100644 --- a/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.cpp +++ b/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.cpp @@ -360,8 +360,8 @@ RTTR_REGISTRATION registration::class_("RigidBody Component") .property("Type" , &SHRigidBodyComponent::GetType , &SHRigidBodyComponent::SetType ) - .property("Auto Mass" , &SHRigidBodyComponent::GetAutoMass , &SHRigidBodyComponent::SetAutoMass ) .property("Mass" , &SHRigidBodyComponent::GetMass , &SHRigidBodyComponent::SetMass ) + .property("Auto Mass" , &SHRigidBodyComponent::GetAutoMass , &SHRigidBodyComponent::SetAutoMass ) .property("Drag" , &SHRigidBodyComponent::GetDrag , &SHRigidBodyComponent::SetDrag ) .property("Angular Drag" , &SHRigidBodyComponent::GetAngularDrag , &SHRigidBodyComponent::SetAngularDrag ) .property("Use Gravity" , &SHRigidBodyComponent::IsGravityEnabled , &SHRigidBodyComponent::SetIsGravityEnabled )