From 5a2401bec2674f801c52208a2ada4b8f54f7bbac Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Sat, 4 Mar 2023 17:42:47 +0800 Subject: [PATCH 1/3] Reverted tolerances for ConvexVSConvex --- .../Physics/Collision/Narrowphase/SHConvexVsConvex.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHConvexVsConvex.cpp b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHConvexVsConvex.cpp index d3b4e59b..8c7ffc5e 100644 --- a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHConvexVsConvex.cpp +++ b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHConvexVsConvex.cpp @@ -67,8 +67,8 @@ namespace SHADE bool SHCollision::ConvexVsConvex(SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept { - static constexpr float ABSOLUTE_TOLERANCE = 0.0005f; - static constexpr float RELATIVE_TOLERANCE = 1.002f; + static constexpr float ABSOLUTE_TOLERANCE = 0.01f; + static constexpr float RELATIVE_TOLERANCE = 0.95f; const SHConvexPolyhedron& POLY_A = reinterpret_cast(A); const SHConvexPolyhedron& POLY_B = reinterpret_cast(B); @@ -130,7 +130,7 @@ namespace SHADE const SHConvexPolyhedron* incidentPoly = nullptr; SHCollisionUtils::FaceQuery minFaceQuery; - if (FACE_QUERY_A.bestDistance > FACE_QUERY_B.bestDistance * RELATIVE_TOLERANCE + ABSOLUTE_TOLERANCE) + if (FACE_QUERY_A.bestDistance + ABSOLUTE_TOLERANCE > FACE_QUERY_B.bestDistance * RELATIVE_TOLERANCE) { minFaceQuery = FACE_QUERY_A; referencePoly = &POLY_A; @@ -157,7 +157,7 @@ namespace SHADE // If an edge pair contains the closest distance,vwe ignore any face queries and find the closest points on // each edge and use that as the contact point. - if (EDGE_QUERY.bestDistance * RELATIVE_TOLERANCE + ABSOLUTE_TOLERANCE > minFaceQuery.bestDistance + ABSOLUTE_TOLERANCE) + if (EDGE_QUERY.bestDistance * RELATIVE_TOLERANCE > minFaceQuery.bestDistance + ABSOLUTE_TOLERANCE) { const SHHalfEdgeStructure::HalfEdge& HALF_EDGE_A = POLY_A.GetHalfEdge(EDGE_QUERY.halfEdgeA); const SHHalfEdgeStructure::HalfEdge& HALF_EDGE_B = POLY_B.GetHalfEdge(EDGE_QUERY.halfEdgeB); From 9b2c5a1804a03f79e75cececade7bbd72c0919ef Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Sat, 4 Mar 2023 19:50:53 +0800 Subject: [PATCH 2/3] Added math helpers for checking if a number is infinity or invalid --- SHADE_Engine/src/Math/SHMathHelpers.h | 6 ++++++ SHADE_Engine/src/Math/SHMathHelpers.hpp | 17 +++++++++++++++++ .../src/Physics/Dynamics/SHContactSolver.cpp | 10 +--------- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/SHADE_Engine/src/Math/SHMathHelpers.h b/SHADE_Engine/src/Math/SHMathHelpers.h index 1d65eb91..01aa5874 100644 --- a/SHADE_Engine/src/Math/SHMathHelpers.h +++ b/SHADE_Engine/src/Math/SHMathHelpers.h @@ -105,6 +105,12 @@ namespace SHADE template [[nodiscard]] static bool CompareFloat (T lhs, T rhs, T absTolerance = EPSILON, T relTolerance = EPSILON); + template + [[nodiscard]] static bool IsInfinity (T value); + + template + [[nodiscard]] static bool IsNaN (T value); + private: /*---------------------------------------------------------------------------------*/ /* Static Data Members */ diff --git a/SHADE_Engine/src/Math/SHMathHelpers.hpp b/SHADE_Engine/src/Math/SHMathHelpers.hpp index 554fa317..9a483566 100644 --- a/SHADE_Engine/src/Math/SHMathHelpers.hpp +++ b/SHADE_Engine/src/Math/SHMathHelpers.hpp @@ -126,4 +126,21 @@ namespace SHADE return std::fabs(lhs - rhs) <= Max(absTolerance, RTOL); } + template + bool SHMath::IsInfinity(T value) + { + const float MAX_VALUE = std::numeric_limits::max(); + const float MIN_VALUE = std::numeric_limits::min(); + + return !(MIN_VALUE <= value && value <= MAX_VALUE); + } + + template + bool SHMath::IsNaN(T value) + { + return value != value; + } + + + } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/Dynamics/SHContactSolver.cpp b/SHADE_Engine/src/Physics/Dynamics/SHContactSolver.cpp index 87276c82..b25f38b0 100644 --- a/SHADE_Engine/src/Physics/Dynamics/SHContactSolver.cpp +++ b/SHADE_Engine/src/Physics/Dynamics/SHContactSolver.cpp @@ -18,14 +18,6 @@ #include "Physics/SHPhysicsConstants.h" #include "Physics/Collision/Narrowphase/SHCollisionUtils.h" -bool IsInfinity(float value) -{ - const float MAX_VAL = std::numeric_limits::max(); - const float MIN_VAL = -MAX_VAL; - - return !(MIN_VAL <= value && value <= MAX_VAL); -} - namespace SHADE { /*-----------------------------------------------------------------------------------*/ @@ -227,7 +219,7 @@ namespace SHADE errorBias = -SHPHYSICS_BAUMGARTE * INV_DT * std::max(0.0f, contact.penetration - SHPHYSICS_LINEAR_SLOP); // TODO: This should never occur. Move this check into the ConvexVSConvex to find the error. - if (IsInfinity(errorBias)) + if (SHMath::IsInfinity(errorBias)) errorBias = 0.0f; // Warm starting From 02b21f2694bfcc6a99d3a33e30bab83ce00db884 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Sat, 4 Mar 2023 20:28:09 +0800 Subject: [PATCH 3/3] 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 )