Skip to content

Commit d12b236

Browse files
committed
Refactors target position prediction
Moves target prediction logic into engine traits, improving modularity. This change consolidates target position prediction within the engine traits, allowing for a more flexible and maintainable design. This eliminates redundant code and simplifies the core prediction engine by delegating target movement calculations to the appropriate trait.
1 parent 7a5090d commit d12b236

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

include/omath/projectile_prediction/engine_traits/source_engine_trait.hpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma once
66
#include "omath/engines/source_engine/formulas.hpp"
77
#include "omath/projectile_prediction/projectile.hpp"
8+
#include "omath/projectile_prediction/target.hpp"
89
#include <optional>
910

1011
namespace omath::projectile_prediction::traits
@@ -25,16 +26,16 @@ namespace omath::projectile_prediction::traits
2526

2627
return current_pos;
2728
}
28-
29-
static bool is_projectile_reached_target(const Vector3<float>& target_position,
30-
const Projectile& projectile, const float pitch,
31-
const float time, const float gravity,
32-
const float tolerance) noexcept
29+
[[nodiscard]]
30+
static constexpr Vector3<float> predict_target_position(const Target& target, const float time,
31+
const float gravity) noexcept
3332
{
34-
const auto yaw = projectile.m_origin.view_angle_to(target_position).y;
35-
const auto projectile_position = predict_projectile_position(projectile, pitch, yaw, time, gravity);
33+
auto predicted = target.m_origin + target.m_velocity * time;
3634

37-
return projectile_position.distance_to(target_position) <= tolerance;
35+
if (target.m_is_airborne)
36+
predicted.z -= gravity * (time * time) * 0.5f;
37+
38+
return predicted;
3839
}
3940
[[nodiscard]]
4041
static float calc_vector_2d_distance(const Vector3<float>& delta) noexcept
@@ -68,5 +69,12 @@ namespace omath::projectile_prediction::traits
6869

6970
return angles::radians_to_degrees(std::asin(delta.z / distance));
7071
}
72+
[[nodiscard]]
73+
static float calc_direct_yaw_angle(const Vector3<float>& origin, const Vector3<float>& view_to) noexcept
74+
{
75+
const auto delta = view_to - origin;
76+
77+
return angles::radians_to_degrees(std::atan2(delta.y, delta.x));
78+
};
7179
};
7280
} // namespace omath::projectile_prediction::traits

include/omath/projectile_prediction/proj_pred_engine_legacy.hpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ namespace omath::projectile_prediction
3030
{
3131
for (float time = 0.f; time < m_maximum_simulation_time; time += m_simulation_time_step)
3232
{
33-
const auto predicted_target_position = target.predict_position(time, m_gravity_constant);
33+
const auto predicted_target_position =
34+
EngineTrait::predict_target_position(target, time, m_gravity_constant);
3435

3536
const auto projectile_pitch =
3637
maybe_calculate_projectile_launch_pitch_angle(projectile, predicted_target_position);
3738

3839
if (!projectile_pitch.has_value()) [[unlikely]]
3940
continue;
4041

41-
if (!EngineTrait::is_projectile_reached_target(predicted_target_position, projectile,
42-
projectile_pitch.value(), time, m_gravity_constant,
43-
m_distance_tolerance))
42+
if (!is_projectile_reached_target(predicted_target_position, projectile, projectile_pitch.value(),
43+
time))
4444
continue;
4545

4646
return EngineTrait::calc_viewpoint_from_angles(projectile, predicted_target_position, projectile_pitch);
@@ -76,7 +76,6 @@ namespace omath::projectile_prediction
7676
if (bullet_gravity == 0.f)
7777
return EngineTrait::calc_direct_pitch_angle(projectile.m_origin, target_position);
7878

79-
8079
const auto delta = target_position - projectile.m_origin;
8180

8281
const auto distance2d = EngineTrait::calc_vector_2d_distance(delta);
@@ -96,5 +95,15 @@ namespace omath::projectile_prediction
9695

9796
return angles::radians_to_degrees(angle);
9897
}
98+
[[nodiscard]]
99+
bool is_projectile_reached_target(const Vector3<float>& target_position, const Projectile& projectile,
100+
const float pitch, const float time) const noexcept
101+
{
102+
const auto yaw = EngineTrait::calc_direct_yaw_angle(projectile.m_origin, target_position);
103+
const auto projectile_position =
104+
EngineTrait::predict_projectile_position(projectile, pitch, yaw, time, m_gravity_constant);
105+
106+
return projectile_position.distance_to(target_position) <= m_distance_tolerance;
107+
}
99108
};
100109
} // namespace omath::projectile_prediction

include/omath/projectile_prediction/target.hpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,6 @@ namespace omath::projectile_prediction
1010
class Target final
1111
{
1212
public:
13-
[[nodiscard]]
14-
constexpr Vector3<float> predict_position(const float time, const float gravity) const noexcept
15-
{
16-
auto predicted = m_origin + m_velocity * time;
17-
18-
if (m_is_airborne)
19-
predicted.z -= gravity * (time*time) * 0.5f;
20-
21-
return predicted;
22-
}
23-
2413
Vector3<float> m_origin;
2514
Vector3<float> m_velocity;
2615
bool m_is_airborne{};

0 commit comments

Comments
 (0)