forked from WindyDarian/ProjectileAimingHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelperLibrary.cpp
191 lines (166 loc) · 4.98 KB
/
HelperLibrary.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Fill out your copyright notice in the Description page of Project Settings.
#include "GMCPPBlueprintFunctionLibrary.h"
#include <limits>
constexpr auto InfFloat = std::numeric_limits<float>::max();
float CalculateProjectileDirectionOneDimensionHelper(float Target, float Gravity, float Speed, bool* bWillHit, float* Time)
{
// Helper function for 1D special case
// Gravity should always be positive
if (Target >= 0)
{
if (bWillHit) { *bWillHit = true; }
if (Time)
{
*Time = (-Speed + FMath::Sqrt(Speed * Speed + 2 * Gravity * Target)) / Gravity;
}
return 1.0f; // Same direction as Gravity
}
else
{
auto Delta = Speed * Speed + 2 * Gravity * Target;
if (Delta < 0)
{
if (bWillHit) { *bWillHit = false; }
if (Time)
{
*Time = InfFloat;
}
}
else
{
if (bWillHit) { *bWillHit = true; }
if (Time)
{
auto SqrtDelta = FMath::Sqrt(Delta);
if (-SqrtDelta + Speed >= 0)
{
*Time = (-SqrtDelta + Speed) / Gravity;
}
else
{
*Time = (SqrtDelta + Speed) / Gravity;
}
}
}
return -1.0f;
}
}
FVector CalculateProjectileDirectionHelper(FVector Target, FVector Origin, FVector Gravity, float Speed, bool* bWillHit, float* Time)
{
// TODO: Maybe changing zero related comparisons to an epsilon
if (Speed < 0)
{
Speed *= -1.0f;
}
auto RelativeTarget = Target - Origin;
if (RelativeTarget.IsNearlyZero())
{
// Special case: same location
if (bWillHit) { *bWillHit = true; }
if (Time) { *Time = 0.0f; }
return FVector::ForwardVector;
}
else if (FMath::IsNearlyZero(Speed))
{
// Special case: zero speed
if (bWillHit) { *bWillHit = false; }
if (Time) { *Time = InfFloat; }
return FVector::ForwardVector;
}
else if (Gravity.IsNearlyZero())
{
// Special case: no gravity
FVector Direction; float Length;
RelativeTarget.ToDirectionAndLength(Direction, Length);
if (bWillHit) { *bWillHit = true; }
if (Time)
{
*Time = Length / Speed;
}
return Direction;
}
FVector GravityAxis;
float GravitySize;
Gravity.ToDirectionAndLength(GravityAxis, GravitySize);
auto FrontAxis = FVector::CrossProduct(Gravity, FVector::CrossProduct(RelativeTarget, Gravity));
bool bIsDifferentDirectionFromGravity = FrontAxis.Normalize();
if (!bIsDifferentDirectionFromGravity)
{
// Special case: X and gravity facing the same direction
auto DirectionFactor = CalculateProjectileDirectionOneDimensionHelper(
FVector::DotProduct(GravityAxis, RelativeTarget),
GravitySize,
Speed,
bWillHit,
Time
);
return DirectionFactor * GravityAxis;
}
auto Distance_X = FVector::DotProduct(FrontAxis, RelativeTarget);
auto Distance_Y = FVector::DotProduct(GravityAxis, RelativeTarget);
auto Distance_X_2 = Distance_X * Distance_X;
auto Distance = RelativeTarget.Size();
auto GravitySize_2 = GravitySize * GravitySize;
auto Speed_2 = Speed * Speed;
auto Speed_4 = Speed_2 * Speed_2;
auto DeltaA = -Distance_X_2 * GravitySize_2
+ 2 * Distance_Y * GravitySize * Speed_2
+ Speed_4;
// To make sure archers can fire to a close position even if cannot hit;
auto AlteredSqrtDeltaA = DeltaA >= 0 ? FMath::Sqrt(DeltaA) : 0;
auto DeltaB = Distance_Y * GravitySize + Speed_2 + AlteredSqrtDeltaA;
constexpr auto OneOverSqrtTwo = 0.707107f;
auto X = (DeltaB > 0) ? (FMath::Sqrt(DeltaB) * Distance_X / Distance * OneOverSqrtTwo) : 0;
auto Y = (DeltaB > 0) ? (Distance_Y * X / Distance_X - 0.5f * Distance_X * GravitySize / X) : -Speed;
if (DeltaB < 0 || DeltaA < 0)
{
if (bWillHit) { *bWillHit = false; }
if (Time) { *Time = X > 0 ? Distance_X / X : InfFloat; }
}
else
{
if (bWillHit) { *bWillHit = true; }
if (Time) { *Time = Distance_X / X; }
}
auto result = GravityAxis * Y + FrontAxis * X;
return result;
}
FVector UGMCPPBlueprintFunctionLibrary::CalculateProjectileDirection(FVector Target, FVector Origin, FVector Gravity, float Speed, bool& bWillHit, float& Time)
{
return CalculateProjectileDirectionHelper(Target, Origin, Gravity, Speed, &bWillHit, &Time);
}
FVector UGMCPPBlueprintFunctionLibrary::CalculateProjectileDirectionForMovingTarget(FVector Target, FVector TargetVelocity, FVector Origin, FVector Gravity, float Speed, bool& bWillHit, float& Time, int Iterations, float EpsilonTime)
{
if (TargetVelocity.IsNearlyZero())
{
return CalculateProjectileDirectionHelper(Target, Origin, Gravity, Speed, &bWillHit, &Time);
}
if (Iterations <= 0)
{
// UE_LOG(LnHLog, Warning, TEXT("Recursion Limit should be positve!"));
Iterations = 10;
}
// TODO: enhance convergence
FVector PrevDirection;
float PrevTime = 0.0f;
float NewTime = 0.0f;
bool bHit = false;
constexpr float Alpha = 0.8f;
for (auto i = 0; i < Iterations; i++)
{
PrevTime = FMath::Lerp(PrevTime, NewTime, Alpha);
PrevDirection = CalculateProjectileDirectionHelper(Target + TargetVelocity * PrevTime, Origin, Gravity, Speed, &bHit, &NewTime);
if (NewTime == InfFloat)
{
bHit = false;
break;
}
if (FMath::IsNearlyEqual(NewTime, PrevTime, EpsilonTime))
{
break;
}
}
Time = NewTime;
bWillHit = bHit;
return PrevDirection;
}