From e43089e6ff0b3c6efcbc187852888f31d2112228 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Thu, 2 Mar 2023 13:52:54 +0800 Subject: [PATCH] Fixed bug where box colliders weren't being deleted on scene destroy --- Assets/Editor/Editor.SHConfig | 2 +- Assets/Scenes/PhysicsSandbox.shade | 6 ++-- Assets/Scenes/SS_Playground.shade | 16 +++++----- Assets/Scripts/Tests/PhysicsTestObj.cs | 31 ++++++++++++++++++- .../Collision/Shapes/SHCollisionShape.cpp | 10 +----- .../Collision/Shapes/SHCollisionShape.h | 3 +- .../Shapes/SHCollisionShapeLibrary.cpp | 6 ++++ .../src/Physics/Dynamics/SHContactManager.cpp | 11 ++++--- .../Routines/SHPhysicsPostUpdateRoutine.cpp | 2 -- 9 files changed, 56 insertions(+), 31 deletions(-) diff --git a/Assets/Editor/Editor.SHConfig b/Assets/Editor/Editor.SHConfig index 2d84f1af..65fe522e 100644 --- a/Assets/Editor/Editor.SHConfig +++ b/Assets/Editor/Editor.SHConfig @@ -1,4 +1,4 @@ Start Maximized: true -Working Scene ID: 97402985 +Working Scene ID: 92914350 Window Size: {x: 2000, y: 1518} Style: 0 \ No newline at end of file diff --git a/Assets/Scenes/PhysicsSandbox.shade b/Assets/Scenes/PhysicsSandbox.shade index 7bab2e59..29ed379e 100644 --- a/Assets/Scenes/PhysicsSandbox.shade +++ b/Assets/Scenes/PhysicsSandbox.shade @@ -251,9 +251,9 @@ NumberOfChildren: 0 Components: Transform Component: - Translate: {x: 2, y: 5, z: 3} - Rotate: {x: 0, y: 0, z: 0.785398185} - Scale: {x: 0.999999642, y: 0.999998569, z: 0.999999642} + Translate: {x: 2.35593724, y: 3, z: 3} + Rotate: {x: -0, y: 0.785398006, z: -0} + Scale: {x: 0.999999702, y: 0.999998569, z: 0.999999702} IsActive: true RigidBody Component: Type: Dynamic diff --git a/Assets/Scenes/SS_Playground.shade b/Assets/Scenes/SS_Playground.shade index 55d60e73..5c5f2d85 100644 --- a/Assets/Scenes/SS_Playground.shade +++ b/Assets/Scenes/SS_Playground.shade @@ -4,7 +4,7 @@ NumberOfChildren: 0 Components: Camera Component: - Position: {x: 0, y: 1, z: 5} + Position: {x: 1, y: -0.5, z: 5} Pitch: 0 Yaw: 0 Roll: 0 @@ -30,8 +30,8 @@ Colliders: - Is Trigger: false Collision Tag: 1 - Type: Box - Half Extents: {x: 1, y: 1, z: 1} + Type: Sphere + Radius: 1 Friction: 0.400000006 Bounciness: 0 Density: 1 @@ -45,8 +45,8 @@ NumberOfChildren: 0 Components: Transform Component: - Translate: {x: 1.5, y: 2, z: 1.5} - Rotate: {x: 0, y: 0.785398185, z: 0} + Translate: {x: 0, y: 5, z: 0} + Rotate: {x: -0, y: 0.785398006, z: -0} Scale: {x: 1, y: 1, z: 1} IsActive: true RigidBody Component: @@ -71,8 +71,8 @@ Colliders: - Is Trigger: false Collision Tag: 1 - Type: Box - Half Extents: {x: 1, y: 1, z: 1} + Type: Sphere + Radius: 1 Friction: 0.400000006 Bounciness: 0 Density: 1 @@ -83,4 +83,4 @@ - Type: PhysicsTestObj Enabled: true forceAmount: 50 - torqueAmount: 500 \ No newline at end of file + torqueAmount: 25 \ No newline at end of file diff --git a/Assets/Scripts/Tests/PhysicsTestObj.cs b/Assets/Scripts/Tests/PhysicsTestObj.cs index 2348894f..943af26a 100644 --- a/Assets/Scripts/Tests/PhysicsTestObj.cs +++ b/Assets/Scripts/Tests/PhysicsTestObj.cs @@ -64,8 +64,37 @@ public class PhysicsTestObj : Script }; public float forceAmount = 50.0f; - public float torqueAmount = 500.0f; + public float torqueAmount = 25.0f; + protected override void onTriggerEnter(CollisionInfo info) + { + Debug.Log("Trigger Enter"); + } + + protected override void onTriggerStay(CollisionInfo info) + { + Debug.Log("Trigger Stay"); + } + + protected override void onTriggerExit(CollisionInfo info) + { + Debug.Log("Trigger Exit"); + } + + protected override void onCollisionEnter(CollisionInfo info) + { + Debug.Log("Collision Enter"); + } + + protected override void onCollisionStay(CollisionInfo info) + { + Debug.Log("Collision Stay"); + } + + protected override void onCollisionExit(CollisionInfo info) + { + Debug.Log("Collision Exit"); + } protected override void awake() { body = GetComponent(); diff --git a/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShape.cpp b/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShape.cpp index dfbed047..ef72f474 100644 --- a/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShape.cpp +++ b/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShape.cpp @@ -92,14 +92,6 @@ namespace SHADE } bool SHCollisionShape::IsTrigger() const noexcept - { - static constexpr int FLAG_POS = 3; - static constexpr uint8_t FLAG_VALUE = 1U << FLAG_POS; - - return flags & FLAG_VALUE; - } - - bool SHCollisionShape::IsColliding() const noexcept { static constexpr int FLAG_POS = 4; static constexpr uint8_t FLAG_VALUE = 1U << FLAG_POS; @@ -158,7 +150,7 @@ namespace SHADE void SHCollisionShape::SetIsTrigger(bool isTrigger) noexcept { - static constexpr int FLAG_POS = 3; + static constexpr int FLAG_POS = 4; static constexpr uint8_t FLAG_VALUE = 1U << FLAG_POS; isTrigger ? flags |= FLAG_VALUE : flags &= ~FLAG_VALUE; diff --git a/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShape.h b/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShape.h index 3e57b74a..0fc0f8c4 100644 --- a/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShape.h +++ b/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShape.h @@ -105,7 +105,6 @@ namespace SHADE [[nodiscard]] Type GetType () const noexcept; [[nodiscard]] bool IsTrigger () const noexcept; - [[nodiscard]] bool IsColliding () const noexcept; [[nodiscard]] const SHCollisionTag& GetCollisionTag () const noexcept; @@ -201,7 +200,7 @@ namespace SHADE // Needed for conversion to euler angles SHVec3 rotationOffset; - uint8_t flags; // 0 0 0 trigger convexHull capsule sphere box + uint8_t flags; // 0 0 0 trigger convexHull capsule box sphere RTTR_ENABLE() diff --git a/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShapeLibrary.cpp b/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShapeLibrary.cpp index 5560c509..3e867786 100644 --- a/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShapeLibrary.cpp +++ b/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShapeLibrary.cpp @@ -86,6 +86,12 @@ namespace SHADE { case SHCollisionShape::Type::BOX: { + SHBox* box = boxes.find(shape->id)->second; + boxes.erase(shape->id); + + delete box; + box = nullptr; + break; } case SHCollisionShape::Type::SPHERE: diff --git a/SHADE_Engine/src/Physics/Dynamics/SHContactManager.cpp b/SHADE_Engine/src/Physics/Dynamics/SHContactManager.cpp index 43a904f6..2e0c6bfc 100644 --- a/SHADE_Engine/src/Physics/Dynamics/SHContactManager.cpp +++ b/SHADE_Engine/src/Physics/Dynamics/SHContactManager.cpp @@ -144,7 +144,7 @@ namespace SHADE for (auto manifoldPair = manifolds.begin(); manifoldPair != manifolds.end();) { // Test collision of every manifold. - SHManifold& manifold = manifoldPair->second; + SHManifold& manifold = manifoldPair->second; SHManifold oldManifold = manifold; const bool IS_COLLIDING = SHCollisionDispatcher::Collide(manifold, *manifold.shapeA, *manifold.shapeB); @@ -159,6 +159,8 @@ namespace SHADE continue; } + SHLOG_INFO("State {}", static_cast(manifold.state)) + updateManifold(manifold, oldManifold); ++manifoldPair; } @@ -167,12 +169,11 @@ namespace SHADE for (auto triggerPair = triggers.begin(); triggerPair != triggers.end();) { // Test collision of every trigger. - Trigger& oldTrigger = triggerPair->second; - Trigger newTrigger = oldTrigger; + Trigger& trigger = triggerPair->second; - const bool IS_COLLIDING = SHCollisionDispatcher::Collide(*newTrigger.A, *newTrigger.B); + const bool IS_COLLIDING = SHCollisionDispatcher::Collide(*trigger.A, *trigger.B); - auto& collisionState = newTrigger.state; + auto& collisionState = trigger.state; updateCollisionState(IS_COLLIDING, collisionState); const bool IS_INVALID = collisionState == SHCollisionState::INVALID; diff --git a/SHADE_Engine/src/Physics/System/Routines/SHPhysicsPostUpdateRoutine.cpp b/SHADE_Engine/src/Physics/System/Routines/SHPhysicsPostUpdateRoutine.cpp index a08b943b..47c4ed7b 100644 --- a/SHADE_Engine/src/Physics/System/Routines/SHPhysicsPostUpdateRoutine.cpp +++ b/SHADE_Engine/src/Physics/System/Routines/SHPhysicsPostUpdateRoutine.cpp @@ -94,8 +94,6 @@ namespace SHADE } } - - // Collision & Trigger messages if (scriptingSystem != nullptr) scriptingSystem->ExecuteCollisionFunctions();