diff --git a/Assets/Scenes/NavigationTest.shade b/Assets/Scenes/NavigationTest.shade new file mode 100644 index 00000000..cd358faa --- /dev/null +++ b/Assets/Scenes/NavigationTest.shade @@ -0,0 +1,79 @@ +- EID: 0 + Name: Camera + IsActive: true + NumberOfChildren: 0 + Components: + Transform Component: + Translate: {x: 0, y: 0, z: 0} + Rotate: {x: 0, y: 0, z: 0} + Scale: {x: 1, y: 1, z: 1} + IsActive: true + Camera Component: + Position: {x: 0, y: 0, z: 0} + Pitch: 0 + Yaw: 0 + Roll: 0 + Width: 1920 + Near: 0.00999999978 + Far: 10000 + Perspective: true + FOV: 90 + IsActive: true + Scripts: ~ +- EID: 1 + Name: Floor + IsActive: true + NumberOfChildren: 0 + Components: + Transform Component: + Translate: {x: 0, y: 0, z: 0} + Rotate: {x: -0, y: 0, z: -0} + Scale: {x: 12.8000002, y: 1, z: 12.8000002} + IsActive: true + Renderable Component: + Mesh: 142686872 + Material: 126223465 + IsActive: true + Scripts: ~ +- EID: 2 + Name: Light + IsActive: true + NumberOfChildren: 0 + Components: + Light Component: + Position: {x: 0, y: 0, z: 0} + Type: Directional + Direction: {x: 0, y: -1, z: 1} + Color: {x: 1, y: 1, z: 1, w: 1} + Layer: 4294967295 + Strength: 0 + Casting Shadows: false + IsActive: true + Scripts: ~ +- EID: 3 + Name: Wall + IsActive: true + NumberOfChildren: 0 + Components: + Transform Component: + Translate: {x: 0, y: 0, z: 0} + Rotate: {x: -0, y: 0, z: -0} + Scale: {x: 1, y: 1, z: 1} + IsActive: true + Renderable Component: + Mesh: 142652392 + Material: 126223465 + IsActive: true + Collider Component: + Colliders: + - Is Trigger: false + Collision Tag: 0 + Type: Box + Half Extents: {x: 2.5, y: 1, z: 1.60000002} + 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: ~ \ No newline at end of file diff --git a/Assets/Scenes/NavigationTest.shade.shmeta b/Assets/Scenes/NavigationTest.shade.shmeta new file mode 100644 index 00000000..a281205a --- /dev/null +++ b/Assets/Scenes/NavigationTest.shade.shmeta @@ -0,0 +1,3 @@ +Name: NavigationTest +ID: 100246336 +Type: 5 diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 42e112b8..93183a4f 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -35,6 +35,7 @@ #include "Scripting/SHScriptEngine.h" #include "UI/SHUISystem.h" #include "Animation/SHAnimationSystem.h" +#include "Navigation/SHNavigationSystem.h" // Components #include "Graphics/MiddleEnd/Interface/SHRenderable.h" @@ -89,6 +90,7 @@ namespace Sandbox SHSystemManager::CreateSystem(); SHSystemManager::CreateSystem(); SHSystemManager::CreateSystem(); + SHSystemManager::CreateSystem(); //std::system("FontCompiler.exe ../../Assets/Fonts/SegoeUI.ttf"); //std::system("FontCompiler.exe ../../Assets/Fonts/ALGER.ttf"); @@ -127,6 +129,7 @@ namespace Sandbox #ifdef SHEDITOR SHSystemManager::RegisterRoutine(); + SHSystemManager::RegisterRoutine(); #endif SHSystemManager::RegisterRoutine(); diff --git a/SHADE_Engine/src/Navigation/SHNavigationSystem.cpp b/SHADE_Engine/src/Navigation/SHNavigationSystem.cpp new file mode 100644 index 00000000..ca427ab0 --- /dev/null +++ b/SHADE_Engine/src/Navigation/SHNavigationSystem.cpp @@ -0,0 +1,132 @@ +#include "SHpch.h" +#include "SHNavigationSystem.h" +#include "ECS_Base/Managers/SHSystemManager.h" +#include "Physics/System/SHPhysicsSystem.h" +#include "Math/Geometry/SHAABB.h" +#include "Input/SHInputManager.h" + +namespace SHADE +{ + void SHNavigationSystem::Init() + { + SystemID i = SystemFamily::GetID(); + } + + void SHNavigationSystem::Exit() + { + + } + + + uint16_t SHNavigationSystem::GetIndex(uint16_t row, uint16_t col) noexcept + { + size_t rowOffset = numRows / sizeof(GridDataType); + return row * rowOffset + (col / sizeof(GridDataType)); + } + + + + bool SHNavigationSystem::GetCollisionData(uint16_t row, uint16_t col) noexcept + { + + size_t index = GetIndex(row, col); + + GridDataType bitMask = 1 << (col % NumGridDataTypeBits); + + if (collisionGrid.size() < index) + { + return collisionGrid[index] & bitMask; + } + else + { + return 0; + } + + + } + + SHVec2 SHNavigationSystem::GetGridSize() noexcept + { + return SHVec2{size.x / numRows, size.y / numCols}; + } + + void SHNavigationSystem::GenerateCollisionGridData(SHVec3 origin, SHVec2 size, uint16_t nr, uint16_t nc, float gridHeight) noexcept + { + if (size.x <= 0.0f || size.y <= 0.0f || nr == 0 || nc == 0 || gridHeight <= 0.0f) + { + return; + } + + + this->origin = origin; + this->size = size; + numRows = nr; + numCols = nc; + collisionGrid.clear(); + + + GridDataType temp = 0; + + for (size_t r = 0; r < numRows; ++r) + { + for (size_t c = 0; c < numCols; ++c) + { + GridDataType bitmask = 1 << (c % NumGridDataTypeBits); + auto physics = SHSystemManager::GetSystem(); + + //Does the AABB calculation + if (physics != nullptr) + { + //Get the top left position + SHVec3 topleft{ origin.x - (size.x / 2.0f), origin.y, origin.z - (size.y / 2.0f) }; + SHVec2 halfGridSize = GetGridSize() * 0.5f; + + //offset it by row and column and center it with half grid size. + topleft += SHVec3{ c * GetGridSize().x, 0.0f, r * GetGridSize().y} + SHVec3{halfGridSize.x,0.0f,halfGridSize.y}; + + //Get half extents. + + SHVec3 halfExtents{ halfGridSize.x, gridHeight, halfGridSize.y }; + + SHAABB aabb{ topleft,halfExtents }; + + if (physics->TestAABBOverlap(aabb)) + { + temp |= bitmask; + } + } + + + if ((c % NumGridDataTypeBits) == NumGridDataTypeBits - 1) + { + collisionGrid.push_back(temp); + temp = 0; + } + + } + //if the number of column dont fit perfectly to the NumGridDataTypeBits. We push in what we have with the rest as 0s. + if (numCols % NumGridDataTypeBits != 0) + { + collisionGrid.push_back(temp); + temp = 0; + } + + } + + + + } + + + + void SHNavigationSystem::NavigationSystemGenerateRoutine::Execute(double dt) noexcept + { + SHNavigationSystem* system = static_cast(GetSystem()); + if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::H)) + { + system->GenerateCollisionGridData(SHVec3{ 0.0f }, SHVec2{ 25.6f }, 128, 128, 10.0f); + } + } + + +} diff --git a/SHADE_Engine/src/Navigation/SHNavigationSystem.h b/SHADE_Engine/src/Navigation/SHNavigationSystem.h new file mode 100644 index 00000000..e9973bb0 --- /dev/null +++ b/SHADE_Engine/src/Navigation/SHNavigationSystem.h @@ -0,0 +1,67 @@ +#pragma once + +#include "ECS_Base/System/SHSystem.h" +#include "ECS_Base/System/SHSystemRoutine.h" +#include "Math/Vector/SHVec2.h" +#include "Math/Vector/SHVec3.h" + +#include +#include "SH_API.h" + + +namespace SHADE +{ + + + + class SH_API SHNavigationSystem final: public SHSystem + { + private: + + using GridDataType = uint8_t; + + const size_t NumGridDataTypeBits = sizeof(GridDataType) * CHAR_BIT; + + std::vector collisionGrid; + + //Number of subdivision on the x axis + uint16_t numRows{0}; + //Number of subdivision on the z axis + uint16_t numCols{0}; + + //The center of the collision area. + SHVec3 origin{0.0f}; + + //Size of the collision area + SHVec2 size{0.0f}; + + SHVec2 GetGridSize() noexcept; + uint16_t GetIndex(uint16_t row, uint16_t col) noexcept; + + + public: + SHNavigationSystem() = default; + virtual ~SHNavigationSystem() = default; + + void Init(); + void Exit(); + + void SaveNavigationData() noexcept; + void GenerateCollisionGridData(SHVec3 origin, SHVec2 size, uint16_t numRow, uint16_t numCol, float gridHeight = 10.0f) noexcept; + bool GetCollisionData(uint16_t row, uint16_t col) noexcept; + + + class SH_API NavigationSystemGenerateRoutine final: public SHSystemRoutine + { + public: + NavigationSystemGenerateRoutine() : SHSystemRoutine("Navigation System Generate", true) {}; + virtual void Execute(double dt)noexcept override final; + }; + friend class NavigationSystemGenerateRoutine; + + }; + + + +} +