/
NVTraceHelper.cpp
168 lines (144 loc) · 7.81 KB
/
NVTraceHelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Fill out your copyright notice in the Description page of Project Settings.
#include "Lib/NVTraceHelper.h"
#include "Engine/World.h"
#include "PhysicsEngine/PhysicsSettings.h"
#include "Runtime\Engine\Private\KismetTraceUtils.h"
static const float CUSTOM_KISMET_TRACE_DEBUG_IMPACTPOINT_SIZE = 16.f;
bool UNVTraceHelper::CustomCapsuleTraceSingleForObjects(const UObject* WorldContextObject, const FVector Center, const FRotator Rotation, float Radius, float HalfHeight, const TArray<TEnumAsByte<EObjectTypeQuery>>& ObjectTypes,
bool bTraceComplex, const TArray<AActor*>& ActorsToIgnore, EDrawDebugTrace::Type DrawDebugType, FHitResult& OutHit, bool bIgnoreSelf, FLinearColor TraceColor, FLinearColor TraceHitColor, float DrawTime)
{
static const FName CapsuleTraceSingleName(TEXT("CapsuleTraceSingleForObjects"));
FCollisionQueryParams Params = CustomConfigureCollisionParams(CapsuleTraceSingleName, bTraceComplex, ActorsToIgnore, bIgnoreSelf, WorldContextObject);
FCollisionObjectQueryParams ObjectParams = CustomConfigureCollisionObjectParams(ObjectTypes);
if (ObjectParams.IsValid() == false)
{
UE_LOG(LogBlueprintUserMessages, Warning, TEXT("Invalid object types"));
return false;
}
UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
bool const bHit = World ? World->SweepSingleByObjectType(OutHit, Center, Center, Rotation.Quaternion(), ObjectParams, FCollisionShape::MakeCapsule(Radius, HalfHeight), Params) : false;
#if ENABLE_DRAW_DEBUG //DRAW DEBUG CAPSULE
if (DrawDebugType != EDrawDebugTrace::None)
{
bool bPersistent = DrawDebugType == EDrawDebugTrace::Persistent;
float LifeTime = (DrawDebugType == EDrawDebugTrace::ForDuration) ? DrawTime : 0.f;
if (bHit && OutHit.bBlockingHit)
{
// Red up to the blocking hit, green thereafter
::DrawDebugCapsule(World, Center, HalfHeight, Radius, Rotation.Quaternion(), TraceColor.ToFColor(true), bPersistent, LifeTime);
::DrawDebugCapsule(World, OutHit.Location, HalfHeight, Radius, Rotation.Quaternion(), TraceColor.ToFColor(true), bPersistent, LifeTime);
::DrawDebugLine(World, Center, OutHit.Location, TraceColor.ToFColor(true), bPersistent, LifeTime);
::DrawDebugPoint(World, OutHit.ImpactPoint, CUSTOM_KISMET_TRACE_DEBUG_IMPACTPOINT_SIZE, TraceColor.ToFColor(true), bPersistent, LifeTime);
::DrawDebugCapsule(World, Center, HalfHeight, Radius, Rotation.Quaternion(), TraceHitColor.ToFColor(true), bPersistent, LifeTime);
::DrawDebugLine(World, OutHit.Location, Center, TraceHitColor.ToFColor(true), bPersistent, LifeTime);
}
else
{
// no hit means all red
::DrawDebugCapsule(World, Center, HalfHeight, Radius, Rotation.Quaternion(), TraceColor.ToFColor(true), bPersistent, LifeTime);
::DrawDebugCapsule(World, Center, HalfHeight, Radius, Rotation.Quaternion(), TraceColor.ToFColor(true), bPersistent, LifeTime);
::DrawDebugLine(World, Center, Center, TraceColor.ToFColor(true), bPersistent, LifeTime);
}
}
#endif
return bHit;
}
bool UNVTraceHelper::CustomCapsuleTraceMultiForObjects(const UObject* WorldContextObject, const FVector Center, const FRotator Rotation, float Radius, float HalfHeight, const TArray<TEnumAsByte<EObjectTypeQuery>>& ObjectTypes, bool bTraceComplex,
const TArray<AActor*>& ActorsToIgnore, EDrawDebugTrace::Type DrawDebugType, TArray<FHitResult>& OutHits, bool bIgnoreSelf, FLinearColor TraceColor, FLinearColor TraceHitColor, float DrawTime)
{
static const FName CapsuleTraceMultiName(TEXT("CapsuleTraceMultiForObjects"));
FCollisionQueryParams Params = CustomConfigureCollisionParams(CapsuleTraceMultiName, bTraceComplex, ActorsToIgnore, bIgnoreSelf, WorldContextObject);
FCollisionObjectQueryParams ObjectParams = CustomConfigureCollisionObjectParams(ObjectTypes);
if (ObjectParams.IsValid() == false)
{
UE_LOG(LogBlueprintUserMessages, Warning, TEXT("Invalid object types"));
return false;
}
UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
bool const bHit = World ? World->SweepMultiByObjectType(OutHits, Center, Center, Rotation.Quaternion(), ObjectParams, FCollisionShape::MakeCapsule(Radius, HalfHeight), Params) : false;
#if ENABLE_DRAW_DEBUG
if (DrawDebugType != EDrawDebugTrace::None)
{
bool bPersistent = DrawDebugType == EDrawDebugTrace::Persistent;
float LifeTime = (DrawDebugType == EDrawDebugTrace::ForDuration) ? DrawTime : 0.f;
if (bHit && OutHits.Last().bBlockingHit)
{
// Red up to the blocking hit, green thereafter
FVector const BlockingHitPoint = OutHits.Last().Location;
::DrawDebugCapsule(World, Center, HalfHeight, Radius, Rotation.Quaternion(), TraceColor.ToFColor(true), bPersistent, LifeTime);
::DrawDebugCapsule(World, BlockingHitPoint, HalfHeight, Radius, Rotation.Quaternion(), TraceColor.ToFColor(true), bPersistent, LifeTime);
::DrawDebugLine(World, Center, BlockingHitPoint, TraceColor.ToFColor(true), bPersistent, LifeTime);
::DrawDebugCapsule(World, Center, HalfHeight, Radius, Rotation.Quaternion(), TraceHitColor.ToFColor(true), bPersistent, LifeTime);
::DrawDebugLine(World, BlockingHitPoint, Center, TraceHitColor.ToFColor(true), bPersistent, LifeTime);
}
else
{
// no hit means all red
::DrawDebugCapsule(World, Center, HalfHeight, Radius, Rotation.Quaternion(), TraceColor.ToFColor(true), bPersistent, LifeTime);
::DrawDebugCapsule(World, Center, HalfHeight, Radius, Rotation.Quaternion(), TraceColor.ToFColor(true), bPersistent, LifeTime);
::DrawDebugLine(World, Center, Center, TraceColor.ToFColor(true), bPersistent, LifeTime);
}
// draw hits
for (int32 HitIdx = 0; HitIdx < OutHits.Num(); ++HitIdx)
{
FHitResult const& Hit = OutHits[HitIdx];
::DrawDebugPoint(World, Hit.ImpactPoint, CUSTOM_KISMET_TRACE_DEBUG_IMPACTPOINT_SIZE, (Hit.bBlockingHit ? TraceColor.ToFColor(true) : TraceHitColor.ToFColor(true)), bPersistent, LifeTime);
}
}
#endif
return bHit;
}
FCollisionQueryParams UNVTraceHelper::CustomConfigureCollisionParams(FName TraceTag, bool bTraceComplex, const TArray<AActor*>& ActorsToIgnore, bool bIgnoreSelf, const UObject* WorldContextObject)
{
FCollisionQueryParams Params(TraceTag, SCENE_QUERY_STAT_ONLY(KismetTraceUtils), bTraceComplex);
Params.bReturnPhysicalMaterial = true;
Params.bReturnFaceIndex = !UPhysicsSettings::Get()->bSuppressFaceRemapTable; // Ask for face index, as long as we didn't disable globally
Params.AddIgnoredActors(ActorsToIgnore);
if (bIgnoreSelf)
{
const AActor* IgnoreActor = Cast<AActor>(WorldContextObject);
if (IgnoreActor)
{
Params.AddIgnoredActor(IgnoreActor);
}
else
{
// find owner
const UObject* CurrentObject = WorldContextObject;
while (CurrentObject)
{
CurrentObject = CurrentObject->GetOuter();
IgnoreActor = Cast<AActor>(CurrentObject);
if (IgnoreActor)
{
Params.AddIgnoredActor(IgnoreActor);
break;
}
}
}
}
return Params;
}
FCollisionObjectQueryParams UNVTraceHelper::CustomConfigureCollisionObjectParams(const TArray<TEnumAsByte<EObjectTypeQuery>>& ObjectTypes)
{
TArray<TEnumAsByte<ECollisionChannel>> CollisionObjectTraces;
CollisionObjectTraces.AddUninitialized(ObjectTypes.Num());
for (auto Iter = ObjectTypes.CreateConstIterator(); Iter; ++Iter)
{
CollisionObjectTraces[Iter.GetIndex()] = UEngineTypes::ConvertToCollisionChannel(*Iter);
}
FCollisionObjectQueryParams ObjectParams;
for (auto Iter = CollisionObjectTraces.CreateConstIterator(); Iter; ++Iter)
{
const ECollisionChannel & Channel = (*Iter);
if (FCollisionObjectQueryParams::IsValidObjectQuery(Channel))
{
ObjectParams.AddObjectTypesToQuery(Channel);
}
else
{
UE_LOG(LogBlueprintUserMessages, Warning, TEXT("%d isn't valid object type"), (int32)Channel);
}
}
return ObjectParams;
}