19{
21 {
22 return;
23 }
24
27
29 FVector
Start = TraceTransform.GetLocation();
30 FVector Forward = TraceTransform.GetRotation().GetForwardVector();
31
32 FVector Center =
Start + (Forward * CurrentForwardDistance * 0.5f);
33
34 float CapsuleHalfHeight = (CurrentForwardDistance * 0.5f) + CurrentRadius;
35
36 FQuat CapsuleRot = FRotationMatrix::MakeFromZ(Forward).ToQuat();
37
39 TArray<FOverlapResult> Overlaps;
40
41 GetWorld()->OverlapMultiByChannel(
42 Overlaps,
43 Center,
44 CapsuleRot,
46 FCollisionShape::MakeCapsule(CurrentRadius, CapsuleHalfHeight),
47 Params
48 );
49
50 TMap<AActor*, int32> HitIndexByActor;
51 HitIndexByActor.Reserve(Overlaps.Num());
52
53 for (const FOverlapResult& Overlap : Overlaps)
54 {
55
56 AActor* OverlapActor = Overlap.GetActor();
57 UPrimitiveComponent* HitComponent = Overlap.GetComponent();
58
59 if (IsValid(OverlapActor) == false || IsValid(HitComponent) == false)
60 {
61 continue;
62 }
63
64 FName BoneName = NAME_None;
65 FVector HitLocation = OverlapActor->GetActorLocation();
66 float DistanceToCenter = FVector::Distance(Center, HitLocation);
67 const int32 BodyIndex = Overlap.GetItemIndex();
68
69 if (const USkeletalMeshComponent* MeshComp = Cast<USkeletalMeshComponent>(HitComponent))
70 {
71 if (MeshComp->Bodies.IsValidIndex(BodyIndex))
72 {
73 const FBodyInstance* BodyInstance = MeshComp->Bodies[BodyIndex];
74 const UBodySetup* BodySetup = BodyInstance ? BodyInstance->GetBodySetup() : nullptr;
75
76 if (BodySetup)
77 {
78 BoneName = BodySetup->BoneName;
79
80 FVector ClosestPoint = Center;
81 const float BodyDistance = BodyInstance->GetDistanceToBody(Center, ClosestPoint);
82
83 if (BodyDistance >= 0.0f)
84 {
85 HitLocation = ClosestPoint;
86 DistanceToCenter = BodyDistance;
87 }
88 }
89 }
90 }
91
92
93 FVector HitNormal = (Center - HitLocation).GetSafeNormal();
94
95
96 if (HitNormal.IsNearlyZero())
97 {
98 HitNormal = -Forward;
99 }
100
101 FHitResult Hit(OverlapActor, HitComponent, HitLocation, HitNormal);
102 Hit.BoneName = BoneName;
103 Hit.Item = BodyIndex;
104 Hit.Distance = DistanceToCenter;
105 Hit.ImpactPoint = HitLocation;
106 Hit.ImpactNormal = HitNormal;
107
108 const int32* ExistingIndex = HitIndexByActor.Find(OverlapActor);
109 if (ExistingIndex == nullptr)
110 {
111 const int32 NewIndex = OutHits.Add(Hit);
112 HitIndexByActor.Add(OverlapActor, NewIndex);
113 continue;
114 }
115
117 FHitResult& ExistingHit = OutHits[*ExistingIndex];
118
119 const bool bHasBone = Hit.BoneName.IsNone() == false;
120 const bool bExistingHasBone = ExistingHit.BoneName.IsNone() == false;
121 const bool bCloser = Hit.Distance < ExistingHit.Distance;
122
123 if (bHasBone && bExistingHasBone == false)
124 {
125
126 ExistingHit = Hit;
127 }
128 else if (bHasBone == bExistingHasBone)
129 {
130
131 if (bCloser)
132 {
133 ExistingHit = Hit;
134 }
135 }
136 }
137
138#if ENABLE_DRAW_DEBUG
140 {
141 FColor DrawColor = OutHits.Num() > 0 ? FColor::Green : FColor::Red;
142 DrawDebugCapsule(GetWorld(), Center, CapsuleHalfHeight, CurrentRadius, CapsuleRot, DrawColor, false, 1.0f);
143 }
144#endif
145
146}
Definition TA_CapsuleOverlap.h:12
float CombatDataLevel
Definition TA_Trace.h:85
ECollisionChannel TraceChannel
Definition TA_Trace.h:86
bool IsDebugEnabled() const
Definition TA_Trace.cpp:171
TObjectPtr< ACharacter > Character
Definition TA_Trace.h:100
FTransform ResolveTraceTransform() const
Definition TA_Trace.cpp:150