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.
This commit is contained in:
2025-08-04 03:16:04 +03:00
parent 7a5090d9f6
commit d12b236e56
3 changed files with 30 additions and 24 deletions

View File

@@ -5,6 +5,7 @@
#pragma once #pragma once
#include "omath/engines/source_engine/formulas.hpp" #include "omath/engines/source_engine/formulas.hpp"
#include "omath/projectile_prediction/projectile.hpp" #include "omath/projectile_prediction/projectile.hpp"
#include "omath/projectile_prediction/target.hpp"
#include <optional> #include <optional>
namespace omath::projectile_prediction::traits namespace omath::projectile_prediction::traits
@@ -25,16 +26,16 @@ namespace omath::projectile_prediction::traits
return current_pos; return current_pos;
} }
[[nodiscard]]
static bool is_projectile_reached_target(const Vector3<float>& target_position, static constexpr Vector3<float> predict_target_position(const Target& target, const float time,
const Projectile& projectile, const float pitch, const float gravity) noexcept
const float time, const float gravity,
const float tolerance) noexcept
{ {
const auto yaw = projectile.m_origin.view_angle_to(target_position).y; auto predicted = target.m_origin + target.m_velocity * time;
const auto projectile_position = predict_projectile_position(projectile, pitch, yaw, time, gravity);
return projectile_position.distance_to(target_position) <= tolerance; if (target.m_is_airborne)
predicted.z -= gravity * (time * time) * 0.5f;
return predicted;
} }
[[nodiscard]] [[nodiscard]]
static float calc_vector_2d_distance(const Vector3<float>& delta) noexcept static float calc_vector_2d_distance(const Vector3<float>& delta) noexcept
@@ -68,5 +69,12 @@ namespace omath::projectile_prediction::traits
return angles::radians_to_degrees(std::asin(delta.z / distance)); return angles::radians_to_degrees(std::asin(delta.z / distance));
} }
[[nodiscard]]
static float calc_direct_yaw_angle(const Vector3<float>& origin, const Vector3<float>& view_to) noexcept
{
const auto delta = view_to - origin;
return angles::radians_to_degrees(std::atan2(delta.y, delta.x));
};
}; };
} // namespace omath::projectile_prediction::traits } // namespace omath::projectile_prediction::traits

View File

@@ -30,7 +30,8 @@ namespace omath::projectile_prediction
{ {
for (float time = 0.f; time < m_maximum_simulation_time; time += m_simulation_time_step) for (float time = 0.f; time < m_maximum_simulation_time; time += m_simulation_time_step)
{ {
const auto predicted_target_position = target.predict_position(time, m_gravity_constant); const auto predicted_target_position =
EngineTrait::predict_target_position(target, time, m_gravity_constant);
const auto projectile_pitch = const auto projectile_pitch =
maybe_calculate_projectile_launch_pitch_angle(projectile, predicted_target_position); maybe_calculate_projectile_launch_pitch_angle(projectile, predicted_target_position);
@@ -38,9 +39,8 @@ namespace omath::projectile_prediction
if (!projectile_pitch.has_value()) [[unlikely]] if (!projectile_pitch.has_value()) [[unlikely]]
continue; continue;
if (!EngineTrait::is_projectile_reached_target(predicted_target_position, projectile, if (!is_projectile_reached_target(predicted_target_position, projectile, projectile_pitch.value(),
projectile_pitch.value(), time, m_gravity_constant, time))
m_distance_tolerance))
continue; continue;
return EngineTrait::calc_viewpoint_from_angles(projectile, predicted_target_position, projectile_pitch); return EngineTrait::calc_viewpoint_from_angles(projectile, predicted_target_position, projectile_pitch);
@@ -76,7 +76,6 @@ namespace omath::projectile_prediction
if (bullet_gravity == 0.f) if (bullet_gravity == 0.f)
return EngineTrait::calc_direct_pitch_angle(projectile.m_origin, target_position); return EngineTrait::calc_direct_pitch_angle(projectile.m_origin, target_position);
const auto delta = target_position - projectile.m_origin; const auto delta = target_position - projectile.m_origin;
const auto distance2d = EngineTrait::calc_vector_2d_distance(delta); const auto distance2d = EngineTrait::calc_vector_2d_distance(delta);
@@ -96,5 +95,15 @@ namespace omath::projectile_prediction
return angles::radians_to_degrees(angle); return angles::radians_to_degrees(angle);
} }
[[nodiscard]]
bool is_projectile_reached_target(const Vector3<float>& target_position, const Projectile& projectile,
const float pitch, const float time) const noexcept
{
const auto yaw = EngineTrait::calc_direct_yaw_angle(projectile.m_origin, target_position);
const auto projectile_position =
EngineTrait::predict_projectile_position(projectile, pitch, yaw, time, m_gravity_constant);
return projectile_position.distance_to(target_position) <= m_distance_tolerance;
}
}; };
} // namespace omath::projectile_prediction } // namespace omath::projectile_prediction

View File

@@ -10,17 +10,6 @@ namespace omath::projectile_prediction
class Target final class Target final
{ {
public: public:
[[nodiscard]]
constexpr Vector3<float> predict_position(const float time, const float gravity) const noexcept
{
auto predicted = m_origin + m_velocity * time;
if (m_is_airborne)
predicted.z -= gravity * (time*time) * 0.5f;
return predicted;
}
Vector3<float> m_origin; Vector3<float> m_origin;
Vector3<float> m_velocity; Vector3<float> m_velocity;
bool m_is_airborne{}; bool m_is_airborne{};