KLingo Project Documentation 1.0.0
Unreal Engine 5.6 C++ Project Documentation
로딩중...
검색중...
일치하는것 없음
UParabolaComponent.cpp
이 파일의 문서화 페이지로 가기
1
6#include "DrawDebugHelpers.h"
7#include "GameFramework/Actor.h"
8
10UParabolaComponent::UParabolaComponent()
11{
12 PrimaryComponentTick.bCanEverTick = true;
13}
14
16void UParabolaComponent::BeginPlay()
17{
18 Super::BeginPlay();
19
20 for (auto& P : BallisticTracks)
21 P.Value.ResetTime();
22 for (auto& P : GeometricTracks)
23 P.Value.ResetTime();
24}
25
27void UParabolaComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
28{
29 Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
30 UpdateTracks(DeltaTime);
31}
32
34void UParabolaComponent::UpdateTracks(float DeltaTime)
35{
36 for (auto& P : BallisticTracks)
37 P.Value.Advance(DeltaTime);
38 for (auto& P : GeometricTracks)
39 P.Value.Advance(DeltaTime);
40}
41
42
43
45FRotator UParabolaComponent::GetParabolaFacing(FName TrackName, bool bYawOnly, EForwardAxis ForwardAxis) const
46{
47 if (const FParabolaBallisticTrack* Ballistic = BallisticTracks.Find(TrackName))
48 {
49 const float CurAlpha = FMath::Clamp(Ballistic->GetAlpha(), 0.f, 1.f);
50 const float NextAlpha = FMath::Clamp(CurAlpha + 0.08f, 0.f, 1.f);
51
52 if (FMath::IsNearlyEqual(CurAlpha, NextAlpha))
53 return GetOwner()->GetActorRotation();
54
55 const FVector CurLocation = Ballistic->EvaluateAtAlpha(GetOwner(), CurAlpha);
56 const FVector NextLocation = Ballistic->EvaluateAtAlpha(GetOwner(), NextAlpha);
57
58 return MakeFacingFromDir(NextLocation - CurLocation, bYawOnly, ForwardAxis);
59 }
60 else if (const FParabolaGeometricTrack* Geometric = GeometricTracks.Find(TrackName))
61 {
62 const float CurAlpha = FMath::Clamp(Geometric->GetAlpha(), 0.f, 1.f);
63 const float NextAlpha = FMath::Clamp(CurAlpha + 0.08f, 0.f, 1.f);
64
65 if (FMath::IsNearlyEqual(CurAlpha, NextAlpha))
66 return GetOwner()->GetActorRotation();
67
68 const FVector CurLocation = Geometric->EvaluateAtAlpha(GetOwner(), CurAlpha);
69 const FVector NextLocation = Geometric->EvaluateAtAlpha(GetOwner(), NextAlpha);
70
71 return MakeFacingFromDir(NextLocation - CurLocation, bYawOnly, ForwardAxis);
72 }
73
74 return GetOwner()->GetActorRotation();
75}
76
78FRotator UParabolaComponent::MakeFacingFromDir(const FVector& Direction, const bool bYawOnly, const EForwardAxis ForwardAxis) const
79{
80 FVector Dir = Direction;
81
82 if (bYawOnly)
83 Dir.Z = 0.f;
84
85 if (!Dir.Normalize())
86 return GetOwner() ? GetOwner()->GetActorRotation() : FRotator::ZeroRotator;
87
88 FRotator Rot;
89 switch (ForwardAxis)
90 {
91 case EForwardAxis::X:
92 Rot = FRotationMatrix::MakeFromX(Dir).Rotator();
93 break;
94 case EForwardAxis::Y:
95 Rot = FRotationMatrix::MakeFromY(Dir).Rotator();
96 break;
97 case EForwardAxis::Z:
98 Rot = FRotationMatrix::MakeFromZ(Dir).Rotator();
99 break;
100 default:
101 Rot = FRotationMatrix::MakeFromX(Dir).Rotator();
102 break;
103 }
104
105 if (bYawOnly)
106 {
107 Rot.Pitch = 0.f;
108 Rot.Roll = 0.f;
109 }
110 else
111 {
112 Rot.Roll = 0.f;
113 }
114
115 return Rot;
116}
117
118// ---- Ballistic ----
120void UParabolaComponent::SetBallisticParabolaTrack(FName TrackName, const FParabolaBallisticTrack& Track)
121{
122 FParabolaBallisticTrack Copy = Track;
123 Copy.ResetTime();
124 BallisticTracks.FindOrAdd(TrackName) = Copy;
125}
126
128FVector UParabolaComponent::GetBallisticParabolaVectorTrack(FName TrackName) const
129{
130 if (const FParabolaBallisticTrack* T = BallisticTracks.Find(TrackName))
131 {
132 return T->EvaluateAtCurrent(GetOwner());
133 }
134 return FVector::ZeroVector;
135}
136
138FVector UParabolaComponent::GetBallisticVectorAtAlphaFromTrack(FName TrackName, float Alpha) const
139{
140 if (const FParabolaBallisticTrack* T = BallisticTracks.Find(TrackName))
141 {
142 return T->EvaluateAtAlpha(GetOwner(), Alpha);
143 }
144 return FVector::ZeroVector;
145}
146
148void UParabolaComponent::DrawBallisticPath(FName TrackName, int32 NumSegments, FColor Color, float LifeTime) const
149{
150 if (const FParabolaBallisticTrack* T = BallisticTracks.Find(TrackName))
151 {
152 const float StepTime = T->Duration / NumSegments;
153 FVector PrevPos = T->EvaluateAtTime(GetOwner(), 0.f);
154
155 for (int32 i = 1; i <= NumSegments; ++i)
156 {
157 float t = StepTime * i;
158 FVector CurrPos = T->EvaluateAtTime(GetOwner(), t);
159
160 DrawDebugLine(
161 GetWorld(),
162 PrevPos,
163 CurrPos,
164 Color,
165 false, // 지속 여부
166 LifeTime, // 지속 시간 (초)
167 0, // Depth Priority
168 1.5f // 두께
169 );
170
171 PrevPos = CurrPos;
172 }
173 }
174}
175
176
177
178// ---- Geometric ----
180void UParabolaComponent::SetGeometricParabolaTrack(FName TrackName, const FParabolaGeometricTrack& Track)
181{
182 FParabolaGeometricTrack Copy = Track;
183 Copy.ResetTime();
184 GeometricTracks.FindOrAdd(TrackName) = Copy;
185}
186
188FVector UParabolaComponent::GetGeometricParabolaVectorTrack(FName TrackName) const
189{
190 if (const FParabolaGeometricTrack* T = GeometricTracks.Find(TrackName))
191 {
192 return T->EvaluateAtCurrent(GetOwner());
193 }
194 return FVector::ZeroVector;
195}
196
198FVector UParabolaComponent::GetGeometricVectorAtAlphaFromTrack(FName TrackName, float Alpha) const
199{
200 if (const FParabolaGeometricTrack* T = GeometricTracks.Find(TrackName))
201 {
202 return T->EvaluateAtAlpha(GetOwner(), Alpha);
203 }
204 return FVector::ZeroVector;
205}
206
208void UParabolaComponent::DrawGeometricPath(FName TrackName, int32 NumSegments, FColor Color, float LifeTime) const
209{
210 if (const FParabolaGeometricTrack* T = GeometricTracks.Find(TrackName))
211 {
212 const float StepAlpha = 1.f / NumSegments;
213 FVector PrevPos = T->EvaluateAtAlpha(GetOwner(), 0.f);
214
215 for (int32 i = 1; i <= NumSegments; ++i)
216 {
217 float a = StepAlpha * i;
218 FVector CurrPos = T->EvaluateAtAlpha(GetOwner(), a);
219
220 DrawDebugLine(
221 GetWorld(),
222 PrevPos,
223 CurrPos,
224 Color,
225 false,
226 LifeTime,
227 0,
228 1.5f
229 );
230
231 PrevPos = CurrPos;
232 }
233 }
234}
EParabolaType 클래스를 선언합니다.
EForwardAxis
정면 방향 축을 지정한다.
물리 기반(초기 속도/중력) 포물선을 계산하는 트랙.
FORCEINLINE FVector EvaluateAtCurrent(const AActor *Owner) const
현재 시간 기준 위치를 계산한다.
정점 높이를 지정해 기하학적 포물선을 계산하는 트랙.
FORCEINLINE FVector EvaluateAtCurrent(const AActor *Owner) const
현재 시간 기준 위치를 계산한다.