Add helper to set dynamic sized nav meshes

This commit is contained in:
Steve Streeting 2024-01-15 18:21:29 +00:00
parent df40ca92e5
commit 672b199612
3 changed files with 81 additions and 1 deletions

View File

@ -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<UNavigationSystemV1>(GetWorld()))
{
NavSys->OnNavigationBoundsUpdated(this);
}
}

View File

@ -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();
};

View File

@ -39,7 +39,8 @@ public class StevesUEHelpers : ModuleRules
{
"RenderCore",
"PhysicsCore",
"Chaos"
"Chaos",
"NavigationSystem"
}
);