14#include "DrawDebugHelpers.h"
21 PrimaryActorTick.bCanEverTick =
false;
23 VisualizerComponent = CreateDefaultSubobject<UBoundsVisualizerComponent>(TEXT(
"BoundsVisualizerComponent"));
28 BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT(
"BoxComponent"));
32 SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT(
"SphereComponent"));
55 const int32 OverlapSensorIndex = 2;
63 const ECollisionChannel OverlapSensorChannel = UCollisionProfile::Get()->ConvertToCollisionChannel(
false, OverlapSensorIndex);
64 BoxComponent->SetCollisionObjectType(OverlapSensorChannel);
65 BoxComponent->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
66 BoxComponent->SetCollisionResponseToChannel(OverlapSensorChannel, ECollisionResponse::ECR_Overlap);
69 SphereComponent->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
73 BoxComponent->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepWorldTransform);
86 const ECollisionChannel OverlapSensorChannel = UCollisionProfile::Get()->ConvertToCollisionChannel(
false, OverlapSensorIndex);
88 SphereComponent->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
89 SphereComponent->SetCollisionResponseToChannel(OverlapSensorChannel, ECollisionResponse::ECR_Overlap);
92 BoxComponent->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
96 SphereComponent->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepWorldTransform);
102 if (TriggerComponent)
108 ROSMessages::std_msgs::String StringMsg;
109 ROSMessage = MakeShared<ROSMessages::std_msgs::String>(StringMsg);
126 VisualizerComponent->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepWorldTransform);
154 Super::EndPlay(EndPlayReason);
178 SphereComponent->SetSphereRadius(FMath::Max3(NewSize.X, NewSize.Y, NewSize.Z));
188 FString Msg = FString::Printf(TEXT(
"OverlapSensor with ID: %s changed overlap bounds to: %s"), *
GetActorID_Implementation(), *NewSize.ToString());
209 DrawDebugBox(GetWorld(), GetActorLocation(),
BoxComponent->GetScaledBoxExtent(), FColor::Red,
false, 3.0f);
214 DrawDebugSphere(GetWorld(), GetActorLocation(),
SphereComponent->GetScaledSphereRadius(), 32, FColor::Red,
false, 3.0f);
218 UE_LOG(LogTemp, Warning, TEXT(
"OverlapSensor.cpp: Visualize overlap bounds: %s"), (Visualize ? TEXT(
"true") : TEXT(
"false")));
241 FString Msg = FString::Printf(TEXT(
"OverlapSensor.cpp: Changed overlap relative position to: %s"), *Vector.ToString());
247 if (Response != ECollisionResponse::ECR_Overlap && Response != ECollisionResponse::ECR_Ignore)
260 BoxComponent->SetCollisionResponseToAllChannels(Response);
265 int32 OtherBodyIndex,
bool bFromSweep,
const FHitResult& SweepResult)
268 AOverlapSensor* OtherOverlapSensor = Cast< AOverlapSensor>(OtherActor);
269 if (OtherOverlapSensor)
278 UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
281 AOverlapSensor* OtherOverlapSensor = Cast< AOverlapSensor>(OtherActor);
282 if (OtherOverlapSensor)
302 Message = FString::Printf(TEXT(
"%s: %s"), *Prefix, *Actor->GetName());
306 Message = FString::Printf(TEXT(
"%s: %s: ID: %s"), *Prefix, *Actor->GetName(), *ID);
320 FString Msg = FString::Printf(TEXT(
"Actor '%s' with ID: '%s' %s"), *
VehicleName, *
VehicleID, *Message);
329 if (Actor && Actor->GetClass()->ImplementsInterface(UActorInformation::StaticClass()))
334 ID = ActorInfo->GetActorID_Implementation();
static constexpr float SIZE_DIVISOR
void OnOverlapEnd(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex)
USphereComponent * SphereComponent
void SetOverlapRelativePosition(const FVector &Vector)
TSharedPtr< ROSMessages::std_msgs::String > ROSMessage
AOverlapSensor(const FObjectInitializer &ObjectInitializer)
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
virtual void BeginPlay() override
UBoundsVisualizerComponent * VisualizerComponent
UBoxComponent * BoxComponent
ETriggerShape OverlapShape
void Init(FOverlapSensorParameters InParameters)
void SetVisualizeOverlapArea(bool Visualize)
void SetOverlapBounds(const FVector &NewSize)
FOverlapSensorParameters Parameters
void BuildAndSendMessage(const FString &Prefix, AActor *Actor)
void SetOverlapResponseToAllChannels(const ECollisionResponse Response)
FString TryGetID(AActor *Actor)
void OnOverlapBegin(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
void SetSimulateSensor(bool SimulateSensor)
UTopic * GetROSTopic() const
virtual FString GetActorID_Implementation() const override
bool IsROSConnected() const
void WriteToLogFile(const FString &Message)
virtual void CreateROSTopic()
virtual FString GetActorID_Implementation() const override
virtual FString GetActorName_Implementation() const override
static void Log(const FString &Message, bool LogToTextFile=true, bool LogToROS=true)
ETriggerShape TriggerShape