mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 15:03:27 +00:00
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:
@@ -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 <optional>
|
||||
|
||||
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<float>& target_position,
|
||||
const Projectile& projectile, const float pitch,
|
||||
const float time, const float gravity,
|
||||
const float tolerance) noexcept
|
||||
[[nodiscard]]
|
||||
static constexpr Vector3<float> 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<float>& 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<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
|
||||
@@ -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<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
|
||||
|
||||
@@ -10,17 +10,6 @@ namespace omath::projectile_prediction
|
||||
class Target final
|
||||
{
|
||||
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_velocity;
|
||||
bool m_is_airborne{};
|
||||
|
||||
Reference in New Issue
Block a user