Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inner margin for ClampLocationNavigableWorld #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions Source/DonAINavigation/Classes/DonNavigationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -998,17 +998,20 @@ class DONAINAVIGATION_API ADonNavigationManager : public AActor
return VolumeAtSafe(x, y, z);
}

/* Clamps a vector to the navigation bounds as defined by the grid configuration of the navigation object you've placed in the map*/
/** Clamps a vector within the navigation bounds, as defined by the grid configuration of the navigation object you've placed in the map,
* brought in by a margin of InnerMarginOffset when clamping downward to prevent rounding errors */
UFUNCTION(BlueprintPure, Category = "DoN Navigation")
FVector ClampLocationToNavigableWorld(FVector DesiredLocation)
FVector ClampLocationToNavigableWorld(FVector DesiredLocation, float InnerMarginOffset = 0.001f)
{
if (bIsUnbound)
return DesiredLocation;

FVector origin = GetActorLocation();
float xClamped = FMath::Clamp(DesiredLocation.X, origin.X, origin.X + XGridSize * VoxelSize);
float yClamped = FMath::Clamp(DesiredLocation.Y, origin.Y, origin.Y + YGridSize * VoxelSize);
float zClamped = FMath::Clamp(DesiredLocation.Z, origin.Z, origin.Z + ZGridSize * VoxelSize);
// When clamping down, bring the vector in by an extra InnerMarginOffset cm. Necessary because VolumeIdAt does a float-to-int rounding, and thus clamping
// to a max world dimension would otherwise round to an invalid index (e.g. rounding 100.5 to invalid index 100 instead of 99 in a 100-voxel world)
float xClamped = FMath::Clamp(DesiredLocation.X, origin.X, origin.X + XGridSize * VoxelSize - InnerMarginOffset);
float yClamped = FMath::Clamp(DesiredLocation.Y, origin.Y, origin.Y + YGridSize * VoxelSize - InnerMarginOffset);
float zClamped = FMath::Clamp(DesiredLocation.Z, origin.Z, origin.Z + ZGridSize * VoxelSize - InnerMarginOffset);

return FVector(xClamped, yClamped, zClamped);
}
Expand Down