diff --git a/include/omath/projectile_prediction/engine_traits/source_engine_trait.hpp b/include/omath/projectile_prediction/engine_traits/source_engine_trait.hpp index 1d4b113..d124a92 100644 --- a/include/omath/projectile_prediction/engine_traits/source_engine_trait.hpp +++ b/include/omath/projectile_prediction/engine_traits/source_engine_trait.hpp @@ -5,6 +5,7 @@ #pragma once #include "omath/engines/source_engine/formulas.hpp" #include "omath/projectile_prediction/projectile.hpp" +#include "omath/projectile_prediction/target.hpp" #include namespace omath::projectile_prediction::traits @@ -25,16 +26,16 @@ namespace omath::projectile_prediction::traits return current_pos; } - - static bool is_projectile_reached_target(const Vector3& target_position, - const Projectile& projectile, const float pitch, - const float time, const float gravity, - const float tolerance) noexcept + [[nodiscard]] + static constexpr Vector3 predict_target_position(const Target& target, const float time, + const float gravity) noexcept { - const auto yaw = projectile.m_origin.view_angle_to(target_position).y; - const auto projectile_position = predict_projectile_position(projectile, pitch, yaw, time, gravity); + auto predicted = target.m_origin + target.m_velocity * time; - return projectile_position.distance_to(target_position) <= tolerance; + if (target.m_is_airborne) + predicted.z -= gravity * (time * time) * 0.5f; + + return predicted; } [[nodiscard]] static float calc_vector_2d_distance(const Vector3& delta) noexcept @@ -68,5 +69,12 @@ namespace omath::projectile_prediction::traits return angles::radians_to_degrees(std::asin(delta.z / distance)); } + [[nodiscard]] + static float calc_direct_yaw_angle(const Vector3& origin, const Vector3& 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 \ No newline at end of file diff --git a/include/omath/projectile_prediction/proj_pred_engine_legacy.hpp b/include/omath/projectile_prediction/proj_pred_engine_legacy.hpp index 69a8582..801f719 100644 --- a/include/omath/projectile_prediction/proj_pred_engine_legacy.hpp +++ b/include/omath/projectile_prediction/proj_pred_engine_legacy.hpp @@ -30,7 +30,8 @@ namespace omath::projectile_prediction { 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 = maybe_calculate_projectile_launch_pitch_angle(projectile, predicted_target_position); @@ -38,9 +39,8 @@ namespace omath::projectile_prediction if (!projectile_pitch.has_value()) [[unlikely]] continue; - if (!EngineTrait::is_projectile_reached_target(predicted_target_position, projectile, - projectile_pitch.value(), time, m_gravity_constant, - m_distance_tolerance)) + if (!is_projectile_reached_target(predicted_target_position, projectile, projectile_pitch.value(), + time)) continue; 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) return EngineTrait::calc_direct_pitch_angle(projectile.m_origin, target_position); - const auto delta = target_position - projectile.m_origin; const auto distance2d = EngineTrait::calc_vector_2d_distance(delta); @@ -96,5 +95,15 @@ namespace omath::projectile_prediction return angles::radians_to_degrees(angle); } + [[nodiscard]] + bool is_projectile_reached_target(const Vector3& 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 diff --git a/include/omath/projectile_prediction/target.hpp b/include/omath/projectile_prediction/target.hpp index 4dd38b0..5b1b0f8 100644 --- a/include/omath/projectile_prediction/target.hpp +++ b/include/omath/projectile_prediction/target.hpp @@ -10,17 +10,6 @@ namespace omath::projectile_prediction class Target final { public: - [[nodiscard]] - constexpr Vector3 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 m_origin; Vector3 m_velocity; bool m_is_airborne{};