diff --git a/Assets/Scenes/PhysicsTest.shade b/Assets/Scenes/PhysicsTest.shade index 7966307c..20903809 100644 --- a/Assets/Scenes/PhysicsTest.shade +++ b/Assets/Scenes/PhysicsTest.shade @@ -10,12 +10,9 @@ IsActive: true RigidBody Component: Type: Dynamic - Auto Mass: false - Mass: 1 Drag: 0.00999999978 Angular Drag: 0.100000001 Use Gravity: true - Gravity Scale: 1 Interpolate: false Sleeping Enabled: true Freeze Position X: false @@ -38,6 +35,10 @@ Rotation Offset: {x: 0, y: 0, z: 0} IsActive: true Scripts: + - Type: PhysicsTestObj + Enabled: true + forceAmount: 50 + torqueAmount: 500 - Type: CollisionTest Enabled: true - EID: 1 @@ -74,10 +75,10 @@ Yaw: 0 Roll: 0 Width: 1920 + Height: 1080 Near: 0.00999999978 Far: 10000 Perspective: true - FOV: 90 IsActive: true Scripts: ~ - EID: 65539 @@ -172,6 +173,43 @@ Rotation Offset: {x: 0, y: 0, z: 0} IsActive: true Scripts: ~ +- EID: 7 + Name: Default + IsActive: true + NumberOfChildren: 0 + Components: + Transform Component: + Translate: {x: 0, y: 0, z: 3} + Rotate: {x: 0, y: 0, z: 0} + Scale: {x: 1, y: 1, z: 1} + IsActive: true + RigidBody Component: + Type: Dynamic + Drag: 0.00999999978 + Angular Drag: 0.100000001 + Use Gravity: true + Interpolate: true + Sleeping Enabled: true + Freeze Position X: false + Freeze Position Y: false + Freeze Position Z: false + Freeze Rotation X: false + Freeze Rotation Y: false + Freeze Rotation Z: false + IsActive: true + Collider Component: + Colliders: + - Is Trigger: false + Collision Tag: 1 + Type: Box + Half Extents: {x: 1, y: 1, z: 1} + Friction: 0.400000006 + Bounciness: 0 + Density: 1 + Position Offset: {x: 0, y: 0, z: 0} + Rotation Offset: {x: 0, y: 0, z: 0} + IsActive: true + Scripts: ~ - EID: 8 Name: Target IsActive: true diff --git a/Assets/Scenes/SS_Playground.shade b/Assets/Scenes/SS_Playground.shade index c151cd81..9acf4216 100644 --- a/Assets/Scenes/SS_Playground.shade +++ b/Assets/Scenes/SS_Playground.shade @@ -4,7 +4,7 @@ NumberOfChildren: 0 Components: Camera Component: - Position: {x: 1, y: 5, z: 5} + Position: {x: 0, y: 2, z: 5} Pitch: 0 Yaw: 0 Roll: 0 @@ -28,9 +28,9 @@ Collider Component: Colliders: - Is Trigger: false - Collision Tag: 6 - Type: Sphere - Radius: 1 + Collision Tag: 0 + Type: Box + Half Extents: {x: 1, y: 1, z: 1} Friction: 0.400000006 Bounciness: 0 Density: 1 @@ -44,9 +44,9 @@ NumberOfChildren: 0 Components: Transform Component: - Translate: {x: 0, y: 5, z: 0} - Rotate: {x: -0, y: 0.785398006, z: -0} - Scale: {x: 1, y: 1, z: 1} + Translate: {x: 0.141888797, y: 5, z: 0} + Rotate: {x: -0, y: 0, z: 0.490080774} + Scale: {x: 0.999999702, y: 0.999999702, z: 1} IsActive: true RigidBody Component: Type: Dynamic @@ -81,4 +81,27 @@ - Type: PhysicsTestObj Enabled: true forceAmount: 50 - torqueAmount: 25 \ No newline at end of file + torqueAmount: 25 +- EID: 2 + Name: Default + IsActive: true + NumberOfChildren: 0 + Components: + Transform Component: + Translate: {x: 0.34412086, y: 3.23541069, z: 0} + Rotate: {x: 0, y: 0, z: 0.490080804} + Scale: {x: 0.999999702, y: 0.999999702, z: 1} + IsActive: true + Collider Component: + Colliders: + - Is Trigger: false + Collision Tag: 0 + Type: Sphere + Radius: 1 + Friction: 0.400000006 + Bounciness: 0 + Density: 1 + Position Offset: {x: 0, y: 0, z: 0} + Rotation Offset: {x: 0, y: 0, z: 0} + IsActive: false + Scripts: ~ \ No newline at end of file diff --git a/Assets/Scripts/Gameplay/AIBehaviour/Implemented/Homeowner1.cs b/Assets/Scripts/Gameplay/AIBehaviour/Implemented/Homeowner1.cs index b57e7fcd..545541cc 100644 --- a/Assets/Scripts/Gameplay/AIBehaviour/Implemented/Homeowner1.cs +++ b/Assets/Scripts/Gameplay/AIBehaviour/Implemented/Homeowner1.cs @@ -1,4 +1,4 @@ -/********************************************************************* +/********************************************************************* * \file Homeowner1.cs * \author Ryan Wang Nian Jing * \brief The implemented behaviour tree for the homeowner diff --git a/Assets/Scripts/Gameplay/AIBehaviour/Implemented/LeafNodes/LeafAttack.cs b/Assets/Scripts/Gameplay/AIBehaviour/Implemented/LeafNodes/LeafAttack.cs index 59470e56..ce510fe9 100644 --- a/Assets/Scripts/Gameplay/AIBehaviour/Implemented/LeafNodes/LeafAttack.cs +++ b/Assets/Scripts/Gameplay/AIBehaviour/Implemented/LeafNodes/LeafAttack.cs @@ -1,4 +1,4 @@ -/********************************************************************* +/********************************************************************* * \file LeafAttack.cs * \author Ryan Wang Nian Jing * \brief Leaf node implementation for AI attacking the player diff --git a/Assets/Scripts/Gameplay/Item/SC_Breakable.cs b/Assets/Scripts/Gameplay/Item/SC_Breakable.cs index a42df17d..207a2d7b 100644 --- a/Assets/Scripts/Gameplay/Item/SC_Breakable.cs +++ b/Assets/Scripts/Gameplay/Item/SC_Breakable.cs @@ -1,4 +1,4 @@ -using SHADE; +using SHADE; using SHADE_Scripting.Audio; using System; using System.Collections.Generic; diff --git a/Assets/Scripts/Gameplay/Item/SC_Item.cs b/Assets/Scripts/Gameplay/Item/SC_Item.cs index b315ab08..bec6bb65 100644 --- a/Assets/Scripts/Gameplay/Item/SC_Item.cs +++ b/Assets/Scripts/Gameplay/Item/SC_Item.cs @@ -1,4 +1,4 @@ -using SHADE; +using SHADE; using SHADE_Scripting.Audio; using System; public class Item : Script diff --git a/Assets/Scripts/Gameplay/SC_JumpPad.cs b/Assets/Scripts/Gameplay/SC_JumpPad.cs index 7aa573df..e9a4db52 100644 --- a/Assets/Scripts/Gameplay/SC_JumpPad.cs +++ b/Assets/Scripts/Gameplay/SC_JumpPad.cs @@ -1,4 +1,4 @@ -using SHADE; +using SHADE; using SHADE_Scripting.Audio; using System; diff --git a/Assets/Scripts/UI/SC_MultiplierTextFx.cs b/Assets/Scripts/UI/SC_MultiplierTextFx.cs index ccd2a4a8..cc445552 100644 --- a/Assets/Scripts/UI/SC_MultiplierTextFx.cs +++ b/Assets/Scripts/UI/SC_MultiplierTextFx.cs @@ -1,4 +1,4 @@ -using SHADE; +using SHADE; using System; using System.Collections.Generic; using System.Linq; diff --git a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp index ae6e79eb..d313bdea 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp +++ b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp @@ -432,8 +432,6 @@ namespace SHADE // TODO: Handle differences between composite & hull collider if (ImGui::BeginMenu("Add Collider")) { - int newColl = -1; - if (ImGui::Selectable("Box Collider")) { auto* compositeCollider = reinterpret_cast(component->GetCollider()); @@ -448,7 +446,10 @@ namespace SHADE ImGui::EndMenu(); } } - else DrawContextMenu(component); + else + { + DrawContextMenu(component); + } ImGui::PopID(); } diff --git a/SHADE_Engine/src/Physics/Collision/SHCollider.cpp b/SHADE_Engine/src/Physics/Collision/SHCollider.cpp index 994a61ed..2f1de000 100644 --- a/SHADE_Engine/src/Physics/Collision/SHCollider.cpp +++ b/SHADE_Engine/src/Physics/Collision/SHCollider.cpp @@ -204,9 +204,9 @@ namespace SHADE for (auto* shape : shapes) { - if (PREV_STATE) // Previously inactive + if (!PREV_STATE) // Previously inactive broadphase->Insert(shape->id, shape->ComputeAABB()); - else // Previously active + else // Previously active broadphase->Remove(shape->id); } } @@ -300,7 +300,7 @@ namespace SHADE }; // Remove from broadphase - if (broadphase) + if (IsActive() && broadphase) broadphase->Remove((*shape)->id); shapeLibrary->DestroyShape(*shape); diff --git a/SHADE_Engine/src/Physics/Collision/SHCollisionSpace.cpp b/SHADE_Engine/src/Physics/Collision/SHCollisionSpace.cpp index 8be6786c..c0fb3724 100644 --- a/SHADE_Engine/src/Physics/Collision/SHCollisionSpace.cpp +++ b/SHADE_Engine/src/Physics/Collision/SHCollisionSpace.cpp @@ -49,11 +49,14 @@ namespace SHADE return; } - collider->broadphase = &broadphase; + if (collider->IsActive()) + { + collider->broadphase = &broadphase; - // Add all existing shapes to the broadphase - for (const auto* shape : collider->shapes) - broadphase.Insert(shape->id, shape->ComputeAABB()); + // Attempt to add all existing shapes to the broadphase + for (const auto* shape : collider->shapes) + broadphase.Insert(shape->id, shape->ComputeAABB()); + } } void SHCollisionSpace::RemoveCollider(SHCollider* collider) noexcept @@ -64,13 +67,17 @@ namespace SHADE if (NUM_SHAPES == 0) return; - for (uint32_t i = 0; i < NUM_SHAPES; ++i) - broadphase.Remove(collider->shapes[i]->id); - - if (contactManager) + // If collider is inactive, skip removals since they were already removed + if (collider->IsActive()) { - contactManager->RemoveInvalidatedTrigger(collider->entityID); - contactManager->RemoveInvalidatedManifold(collider->entityID); + for (uint32_t i = 0; i < NUM_SHAPES; ++i) + broadphase.Remove(collider->shapes[i]->id); + + if (contactManager) + { + contactManager->RemoveInvalidatedTrigger(collider->entityID); + contactManager->RemoveInvalidatedManifold(collider->entityID); + } } /* diff --git a/SHADE_Engine/src/Physics/Collision/SHCompositeCollider.cpp b/SHADE_Engine/src/Physics/Collision/SHCompositeCollider.cpp index 28b6aafe..9b3b08b9 100644 --- a/SHADE_Engine/src/Physics/Collision/SHCompositeCollider.cpp +++ b/SHADE_Engine/src/Physics/Collision/SHCompositeCollider.cpp @@ -100,7 +100,8 @@ namespace SHADE shapes.emplace_back(sphere); - if (broadphase) + // Only add shapes to the broadphase if the collider is active + if (IsActive() && broadphase) broadphase->Insert(NEW_SHAPE_ID, sphere->ComputeAABB()); // Broadcast Event for adding a shape @@ -153,7 +154,7 @@ namespace SHADE shapes.emplace_back(box); - if (broadphase) + if (IsActive() && broadphase) broadphase->Insert(NEW_SHAPE_ID, box->ComputeAABB()); // Broadcast Event for adding a shape diff --git a/SHADE_Engine/src/Physics/Dynamics/SHContactManager.cpp b/SHADE_Engine/src/Physics/Dynamics/SHContactManager.cpp index c9b8dc5f..940c500c 100644 --- a/SHADE_Engine/src/Physics/Dynamics/SHContactManager.cpp +++ b/SHADE_Engine/src/Physics/Dynamics/SHContactManager.cpp @@ -252,22 +252,13 @@ namespace SHADE { // New states start at invalid. In the first frame of collision, move to enter. // If it already in enter, move to stay - if (state == SHCollisionState::ENTER) - state = SHCollisionState::STAY; - - if (state == SHCollisionState::INVALID) - state = SHCollisionState::ENTER; + state = state == SHCollisionState::INVALID ? SHCollisionState::ENTER : SHCollisionState::STAY; } else { // If already exited and still not colliding, the collision has expired. // Invalid states are removed in the next frame - if (state == SHCollisionState::EXIT) - state = SHCollisionState::INVALID; - - // If previously colliding, move to exit. - if (state == SHCollisionState::ENTER || state == SHCollisionState::STAY) - state = SHCollisionState::EXIT; + state = state == SHCollisionState::EXIT ? SHCollisionState::INVALID : SHCollisionState::EXIT; } } diff --git a/SHADE_Engine/src/Physics/Dynamics/SHPhysicsWorld.cpp b/SHADE_Engine/src/Physics/Dynamics/SHPhysicsWorld.cpp index b4e67688..db70579c 100644 --- a/SHADE_Engine/src/Physics/Dynamics/SHPhysicsWorld.cpp +++ b/SHADE_Engine/src/Physics/Dynamics/SHPhysicsWorld.cpp @@ -86,8 +86,6 @@ namespace SHADE void SHPhysicsWorld::Step(float dt) { - - /* * Detect Collisions */ diff --git a/SHADE_Engine/src/Physics/System/Routines/SHPhysicsDebugDrawRoutine.cpp b/SHADE_Engine/src/Physics/System/Routines/SHPhysicsDebugDrawRoutine.cpp index fb2750cc..3fab2440 100644 --- a/SHADE_Engine/src/Physics/System/Routines/SHPhysicsDebugDrawRoutine.cpp +++ b/SHADE_Engine/src/Physics/System/Routines/SHPhysicsDebugDrawRoutine.cpp @@ -57,7 +57,8 @@ namespace SHADE for (const auto& COLLIDER_COMPONENT : COLLIDER_COMPONENT_DENSE) { const auto* COLLIDER = COLLIDER_COMPONENT.GetCollider(); - drawCollider(debugDrawSystem, *COLLIDER); + if (COLLIDER->IsActive()) + drawCollider(debugDrawSystem, *COLLIDER); } } else if (!physicsDebugDrawSystem->collidersToDraw.empty()) @@ -65,7 +66,8 @@ namespace SHADE for (const auto EID : physicsDebugDrawSystem->collidersToDraw) { const auto* COLLIDER = SHComponentManager::GetComponent(EID)->GetCollider(); - drawCollider(debugDrawSystem, *COLLIDER); + if (COLLIDER->IsActive()) + drawCollider(debugDrawSystem, *COLLIDER); } } diff --git a/SHADE_Engine/src/Physics/System/Routines/SHPhysicsPostUpdateRoutine.cpp b/SHADE_Engine/src/Physics/System/Routines/SHPhysicsPostUpdateRoutine.cpp index c884c01a..f779bb8b 100644 --- a/SHADE_Engine/src/Physics/System/Routines/SHPhysicsPostUpdateRoutine.cpp +++ b/SHADE_Engine/src/Physics/System/Routines/SHPhysicsPostUpdateRoutine.cpp @@ -87,10 +87,6 @@ namespace SHADE transformComponent->SetWorldPosition(MOTION_STATE.position); transformComponent->SetWorldOrientation(MOTION_STATE.orientation); } - - /* - * TODO: Test if the scene graph transforms abides by setting world position. Collisions will ignore the scene graph hierarchy. - */ } } } diff --git a/SHADE_Engine/src/Physics/System/Routines/SHPhysicsPreUpdateRoutine.cpp b/SHADE_Engine/src/Physics/System/Routines/SHPhysicsPreUpdateRoutine.cpp index 3f2de93f..b68406c0 100644 --- a/SHADE_Engine/src/Physics/System/Routines/SHPhysicsPreUpdateRoutine.cpp +++ b/SHADE_Engine/src/Physics/System/Routines/SHPhysicsPreUpdateRoutine.cpp @@ -52,8 +52,19 @@ namespace SHADE const bool RIGIDBODY_ACTIVE = physicsObject.rigidBody->IsActive(); if (IS_ACTIVE != RIGIDBODY_ACTIVE) + { physicsObject.rigidBody->SetIsActive(IS_ACTIVE); + // If previously active, we remove it from the world. + // If previously inactive, we add it back to the world. + // This removes the need to check for inactive objects in the world every frame to remove invalid contacts. + + if (IS_ACTIVE) // Previously inactive + physicsSystem->physicsWorld->AddRigidBody(physicsObject.rigidBody); + else // Previously active + physicsSystem->physicsWorld->RemoveRigidBody(physicsObject.rigidBody); + } + if (UPDATE_TRANSFORM) { const SHVec3& WORLD_POS = TRANSFORM_COMPONENT->GetWorldPosition(); @@ -71,8 +82,19 @@ namespace SHADE const bool COLLIDER_ACTIVE = physicsObject.collider->IsActive(); if (IS_ACTIVE != COLLIDER_ACTIVE) + { physicsObject.collider->SetIsActive(IS_ACTIVE); + // If previously active, we remove it from the world. + // If previously inactive, we add it back to the world. + // This removes the need to check for inactive objects in the world every frame to remove invalid contacts. + + if (IS_ACTIVE) // Previously inactive + physicsSystem->collisionSpace->AddCollider(physicsObject.collider); + else // Previously active + physicsSystem->collisionSpace->RemoveCollider(physicsObject.collider); + } + if (UPDATE_TRANSFORM) { const SHVec3& WORLD_POS = TRANSFORM_COMPONENT->GetWorldPosition();