diff --git a/Source/StevesUEHelpers/Private/StevesDynamicNavMeshVolume.cpp b/Source/StevesUEHelpers/Private/StevesDynamicNavMeshVolume.cpp new file mode 100644 index 0000000..bd28634 --- /dev/null +++ b/Source/StevesUEHelpers/Private/StevesDynamicNavMeshVolume.cpp @@ -0,0 +1,55 @@ + +#include "StevesDynamicNavMeshVolume.h" + +#include "NavigationSystem.h" +#include "Components/BrushComponent.h" +#include "PhysicsEngine/BodySetup.h" + + +AStevesDynamicNavMeshVolume::AStevesDynamicNavMeshVolume() +{ + +} + +void AStevesDynamicNavMeshVolume::SetLocationAndDimensions(const FVector& Location, const FVector& NewDimensions) +{ + SetActorLocation(Location); + UpdateDimensions(NewDimensions); + NotifyNavSystem(); +} + +void AStevesDynamicNavMeshVolume::SetDimensions(const FVector& NewDimensions) +{ + UpdateDimensions(NewDimensions); + NotifyNavSystem(); +} + +void AStevesDynamicNavMeshVolume::UpdateDimensions(const FVector& NewDimensions) +{ + // Volumes are built using UCubeBuilder, but we can't use that class at runtime (Editor only) + // It generates the 6 faces as polys, like old BSP stuff. No idea why for a cube, we don't need that here + // Just box it baby + + if (auto Body = GetBrushComponent()->GetBodySetup()) + { + Body->AggGeom.ConvexElems.Empty(); + if (Body->AggGeom.BoxElems.Num() == 0) + { + Body->AggGeom.BoxElems.Emplace(); + } + auto& Box = Body->AggGeom.BoxElems[0]; + Box.X = NewDimensions.X; + Box.Y = NewDimensions.Y; + Box.Z = NewDimensions.Z; + GetBrushComponent()->Bounds = FBoxSphereBounds(FVector::ZeroVector, NewDimensions*0.5f, NewDimensions.GetMax()*0.5f); + } + +} + +void AStevesDynamicNavMeshVolume::NotifyNavSystem() +{ + if (UNavigationSystemV1* NavSys = FNavigationSystem::GetCurrent(GetWorld())) + { + NavSys->OnNavigationBoundsUpdated(this); + } +} diff --git a/Source/StevesUEHelpers/Public/StevesDynamicNavMeshVolume.h b/Source/StevesUEHelpers/Public/StevesDynamicNavMeshVolume.h new file mode 100644 index 0000000..c249430 --- /dev/null +++ b/Source/StevesUEHelpers/Public/StevesDynamicNavMeshVolume.h @@ -0,0 +1,24 @@ +// + +#pragma once + +#include "CoreMinimal.h" +#include "NavMesh/NavMeshBoundsVolume.h" +#include "StevesDynamicNavMeshVolume.generated.h" + +/// NavMesh bounds that can be changed at runtime (requires that your NavMesh data Runtime Generation is set to Dynamic) +UCLASS() +class STEVESUEHELPERS_API AStevesDynamicNavMeshVolume : public ANavMeshBoundsVolume +{ + GENERATED_BODY() + +public: + AStevesDynamicNavMeshVolume(); + + void SetLocationAndDimensions(const FVector& Location, const FVector& NewDimensions); + void SetDimensions(const FVector& NewDimensions); + +protected: + void UpdateDimensions(const FVector& NewDimensions); + void NotifyNavSystem(); +}; diff --git a/Source/StevesUEHelpers/StevesUEHelpers.Build.cs b/Source/StevesUEHelpers/StevesUEHelpers.Build.cs index 102a76e..1f1d565 100644 --- a/Source/StevesUEHelpers/StevesUEHelpers.Build.cs +++ b/Source/StevesUEHelpers/StevesUEHelpers.Build.cs @@ -39,7 +39,8 @@ public class StevesUEHelpers : ModuleRules { "RenderCore", "PhysicsCore", - "Chaos" + "Chaos", + "NavigationSystem" } );