diff --git a/SHADE_Application/premake5.lua b/SHADE_Application/premake5.lua index 014df726..c2312805 100644 --- a/SHADE_Application/premake5.lua +++ b/SHADE_Application/premake5.lua @@ -78,10 +78,7 @@ project "SHADE_Application" "26451", "26437", "4275", - "4633", - "4634", - "4635", - "4638" + "4635" } linkoptions { "-IGNORE:4006" } @@ -90,7 +87,7 @@ project "SHADE_Application" filter "configurations:Debug" symbols "On" - defines {"_DEBUG", "SHEDITOR"} + defines {"_DEBUG"} filter "configurations:Release" optimize "On" diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 886b3b13..5062b4de 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -85,6 +85,7 @@ namespace Sandbox SHSystemManager::CreateSystem(); SHGraphicsSystem* graphicsSystem = static_cast(SHSystemManager::GetSystem()); + SHPhysicsSystem* physicsSystem = SHSystemManager::GetSystem(); // Link up SHDebugDraw SHSystemManager::CreateSystem(); @@ -176,11 +177,15 @@ namespace Sandbox #ifdef SHEDITOR if(editor->editorState == SHEditor::State::PLAY) - SHSceneManager::SceneUpdate(0.016f); - SHSystemManager::RunRoutines(editor->editorState != SHEditor::State::PLAY, 0.016f); - editor->PollPicking(); -#endif +#endif + SHSceneManager::SceneUpdate(0.016f); +#ifdef SHEDITOR + SHSystemManager::RunRoutines(editor->editorState != SHEditor::State::PLAY, SHFrameRateController::GetRawDeltaTime()); + editor->PollPicking(); +#else + SHSystemManager::RunRoutines(false, SHFrameRateController::GetRawDeltaTime()); +#endif // TODO: Move into an Editor menu static bool drawColliders = false; if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::F10)) @@ -194,6 +199,13 @@ namespace Sandbox drawRays = !drawRays; SHSystemManager::GetSystem()->SetDebugDrawFlag(SHPhysicsDebugDrawSystem::DebugDrawFlags::RAYCASTS, drawRays); } + static bool drawContacts = false; + if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::F9)) + { + drawContacts = !drawContacts; + SHSystemManager::GetSystem()->SetDebugDrawFlag(SHPhysicsDebugDrawSystem::DebugDrawFlags::CONTACT_POINTS, drawContacts); + SHSystemManager::GetSystem()->SetDebugDrawFlag(SHPhysicsDebugDrawSystem::DebugDrawFlags::CONTACT_NORMALS, drawContacts); + } } // Finish all graphics jobs first graphicsSystem->AwaitGraphicsExecution(); diff --git a/SHADE_Application/src/Scenes/SBMainScene.cpp b/SHADE_Application/src/Scenes/SBMainScene.cpp index a0e80556..929d28fa 100644 --- a/SHADE_Application/src/Scenes/SBMainScene.cpp +++ b/SHADE_Application/src/Scenes/SBMainScene.cpp @@ -7,6 +7,7 @@ #include "Graphics/MiddleEnd/Interface/SHRenderable.h" #include "Scene/SHSceneManager.h" #include "Graphics/MiddleEnd/Interface/SHGraphicsSystem.h" +#include "Physics/System/SHPhysicsSystem.h" #include "Scripting/SHScriptEngine.h" #include "Math/Transform/SHTransformComponent.h" #include "Graphics/MiddleEnd/Interface/SHMaterialInstance.h" @@ -42,6 +43,28 @@ namespace Sandbox void SBMainScene::Init() { sceneName = SHSerialization::DeserializeSceneFromFile(sceneAssetID); + + auto* physicsSystem = SHSystemManager::GetSystem(); + if (!physicsSystem) + { + SHLOGV_CRITICAL("Failed to get the physics system for building the scene!") + return; + } + + physicsSystem->BuildScene(SHSceneManager::GetCurrentSceneGraph()); + + /*-----------------------------------------------------------------------*/ + /* TESTING CODE */ + /*-----------------------------------------------------------------------*/ + //testText = SHEntityManager::CreateEntity(MAX_EID, "Test Text"); + //auto gfxSystem =SHSystemManager::GetSystem(); + + //auto textComp = SHComponentManager::GetComponent(testText); + + //textComp->SetFont(gfxSystem->GetFontLibrary().GetFonts()[0]); + /*-----------------------------------------------------------------------*/ + /* TESTING CODE */ + /*-----------------------------------------------------------------------*/ } void SBMainScene::Update(float dt) diff --git a/SHADE_Engine/premake5.lua b/SHADE_Engine/premake5.lua index 7fb7291d..88df50d7 100644 --- a/SHADE_Engine/premake5.lua +++ b/SHADE_Engine/premake5.lua @@ -79,10 +79,7 @@ project "SHADE_Engine" "26451", "26437", "4275", - "4633", - "4634", - "4635", - "4638" + "4635" } linkoptions { "-IGNORE:4006" } diff --git a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp index 386f0988..80ae3331 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp +++ b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp @@ -252,7 +252,7 @@ namespace SHADE if(rbType == SHRigidBodyComponent::Type::DYNAMIC) //Dynamic only fields { SHEditorWidgets::CheckBox("Use Gravity", [component]{return component->IsGravityEnabled();}, [component](bool const& value){component->SetGravityEnabled(value);}, "Gravity"); - SHEditorWidgets::DragFloat("Mass", [component] {return component->GetMass(); }, [component](float const& value) {component->SetMass(value); }, "Mass"); + //SHEditorWidgets::DragFloat("Mass", [component] {return component->GetMass(); }, [component](float const& value) {component->SetMass(value); }, "Mass"); } if (rbType == SHRigidBodyComponent::Type::DYNAMIC || rbType == SHRigidBodyComponent::Type::KINEMATIC) //Dynamic or Kinematic only fields { @@ -284,6 +284,7 @@ namespace SHADE //Debug Info (Read-Only) if(ImGui::CollapsingHeader("Debug Information", ImGuiTreeNodeFlags_DefaultOpen))//Dynamic or Kinematic only fields { + SHEditorWidgets::DragFloat("Mass", [component] { return component->GetMass(); }, [](float value){}, "Mass", 0.1f, 0.0f, std::numeric_limits::infinity(), "%.3f", ImGuiSliderFlags_ReadOnly); SHEditorWidgets::DragVec3("Position", { "X", "Y", "Z" }, [component] {return component->GetPosition(); }, [](SHVec3 const& value) {}, false, "Position", 0.1f, "%.3f", 0.0f, 0.0f, ImGuiSliderFlags_ReadOnly); SHEditorWidgets::DragVec3("Rotation", { "X", "Y", "Z" }, [component] {return component->GetRotation(); }, [](SHVec3 const& value) {}, false, "Rotation", 0.1f, "%.3f", 0.0f, 0.0f, ImGuiSliderFlags_ReadOnly); if (rbType == SHRigidBodyComponent::Type::DYNAMIC || rbType == SHRigidBodyComponent::Type::KINEMATIC) //Dynamic or Kinematic only fields diff --git a/SHADE_Engine/src/Physics/Interface/SHColliderComponent.cpp b/SHADE_Engine/src/Physics/Interface/SHColliderComponent.cpp index 877d238f..135e7e42 100644 --- a/SHADE_Engine/src/Physics/Interface/SHColliderComponent.cpp +++ b/SHADE_Engine/src/Physics/Interface/SHColliderComponent.cpp @@ -80,6 +80,14 @@ namespace SHADE } system = physicsSystem; + + // Sync with transform if one already exists + if (auto* transformComponent = SHComponentManager::GetComponent_s(GetEID()); transformComponent) + { + position = transformComponent->GetWorldPosition(); + orientation = transformComponent->GetWorldOrientation(); + scale = transformComponent->GetWorldScale(); + } } void SHColliderComponent::OnDestroy() diff --git a/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.cpp b/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.cpp index 28b6f842..4fddc892 100644 --- a/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.cpp +++ b/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.cpp @@ -322,25 +322,25 @@ namespace SHADE // dirtyFlags |= 1U << FLAG_POS; //} - void SHRigidBodyComponent::SetMass(float newMass) noexcept - { - static constexpr int FLAG_POS = 9; + //void SHRigidBodyComponent::SetMass(float newMass) noexcept + //{ + // static constexpr int FLAG_POS = 9; - if (newMass < 0.0f) - return; + // if (newMass < 0.0f) + // return; - if (type != Type::DYNAMIC) - { - SHLOG_WARNING("Cannot set mass of a non-dynamic object {}", GetEID()) - return; - } + // if (type != Type::DYNAMIC) + // { + // SHLOG_WARNING("Cannot set mass of a non-dynamic object {}", GetEID()) + // return; + // } - dirtyFlags |= 1U << FLAG_POS; - mass = newMass; + // dirtyFlags |= 1U << FLAG_POS; + // mass = newMass; - // Turn off automass - flags &= ~(1U << FLAG_POS); - } + // // Turn off automass + // flags &= ~(1U << FLAG_POS); + //} void SHRigidBodyComponent::SetDrag(float newDrag) noexcept { @@ -411,6 +411,13 @@ namespace SHADE } system = physicsSystem; + + // Sync with transform if one already exists + if (auto* transformComponent = SHComponentManager::GetComponent_s(GetEID()); transformComponent) + { + position = transformComponent->GetWorldPosition(); + orientation = transformComponent->GetWorldOrientation(); + } } void SHRigidBodyComponent::AddForce(const SHVec3& force) const noexcept @@ -489,7 +496,7 @@ RTTR_REGISTRATION registration::class_("RigidBody Component") .property("Type" , &SHRigidBodyComponent::GetType , &SHRigidBodyComponent::SetType ) - .property("Mass" , &SHRigidBodyComponent::GetMass , &SHRigidBodyComponent::SetMass ) + //.property("Mass" , &SHRigidBodyComponent::GetMass , &SHRigidBodyComponent::SetMass ) .property("Drag" , &SHRigidBodyComponent::GetDrag , &SHRigidBodyComponent::SetDrag ) .property("Angular Drag" , &SHRigidBodyComponent::GetAngularDrag , &SHRigidBodyComponent::SetAngularDrag ) .property("Use Gravity" , &SHRigidBodyComponent::IsGravityEnabled , &SHRigidBodyComponent::SetGravityEnabled ) diff --git a/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.h b/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.h index d5204d94..532b3312 100644 --- a/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.h +++ b/SHADE_Engine/src/Physics/Interface/SHRigidBodyComponent.h @@ -114,7 +114,7 @@ namespace SHADE void SetInterpolate (bool allowInterpolation) noexcept; //void SetAutoMass (bool autoMass) noexcept; - void SetMass (float newMass) noexcept; + //void SetMass (float newMass) noexcept; void SetDrag (float newDrag) noexcept; void SetAngularDrag (float newAngularDrag) noexcept; diff --git a/SHADE_Engine/src/Physics/PhysicsObject/SHPhysicsObject.cpp b/SHADE_Engine/src/Physics/PhysicsObject/SHPhysicsObject.cpp index 8de928f0..00f280e9 100644 --- a/SHADE_Engine/src/Physics/PhysicsObject/SHPhysicsObject.cpp +++ b/SHADE_Engine/src/Physics/PhysicsObject/SHPhysicsObject.cpp @@ -94,7 +94,7 @@ namespace SHADE /* Public Function Member Definitions */ /*-----------------------------------------------------------------------------------*/ - int SHPhysicsObject::AddCollisionShape(int index) const + int SHPhysicsObject::AddCollisionShape(int index) { // Get collider component auto* colliderComponent = SHComponentManager::GetComponent_s(entityID); @@ -123,13 +123,19 @@ namespace SHADE default: break; } - rp3dBody->updateLocalCenterOfMassFromColliders(); - rp3dBody->updateLocalInertiaTensorFromColliders(); + if (rp3dBody->getType() == rp3d::BodyType::DYNAMIC) + { + rp3dBody->updateMassPropertiesFromColliders(); + + auto* rigidBodyComponent = SHComponentManager::GetComponent_s(entityID); + if (rigidBodyComponent) + rigidBodyComponent->mass = rp3dBody->getMass(); + } return index; } - void SHPhysicsObject::RemoveCollisionShape(int index) const + void SHPhysicsObject::RemoveCollisionShape(int index) { const int NUM_COLLIDERS = static_cast(rp3dBody->getNbColliders()); if (NUM_COLLIDERS == 0) @@ -140,6 +146,15 @@ namespace SHADE auto* collider = rp3dBody->getCollider(index); rp3dBody->removeCollider(collider); + + if (rp3dBody->getType() == rp3d::BodyType::DYNAMIC) + { + rp3dBody->updateMassPropertiesFromColliders(); + + auto* rigidBodyComponent = SHComponentManager::GetComponent_s(entityID); + if (rigidBodyComponent) + rigidBodyComponent->mass = rp3dBody->getMass(); + } } void SHPhysicsObject::RemoveAllCollisionShapes() const noexcept @@ -254,9 +269,7 @@ namespace SHADE } case 9: // Mass { - rp3dBody->setMass(component.mass); - rp3dBody->updateLocalCenterOfMassFromColliders(); - rp3dBody->updateLocalInertiaTensorFromColliders(); + //rp3dBody->setMass(component.mass); //if (component.GetAutoMass()) //{ diff --git a/SHADE_Engine/src/Physics/PhysicsObject/SHPhysicsObject.h b/SHADE_Engine/src/Physics/PhysicsObject/SHPhysicsObject.h index fefc983f..818e5471 100644 --- a/SHADE_Engine/src/Physics/PhysicsObject/SHPhysicsObject.h +++ b/SHADE_Engine/src/Physics/PhysicsObject/SHPhysicsObject.h @@ -71,8 +71,8 @@ namespace SHADE /* Function Members */ /*---------------------------------------------------------------------------------*/ - int AddCollisionShape (int index) const; - void RemoveCollisionShape (int index) const; + int AddCollisionShape (int index); + void RemoveCollisionShape (int index); void RemoveAllCollisionShapes () const noexcept; void SyncRigidBody (SHRigidBodyComponent& component) const noexcept; @@ -98,12 +98,12 @@ namespace SHADE // Box Shapes - void addBoxShape (SHCollisionShape& boxShape) const noexcept; - void syncBoxShape (int index, SHCollisionShape& boxShape) const noexcept; + void addBoxShape (SHCollisionShape& boxShape) const noexcept; + void syncBoxShape (int index, SHCollisionShape& boxShape) const noexcept; // Sphere Shapes - void addSphereShape (SHCollisionShape& sphereShape) const noexcept; - void syncSphereShape (int index, SHCollisionShape& sphereShape) const noexcept; + void addSphereShape (SHCollisionShape& sphereShape) const noexcept; + void syncSphereShape (int index, SHCollisionShape& sphereShape) const noexcept; }; } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp b/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp index bf1debac..3c80883c 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp +++ b/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp @@ -15,6 +15,7 @@ // Project Headers #include "ECS_Base/Managers/SHSystemManager.h" +#include "Editor/SHEditor.h" #include "Scene/SHSceneManager.h" namespace SHADE @@ -118,11 +119,22 @@ namespace SHADE return; } + rp3d::DebugRenderer* rp3dRenderer = nullptr; + #ifdef SHEDITOR + const auto* EDITOR = SHSystemManager::GetSystem(); + if (EDITOR && EDITOR->editorState != SHEditor::State::STOP) + { + rp3dRenderer = &system->physicsSystem->worldState.world->getDebugRenderer(); + rp3dRenderer->setIsDebugItemDisplayed(rp3d::DebugRenderer::DebugItem::CONTACT_POINT, false); + rp3dRenderer->setIsDebugItemDisplayed(rp3d::DebugRenderer::DebugItem::CONTACT_NORMAL, false); + } + #endif + for (int i = 0; i < SHUtilities::ConvertEnum(DebugDrawFlags::NUM_FLAGS); ++i) { const bool DRAW = (system->debugDrawFlags & (1U << i)) > 0; if (DRAW) - drawFunctions[i](debugDrawSystem); + drawFunctions[i](debugDrawSystem, rp3dRenderer); } // Automatically clear the container of raycasts despite debug drawing state @@ -134,7 +146,7 @@ namespace SHADE /* Private Function Member Definitions */ /*-----------------------------------------------------------------------------------*/ - void SHPhysicsDebugDrawSystem::drawColliders(SHDebugDrawSystem* debugRenderer) noexcept + void SHPhysicsDebugDrawSystem::drawColliders(SHDebugDrawSystem* debugRenderer, rp3d::DebugRenderer* rp3dRenderer) noexcept { const auto& COLLIDER_SET = SHComponentManager::GetDense(); for (const auto& COLLIDER : COLLIDER_SET) @@ -155,27 +167,53 @@ namespace SHADE } } - void SHPhysicsDebugDrawSystem::drawColliderAABBs(SHDebugDrawSystem* debugRenderer) noexcept + void SHPhysicsDebugDrawSystem::drawColliderAABBs(SHDebugDrawSystem* debugRenderer, rp3d::DebugRenderer* rp3dRenderer) noexcept { } - void SHPhysicsDebugDrawSystem::drawBroadPhaseAABBs(SHDebugDrawSystem* debugRenderer) noexcept + void SHPhysicsDebugDrawSystem::drawBroadPhaseAABBs(SHDebugDrawSystem* debugRenderer, rp3d::DebugRenderer* rp3dRenderer) noexcept { } - void SHPhysicsDebugDrawSystem::drawContactPoints(SHDebugDrawSystem* debugRenderer) noexcept + void SHPhysicsDebugDrawSystem::drawContactPoints(SHDebugDrawSystem* debugRenderer, rp3d::DebugRenderer* rp3dRenderer) noexcept { - + #ifdef SHEDITOR + const auto* EDITOR = SHSystemManager::GetSystem(); + if (EDITOR && EDITOR->editorState != SHEditor::State::STOP) + { + rp3dRenderer->setIsDebugItemDisplayed(rp3d::DebugRenderer::DebugItem::CONTACT_POINT, true); + const int NUM_TRIS = static_cast(rp3dRenderer->getNbTriangles()); + if (NUM_TRIS == 0) + return; + + const auto& TRI_ARRAY = rp3dRenderer->getTrianglesArray(); + for (int i = 0; i < NUM_TRIS; ++i) + debugRenderer->DrawTri(SHColour::RED, TRI_ARRAY[i].point1, TRI_ARRAY[i].point2, TRI_ARRAY[i].point3); + } + #endif } - void SHPhysicsDebugDrawSystem::drawContactNormals(SHDebugDrawSystem* debugRenderer) noexcept + void SHPhysicsDebugDrawSystem::drawContactNormals(SHDebugDrawSystem* debugRenderer, rp3d::DebugRenderer* rp3dRenderer) noexcept { - + #ifdef SHEDITOR + const auto* EDITOR = SHSystemManager::GetSystem(); + if (EDITOR && EDITOR->editorState != SHEditor::State::STOP) + { + rp3dRenderer->setIsDebugItemDisplayed(rp3d::DebugRenderer::DebugItem::CONTACT_NORMAL, true); + const int NUM_LINES = static_cast(rp3dRenderer->getNbLines()); + if (NUM_LINES == 0) + return; + + const auto& LINE_ARRAY = rp3dRenderer->getLinesArray(); + for (int i = 0; i < NUM_LINES; ++i) + debugRenderer->DrawLine(SHColour::RED, LINE_ARRAY[i].point1, LINE_ARRAY[i].point2); + } + #endif } - void SHPhysicsDebugDrawSystem::drawRaycasts(SHDebugDrawSystem* debugRenderer) noexcept + void SHPhysicsDebugDrawSystem::drawRaycasts(SHDebugDrawSystem* debugRenderer, rp3d::DebugRenderer* rp3dRenderer) noexcept { auto* physicsSystem = SHSystemManager::GetSystem(); if (!physicsSystem) diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.h b/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.h index 867a6e11..dc703092 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.h +++ b/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.h @@ -93,7 +93,7 @@ namespace SHADE /* Type Definitions */ /*---------------------------------------------------------------------------------*/ - using DebugDrawFunction = void(*)(SHDebugDrawSystem*) noexcept; + using DebugDrawFunction = void(*)(SHDebugDrawSystem*, rp3d::DebugRenderer*) noexcept; /*---------------------------------------------------------------------------------*/ /* Data Members */ @@ -118,12 +118,12 @@ namespace SHADE // Generic Draw Functions - static void drawColliders (SHDebugDrawSystem* debugRenderer) noexcept; - static void drawColliderAABBs (SHDebugDrawSystem* debugRenderer) noexcept; - static void drawBroadPhaseAABBs (SHDebugDrawSystem* debugRenderer) noexcept; - static void drawContactPoints (SHDebugDrawSystem* debugRenderer) noexcept; - static void drawContactNormals (SHDebugDrawSystem* debugRenderer) noexcept; - static void drawRaycasts (SHDebugDrawSystem* debugRenderer) noexcept; + static void drawColliders (SHDebugDrawSystem* debugRenderer, rp3d::DebugRenderer* rp3dRenderer) noexcept; + static void drawColliderAABBs (SHDebugDrawSystem* debugRenderer, rp3d::DebugRenderer* rp3dRenderer) noexcept; + static void drawBroadPhaseAABBs (SHDebugDrawSystem* debugRenderer, rp3d::DebugRenderer* rp3dRenderer) noexcept; + static void drawContactPoints (SHDebugDrawSystem* debugRenderer, rp3d::DebugRenderer* rp3dRenderer) noexcept; + static void drawContactNormals (SHDebugDrawSystem* debugRenderer, rp3d::DebugRenderer* rp3dRenderer) noexcept; + static void drawRaycasts (SHDebugDrawSystem* debugRenderer, rp3d::DebugRenderer* rp3dRenderer) noexcept; // Shape Generation Functions diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp index 396edd93..89be2614 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp +++ b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp @@ -97,6 +97,13 @@ namespace SHADE defaultCollisionTagNameFilePath.append("CollisionTags.SHConfig"); SHCollisionTagMatrix::Init(defaultCollisionTagNameFilePath); + // Link Physics Object Manager with System & Raycaster + objectManager.SetFactory(factory); + raycaster.SetObjectManager(&objectManager); + + // Link Collision Listener with System + collisionListener.BindToSystem(this); + // Subscribe to component events const std::shared_ptr ADD_COMPONENT_RECEIVER { std::make_shared>(this, &SHPhysicsSystem::addPhysicsComponent) }; const ReceiverPtr ADD_COMPONENT_RECEIVER_PTR = std::dynamic_pointer_cast(ADD_COMPONENT_RECEIVER); @@ -118,12 +125,7 @@ namespace SHADE SHEventManager::SubscribeTo(SH_EDITOR_ON_STOP_EVENT, ON_STOP_RECEIVER_PTR); #endif - // Link Physics Object Manager with System & Raycaster - objectManager.SetFactory(factory); - raycaster.SetObjectManager(&objectManager); - - // Link Collision Listener with System - collisionListener.BindToSystem(this); + } void SHPhysicsSystem::Exit() @@ -136,6 +138,55 @@ namespace SHADE SHCollisionTagMatrix::Exit(defaultCollisionTagNameFilePath); } + void SHPhysicsSystem::BuildScene(SHSceneGraph& sceneGraph) + { + static const auto BUILD_NEW_SCENE_PHYSICS_OBJECT = [&](SHSceneNode* node) + { + const EntityID EID = node->GetEntityID(); + + if (SHComponentManager::HasComponent(EID)) + objectManager.AddRigidBody(EID); + + if (SHComponentManager::HasComponent(EID)) + objectManager.AddCollider(EID); + }; + + //////////////////////////////// + + // Destroy an existing world + if (worldState.world != nullptr) + { + objectManager.RemoveAllObjects(); + objectManager.SetWorld(nullptr); + + collisionListener.ClearContainers(); + raycaster.ClearFrame(); + + worldState.DestroyWorld(factory); + } + + worldState.CreateWorld(factory); + #ifdef _PUBLISH + worldState.world->setIsDebugRenderingEnabled(false); + #else + worldState.world->setIsDebugRenderingEnabled(true); + #endif + + // Link Collision Listener & Raycaster + collisionListener.BindToWorld(worldState.world); + raycaster.BindToWorld(worldState.world); + + // Link with object manager & create all physics objects + objectManager.SetWorld(worldState.world); + + // When building a scene, clear the object manager command queue and build scene objects again. + // This is done to avoid duplicate adds. + while (!objectManager.commandQueue.empty()) + objectManager.commandQueue.pop(); + + sceneGraph.Traverse(BUILD_NEW_SCENE_PHYSICS_OBJECT); + } + void SHPhysicsSystem::ForceUpdate() { if (!worldState.world) @@ -228,10 +279,13 @@ namespace SHADE { objectManager.AddCollisionShape(entityID, index); + auto* colliderComponent = SHComponentManager::GetComponent(entityID); + auto& collisionShape = colliderComponent->GetCollisionShape(index); + const SHPhysicsColliderAddedEvent COLLIDER_ADDED_EVENT_DATA { .entityID = entityID - , .colliderType = SHComponentManager::GetComponent(entityID)->GetCollisionShape(index).GetType() + , .colliderType = collisionShape.GetType() , .colliderIndex = index }; @@ -367,6 +421,11 @@ namespace SHADE return onPlayEvent->handle; worldState.CreateWorld(factory); + #ifdef _PUBLISH + worldState.world->setIsDebugRenderingEnabled(false); + #else + worldState.world->setIsDebugRenderingEnabled(true); + #endif // Link Collision Listener & Raycaster collisionListener.BindToWorld(worldState.world); @@ -375,8 +434,8 @@ namespace SHADE // Link with object manager & create all physics objects objectManager.SetWorld(worldState.world); - const auto& SCENE_GRAPH = SHSceneManager::GetCurrentSceneGraph(); - SCENE_GRAPH.Traverse(BUILD_PHYSICS_OBJECT); + // Build scene + SHSceneManager::GetCurrentSceneGraph().Traverse(BUILD_PHYSICS_OBJECT); return onPlayEvent->handle; } @@ -390,11 +449,11 @@ namespace SHADE // Clear all collision info // Collision listener is automatically unbound when world is destroyed collisionListener.ClearContainers(); + raycaster.ClearFrame(); // Destroy the world worldState.DestroyWorld(factory); return onStopEvent->handle; } - } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h index f92be4cd..f7340d31 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h +++ b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h @@ -27,7 +27,7 @@ #include "Physics/Interface/SHColliderComponent.h" #include "Physics/PhysicsObject/SHPhysicsObjectManager.h" #include "Physics/SHPhysicsWorld.h" - +#include "Scene/SHSceneGraph.h" namespace SHADE { @@ -74,10 +74,12 @@ namespace SHADE /* Function Members */ /*---------------------------------------------------------------------------------*/ - void Init () override; - void Exit () override; + void Init () override; + void Exit () override; + + void BuildScene (SHSceneGraph& sceneGraph); + void ForceUpdate (); - void ForceUpdate (); /** * @brief Casts a ray into the world. @@ -280,11 +282,11 @@ namespace SHADE /* Function Members */ /*---------------------------------------------------------------------------------*/ - SHEventHandle addPhysicsComponent (SHEventPtr addComponentEvent) noexcept; - SHEventHandle removePhysicsComponent (SHEventPtr removeComponentEvent) noexcept; - - SHEventHandle onPlay (SHEventPtr onPlayEvent); - SHEventHandle onStop (SHEventPtr onStopEvent); + SHEventHandle addPhysicsComponent (SHEventPtr addComponentEvent) noexcept; + SHEventHandle removePhysicsComponent (SHEventPtr removeComponentEvent) noexcept; + SHEventHandle onPlay (SHEventPtr onPlayEvent); + SHEventHandle onStop (SHEventPtr onStopEvent); + SHEventHandle buildScene (SHEventPtr onSceneChangeEvent); }; } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Scene/SHSceneGraphEvents.h b/SHADE_Engine/src/Scene/SHSceneEvents.h similarity index 72% rename from SHADE_Engine/src/Scene/SHSceneGraphEvents.h rename to SHADE_Engine/src/Scene/SHSceneEvents.h index ccdf06be..c0d7dbc1 100644 --- a/SHADE_Engine/src/Scene/SHSceneGraphEvents.h +++ b/SHADE_Engine/src/Scene/SHSceneEvents.h @@ -1,7 +1,7 @@ /**************************************************************************************** - * \file SHSceneGraphEvents.h + * \file SHSceneEvents.h * \author Diren D Bharwani, diren.dbharwani, 390002520 - * \brief Interface for Scene Graph Events. + * \brief Interface for Scene Events. * * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or * disclosure of this file or its contents without the prior written consent @@ -21,21 +21,21 @@ namespace SHADE struct SHSceneGraphChangeParentEvent { - SHSceneNode* node; - SHSceneNode* oldParent; - SHSceneNode* newParent; + SHSceneNode* node = nullptr; + SHSceneNode* oldParent = nullptr; + SHSceneNode* newParent = nullptr; }; struct SHSceneGraphAddChildEvent { - SHSceneNode* parent; - SHSceneNode* childAdded; + SHSceneNode* parent = nullptr; + SHSceneNode* childAdded = nullptr; }; struct SHSceneGraphRemoveChildEvent { - SHSceneNode* parent; - SHSceneNode* childRemoved; + SHSceneNode* parent = nullptr; + SHSceneNode* childRemoved = nullptr; }; } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Scene/SHSceneGraph.h b/SHADE_Engine/src/Scene/SHSceneGraph.h index 5747be7b..37d0e063 100644 --- a/SHADE_Engine/src/Scene/SHSceneGraph.h +++ b/SHADE_Engine/src/Scene/SHSceneGraph.h @@ -16,7 +16,7 @@ #include "ECS_Base/Entity/SHEntity.h" #include "SH_API.h" #include "SHSceneNode.h" -#include "SHSceneGraphEvents.h" +#include "SHSceneEvents.h" namespace SHADE { diff --git a/SHADE_Engine/src/Scene/SHSceneManager.cpp b/SHADE_Engine/src/Scene/SHSceneManager.cpp index be9c7755..110aaea6 100644 --- a/SHADE_Engine/src/Scene/SHSceneManager.cpp +++ b/SHADE_Engine/src/Scene/SHSceneManager.cpp @@ -85,7 +85,6 @@ namespace SHADE currentScene->Load(); currentScene->Init(); } - } else // restarting scene { diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp index 84b50373..90121994 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp @@ -27,7 +27,7 @@ of DigiPen Institute of Technology is prohibited. #include "Events/SHEventManager.hpp" #include "Physics/System/SHPhysicsSystem.h" #include "Physics/SHPhysicsEvents.h" -#include "Scene/SHSceneGraphEvents.h" +#include "Scene/SHSceneEvents.h" #include "Assets/SHAssetMacros.h" diff --git a/SHADE_Engine/src/Serialization/SHYAMLConverters.h b/SHADE_Engine/src/Serialization/SHYAMLConverters.h index eb273a15..06c785ae 100644 --- a/SHADE_Engine/src/Serialization/SHYAMLConverters.h +++ b/SHADE_Engine/src/Serialization/SHYAMLConverters.h @@ -116,6 +116,7 @@ namespace YAML static constexpr const char* Bounciness = "Bounciness"; static constexpr const char* Density = "Density"; static constexpr const char* PositionOffset = "Position Offset"; + static constexpr const char* RotationOffset = "Rotation Offset"; static Node encode(SHCollisionShape& rhs) { @@ -151,6 +152,7 @@ namespace YAML node[Bounciness] = rhs.GetBounciness(); node[Density] = rhs.GetDensity(); node[PositionOffset] = rhs.GetPositionOffset(); + node[RotationOffset] = rhs.GetRotationOffset(); return node; } @@ -191,6 +193,8 @@ namespace YAML rhs.SetDensity(node[Density].as()); if (node[PositionOffset].IsDefined()) rhs.SetPositionOffset(node[PositionOffset].as()); + if (node[RotationOffset].IsDefined()) + rhs.SetRotationOffset(node[RotationOffset].as()); return true; } diff --git a/SHADE_Managed/src/Components/RigidBody.cxx b/SHADE_Managed/src/Components/RigidBody.cxx index cdaa296a..a564402f 100644 --- a/SHADE_Managed/src/Components/RigidBody.cxx +++ b/SHADE_Managed/src/Components/RigidBody.cxx @@ -58,7 +58,7 @@ namespace SHADE } void RigidBody::Mass::set(float value) { - return GetNativeComponent()->SetMass(value); + /*return GetNativeComponent()->SetMass(value);*/ } float RigidBody::Drag::get() {