Agrarsense
Static Public Member Functions | List of all members
UPhysicsUtilities Class Reference

#include <PhysicsUtilities.h>

Inheritance diagram for UPhysicsUtilities:
Inheritance graph
[legend]
Collaboration diagram for UPhysicsUtilities:
Collaboration graph
[legend]

Static Public Member Functions

static bool SnapActorToGround (AActor *Actor, float StartZOffset=600.0f, float EndZOffset=600.0f)
 
static bool SnapActorAboveGround (AActor *Actor, float AboveOffset=50.0f)
 
static bool DoesTopPercentageMeshOverlap (AActor *Actor, const UStaticMesh *Mesh, float TopPercentage, ECollisionChannel CollisionChannel)
 
static bool HasOverlappingActors (const UStaticMesh *StaticMesh, const FTransform &Transform)
 
static TArray< AActor * > FindOverlappingActorsInSphere (const FTransform &ActorTransform, float Radius, bool DebugVisualizeRadius=false)
 
template<typename T >
static TArray< T * > FindOverlappingActorsOfClass (const FTransform &ActorTransform, float Radius, bool DebugVisualizeRadius=false)
 

Detailed Description

UPhysicsUtilities is collection of static functions for Physics related utilities.

Definition at line 19 of file PhysicsUtilities.h.

Member Function Documentation

◆ DoesTopPercentageMeshOverlap()

bool UPhysicsUtilities::DoesTopPercentageMeshOverlap ( AActor *  Actor,
const UStaticMesh *  Mesh,
float  TopPercentage,
ECollisionChannel  CollisionChannel 
)
static

Checks if the top percentage of a given mesh overlaps with any other objects.

Parameters
ActorThe actor to check for overlap with.
MeshThe static mesh to check for overlap with.
TopPercentageThe percentage of the mesh's top area to check for overlap (0.0 to 1.0).
CollisionChannelThe collision channel to use for the overlap check.
Returns
True if the top percentage of the mesh overlaps with other objects, false otherwise.

Definition at line 68 of file PhysicsUtilities.cpp.

69{
70 if (!Actor)
71 {
72 return false;
73 }
74
75 UWorld* World = Actor->GetWorld();
76
77 if (World && Mesh)
78 {
79 FTransform Transform = Actor->GetActorTransform();
80 FVector ActorLocation = Transform.GetLocation();
81 FVector ActorScale = Transform.GetScale3D();
82
83 // Calculate the top extent of the mesh bounds
84 FVector MeshExtent = Mesh->GetBounds().BoxExtent;
85 FVector TopExtent = FVector(MeshExtent.X, MeshExtent.Y, MeshExtent.Z * TopPercentage);
86
87 FVector StartLocation = ActorLocation + FVector(0.0f, 0.0f, MeshExtent.Z);
88 FVector EndLocation = ActorLocation + FVector(0.0f, 0.0f, MeshExtent.Z * TopPercentage);
89
90 FHitResult HitResult;
91 FCollisionQueryParams QueryParams;
92 QueryParams.AddIgnoredActor(Actor);
93
94 // Perform a line trace to check for collisions in the top percentage of the mesh
95 return World->LineTraceSingleByChannel(HitResult, StartLocation, EndLocation, CollisionChannel, QueryParams);
96 }
97
98 return false;
99}

References Transform.

◆ FindOverlappingActorsInSphere()

TArray< AActor * > UPhysicsUtilities::FindOverlappingActorsInSphere ( const FTransform &  ActorTransform,
float  Radius,
bool  DebugVisualizeRadius = false 
)
static

Finds all overlapping actors within a sphere.

Parameters
ActorTransformThe transform of the actor to check for overlaps.
RadiusThe radius of the sphere.
DebugVisualizeRadiusIf true, visualizes the sphere for 2 seconds in the editor for debugging purposes.
Returns
An array of overlapping actors within the sphere.

Definition at line 138 of file PhysicsUtilities.cpp.

139{
140 return FindOverlappingActorsOfClass<AActor>(ActorTransform, Radius, DebugVisualizeRadius);
141}

◆ FindOverlappingActorsOfClass()

template<typename T >
TArray< T * > UPhysicsUtilities::FindOverlappingActorsOfClass ( const FTransform &  ActorTransform,
float  Radius,
bool  DebugVisualizeRadius = false 
)
static

Finds all overlapping actors of a specific class within a sphere.

Parameters
ActorTransformThe transform of the actor to check for overlaps.
RadiusThe radius of the sphere.
DebugVisualizeRadiusIf true, visualizes the sphere for 2 seconds in the editor for debugging purposes.
Returns
An array of overlapping actors of the specified class within the sphere.

Definition at line 144 of file PhysicsUtilities.cpp.

145{
146 TArray<T*> OverlappingActors;
147
148 if (!GEngine || !GEngine->GameViewport)
149 {
150 return OverlappingActors;
151 }
152
153 UWorld* World = GEngine->GameViewport->GetWorld();
154
155 if (World)
156 {
157 TArray<FOverlapResult> OverlapResults;
158 FCollisionQueryParams Params;
159 Params.AddIgnoredActor(nullptr);
160 Params.bTraceComplex = false;
161 Params.bReturnPhysicalMaterial = false;
162
163 FCollisionObjectQueryParams ObjectQueryParams;
164 ObjectQueryParams.AddObjectTypesToQuery(ECC_WorldStatic);
165
166 bool bOverlap = World->OverlapMultiByObjectType(
167 OverlapResults,
168 ActorTransform.GetLocation(),
169 ActorTransform.GetRotation(),
170 ObjectQueryParams,
171 FCollisionShape::MakeSphere(Radius),
172 Params
173 );
174
175 if (bOverlap)
176 {
177 for (const FOverlapResult& OverlapResult : OverlapResults)
178 {
179 T* OverlappingActor = Cast<T>(OverlapResult.GetActor());
180 if (OverlappingActor)
181 {
182 OverlappingActors.Add(OverlappingActor);
183 }
184 }
185 }
186 }
187
188#if WITH_EDITOR
189 if (DebugVisualizeRadius)
190 {
191 DrawDebugSphere(World, ActorTransform.GetLocation(), Radius, 12, FColor::Green, false, 2.0f, 0, 1.0f);
192 }
193#endif
194
195 return OverlappingActors;
196}

◆ HasOverlappingActors()

bool UPhysicsUtilities::HasOverlappingActors ( const UStaticMesh *  StaticMesh,
const FTransform &  Transform 
)
static

Checks if there are any overlapping actors based on a provided static mesh and transform.

Parameters
StaticMeshThe static mesh used to define the shape for overlap detection.
TransformThe transform of the actor to check for overlapping actors.
Returns
True if there are overlapping actors, false otherwise.

Definition at line 101 of file PhysicsUtilities.cpp.

102{
103 if (!StaticMesh)
104 {
105#if WITH_EDITOR
106 UE_LOG(LogTemp, Warning, TEXT("UPhysicsUtilities.cpp: StaticMesh is null!"));
107#endif
108 return false;
109 }
110
111 if (!GEngine || !GEngine->GameViewport)
112 {
113 return false;
114 }
115
116 TArray<FOverlapResult> OverlapResults;
117
118 UWorld* World = GEngine->GameViewport->GetWorld();
119 if (World)
120 {
121 FVector Location = Transform.GetLocation();
122 FQuat Rotation = Transform.GetRotation();
123 FVector Scale = Transform.GetScale3D();
124 FBox ActorBounds = StaticMesh->GetBoundingBox().TransformBy(FTransform(Rotation, Location, Scale));
125
126 World->OverlapMultiByObjectType(
127 OverlapResults,
128 Location,
129 Rotation,
130 FCollisionObjectQueryParams(ECC_WorldStatic),
131 FCollisionShape::MakeBox(ActorBounds.GetSize() * 0.5f)
132 );
133 }
134
135 return OverlapResults.Num() > 0;
136}

References Transform.

◆ SnapActorAboveGround()

bool UPhysicsUtilities::SnapActorAboveGround ( AActor *  Actor,
float  AboveOffset = 50.0f 
)
static

Snaps the given actor above ground

Parameters
ActorThe actor to snap to the ground.
AboveOffsetThe offset value to the ground
Returns
True if the actor's position was adjusted, false otherwise.

Definition at line 49 of file PhysicsUtilities.cpp.

50{
51 if (!Actor)
52 {
53 return false;
54 }
55
56 // First snap actor to the ground
57 SnapActorToGround(Actor);
58
59 // Then add Z offset
60 const FVector CurrentActorLocation = Actor->GetActorLocation();
61 const FVector EndActorLocation = CurrentActorLocation + FVector(0.0f, 0.0f, AboveOffset);
62 Actor->TeleportTo(EndActorLocation, Actor->GetActorRotation());
63 //Actor->SetActorLocation(EndActorLocation);
64
65 return true;
66}
static bool SnapActorToGround(AActor *Actor, float StartZOffset=600.0f, float EndZOffset=600.0f)

References SnapActorToGround().

Referenced by UAssetLibrary::SpawnVehicle().

◆ SnapActorToGround()

bool UPhysicsUtilities::SnapActorToGround ( AActor *  Actor,
float  StartZOffset = 600.0f,
float  EndZOffset = 600.0f 
)
static

Snaps the given actor to the ground by performing a line trace and adjusting its location.

Parameters
ActorThe actor to snap to the ground.
StartZOffsetThe offset value added to the actor's original Z coordinate to determine the start location of the line trace.
EndZOffsetThe offset value subtracted from the actor's original Z coordinate to determine the end location of the line trace.
Returns
True if the actor's position was adjusted, false otherwise.

Definition at line 17 of file PhysicsUtilities.cpp.

18{
19 bool ActorPositionWasChanged = false;
20
21 if (!Actor)
22 {
23 return ActorPositionWasChanged;
24 }
25
26 UWorld* World = Actor->GetWorld();
27 if (World && Actor)
28 {
29 // Perform a line trace from a point above the actor's location to a point slightly below it
30 const FVector ActorLocation = Actor->GetActorLocation();
31 const FVector StartLocation = ActorLocation + FVector(0.0f, 0.0f, StartZOffset);
32 const FVector EndLocation = ActorLocation - FVector(0.0f, 0.0f, EndZOffset);
33 FHitResult HitResult;
34 FCollisionQueryParams QueryParams;
35 QueryParams.AddIgnoredActor(Actor);
36 bool Hits = World->LineTraceSingleByChannel(HitResult, StartLocation, EndLocation, ECC_WorldStatic, QueryParams);
37
38 if (Hits)
39 {
40 //Actor->SetActorLocation(HitResult.ImpactPoint);
41 Actor->TeleportTo(HitResult.ImpactPoint, Actor->GetActorRotation());
42 ActorPositionWasChanged = true;
43 }
44 }
45
46 return ActorPositionWasChanged;
47}

Referenced by SnapActorAboveGround(), and UAssetLibrary::TrySpawnActor().


The documentation for this class was generated from the following files: