How to detect if an Actor is IN or OUT a closed Spline? I don´t know if there is a note or a setting for that.
Same question?
Sounds like. This question just means the same, but without the grid system.
[quote=“Avlu_, post:1, topic:1670073, full:true, username:Avlu”]
How to detect if an Actor is IN or OUT a closed Spline? I don´t know if there is a note or a setting for that.
[/quote]
Blockquote
You can try this code:
void ASplineEffectActor::SetupSpline()
{
// Clear existing spline points and box components
SplineComponent->ClearSplinePoints(false);
for (UBoxComponent* BoxComponent : BoxComponents)
{
if (BoxComponent)
{
BoxComponent->DestroyComponent();
}
}
BoxComponents.Empty();
// Define spline points in World space
FVector Points[4] = { FVector(0.f, 0.f, 0.f), FVector(100.f, 0.f, 0.f), FVector(100.f, 40.f, 0.f), FVector(0.f, 40.f, 0.f) };
for (int32 i = 0; i < 4; i++)
{
SplineComponent->AddSplinePoint(Points[i], ESplineCoordinateSpace::Local, true);
}
SplineComponent->SetClosedLoop(true);
SplineComponent->UpdateSpline();
}
void ASplineEffectActor::SetupMultipleSplineColliders()
{
// Number of colliders per segment
const int32 NumCollidersPerSegment = 5;
// Clear previous box components
for (UBoxComponent* BoxComponent : BoxComponents)
{
if (BoxComponent)
{
BoxComponent->DestroyComponent();
}
}
BoxComponents.Empty();
// Get total spline length
float TotalSplineLength = SplineComponent->GetSplineLength();
// Calculate desired distance between colliders
float ColliderSpacing = TotalSplineLength / (NumCollidersPerSegment * SplineComponent->GetNumberOfSplinePoints());
// Add multiple Box Colliders along the spline
for (float DistanceAlongSpline = 0.0f; DistanceAlongSpline < TotalSplineLength - ColliderSpacing; DistanceAlongSpline += ColliderSpacing)
{
FVector StartLocation = SplineComponent->GetLocationAtDistanceAlongSpline(DistanceAlongSpline, ESplineCoordinateSpace::Local);
FVector EndLocation = SplineComponent->GetLocationAtDistanceAlongSpline(DistanceAlongSpline + ColliderSpacing, ESplineCoordinateSpace::Local);
FVector Center = (StartLocation + EndLocation) / 2.0f;
FVector Direction = (EndLocation - StartLocation).GetSafeNormal();
UE_LOG(LogTemp, Log, TEXT("DistanceAlongSpline: %f"), DistanceAlongSpline);
UE_LOG(LogTemp, Log, TEXT("StartLocation: %s"), *StartLocation.ToString());
UE_LOG(LogTemp, Log, TEXT("EndLocation: %s"), *EndLocation.ToString());
UE_LOG(LogTemp, Log, TEXT("Center (before Z offset): %s"), *Center.ToString());
UE_LOG(LogTemp, Log, TEXT("Direction: %s"), *Direction.ToString());
// Offset the center slightly below the spline and make the box extend downward
Center.Z -= 500.f; // Move the box slightly below the spline
// Log the adjusted center location
UE_LOG(LogTemp, Log, TEXT("Center (after Z offset): %s"), *Center.ToString());
// Calculate the extent of the box
float SplineWidth = 10.f; // Adjust based on your spline width
FVector Extent = FVector(ColliderSpacing / 2.0f, SplineWidth, 510.f); // Adjust the Z extent as needed
// Create a new box component
UBoxComponent* BoxComponent = NewObject<UBoxComponent>(this, UBoxComponent::StaticClass());
BoxComponent->SetWorldLocation(Center);
BoxComponent->SetBoxExtent(Extent);
// Set the box rotation to match the direction of the spline
FRotator BoxRotation = Direction.Rotation();
BoxComponent->SetWorldRotation(BoxRotation);
// Attach the box to the spline component
BoxComponent->SetupAttachment(SplineComponent);
// Configure box collision properties
BoxComponent->SetCollisionProfileName(TEXT("OverlapAllDynamic"));
BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &ASplineEffectActor::OnBoxOverlap);
// Enable visibility for debugging
BoxComponent->SetHiddenInGame(false);
BoxComponent->SetVisibility(true);
// Register the component with the world
BoxComponent->RegisterComponent();
// Add the box to the array of box components
BoxComponents.Add(BoxComponent);
// Create a debug text render component
UTextRenderComponent* TextComponent = NewObject<UTextRenderComponent>(this);
TextComponent->SetText(FText::FromString(FString::Printf(TEXT("Box %d"), BoxComponents.Num())));
TextComponent->SetWorldSize(30.0f);
TextComponent->SetupAttachment(BoxComponent);
TextComponent->SetRelativeLocation(FVector(0.f, 0.f, 400.f)); // Adjust to position above the box
TextComponent->SetHorizontalAlignment(EHorizTextAligment::EHTA_Center);
// Register the text component
TextComponent->RegisterComponent();
//UE_LOG(LogTemp, Log, TEXT("BoxComponents.Num: %d"), *BoxComponents.Num());
}
}
SetupSpline should be called from the constructor:
ASplineEffectActor::ASplineEffectActor()
{
PrimaryActorTick.bCanEverTick = true;
SetRootComponent(CreateDefaultSubobject<USceneComponent>("SceneRoot"));
// Create Spline Component
SplineComponent = CreateDefaultSubobject<USplineComponent>(TEXT("SplineComponent"));
SplineComponent->SetupAttachment(RootComponent);
SplineComponent->SetClosedLoop(true);
SetupSpline();
}
SetupMultipleSplineColliders should be called from BeginPlay
basicly it created a wall of boxColliders around the spline which i can easily observe.
Hope it helps…