mirror of
https://github.com/orange-cpp/omath.git
synced 2026-08-08 05:52:07 +00:00
improved prediction
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
#include "omath/engines/source_engine/formulas.hpp"
|
||||
#include "omath/projectile_prediction/projectile.hpp"
|
||||
#include "omath/projectile_prediction/target.hpp"
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
|
||||
namespace omath::source_engine
|
||||
@@ -14,19 +16,58 @@ namespace omath::source_engine
|
||||
{
|
||||
public:
|
||||
[[nodiscard("projectile position result should not be discarded")]]
|
||||
constexpr static Vector3<float> predict_projectile_position(const projectile_prediction::Projectile<float>& projectile,
|
||||
const float pitch, const float yaw,
|
||||
const float time, const float gravity) noexcept
|
||||
constexpr static Vector3<float>
|
||||
predict_projectile_position(const projectile_prediction::Projectile<float>& projectile, const float pitch,
|
||||
const float yaw, const float time, const float gravity) noexcept
|
||||
{
|
||||
const auto launch_pos = projectile.m_origin + projectile.m_launch_offset;
|
||||
auto current_pos = launch_pos
|
||||
+ forward_vector({PitchAngle::from_degrees(-pitch), YawAngle::from_degrees(yaw),
|
||||
RollAngle::from_degrees(0)})
|
||||
* projectile.m_launch_speed * time;
|
||||
const auto pitch_angle = PitchAngle::from_degrees(-pitch);
|
||||
const auto yaw_angle = YawAngle::from_degrees(yaw);
|
||||
const auto pitch_cos = pitch_angle.cos();
|
||||
// Roll is always zero here, so this is the exact first column of the rotation matrix.
|
||||
const Vector3 forward{pitch_cos * yaw_angle.cos(), pitch_cos * yaw_angle.sin(), -pitch_angle.sin()};
|
||||
auto current_pos = launch_pos + forward * projectile.m_launch_speed * time;
|
||||
current_pos.z -= (gravity * projectile.m_gravity_scale) * (time * time) * 0.5f;
|
||||
|
||||
return current_pos;
|
||||
}
|
||||
|
||||
[[nodiscard("reachability result should not be discarded")]]
|
||||
static constexpr bool
|
||||
can_projectile_reach_target_at_time(const projectile_prediction::Projectile<float>& projectile,
|
||||
const Vector3<float>& target_position, const float time,
|
||||
const float gravity, const float distance_tolerance) noexcept
|
||||
{
|
||||
if (!(distance_tolerance >= 0.f))
|
||||
return false;
|
||||
|
||||
// After undoing gravity, every possible projectile position is on a sphere with radius speed * time.
|
||||
const auto launch_position = projectile.m_origin + projectile.m_launch_offset;
|
||||
auto adjusted_delta = target_position - launch_position;
|
||||
const auto gravity_displacement = (gravity * projectile.m_gravity_scale) * (time * time) * 0.5f;
|
||||
adjusted_delta.z += gravity_displacement;
|
||||
|
||||
const auto target_distance_sqr = adjusted_delta.length_sqr();
|
||||
const auto projectile_distance = std::abs(projectile.m_launch_speed * time);
|
||||
const auto floating_point_margin =
|
||||
std::numeric_limits<float>::epsilon() * 8.f
|
||||
* (std::abs(target_position.x) + std::abs(target_position.y) + std::abs(target_position.z)
|
||||
+ std::abs(launch_position.x) + std::abs(launch_position.y) + std::abs(launch_position.z)
|
||||
+ std::abs(gravity_displacement) + projectile_distance + distance_tolerance + 1.f);
|
||||
|
||||
if (!std::isfinite(target_distance_sqr) || !std::isfinite(floating_point_margin)) [[unlikely]]
|
||||
return true;
|
||||
|
||||
const auto conservative_tolerance = distance_tolerance + floating_point_margin;
|
||||
const auto maximum_distance = projectile_distance + conservative_tolerance;
|
||||
if (target_distance_sqr > maximum_distance * maximum_distance)
|
||||
return false;
|
||||
|
||||
const auto minimum_distance =
|
||||
projectile_distance > conservative_tolerance ? projectile_distance - conservative_tolerance : 0.f;
|
||||
return target_distance_sqr >= minimum_distance * minimum_distance;
|
||||
}
|
||||
|
||||
[[nodiscard("target position result should not be discarded")]]
|
||||
static constexpr Vector3<float> predict_target_position(const projectile_prediction::Target<float>& target,
|
||||
const float time, const float gravity) noexcept
|
||||
@@ -78,4 +119,4 @@ namespace omath::source_engine
|
||||
return angles::radians_to_degrees(std::atan2(delta.y, delta.x));
|
||||
};
|
||||
};
|
||||
} // namespace omath::source_engine
|
||||
} // namespace omath::source_engine
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "omath/projectile_prediction/proj_pred_engine.hpp"
|
||||
#include "omath/projectile_prediction/projectile.hpp"
|
||||
#include "omath/projectile_prediction/target.hpp"
|
||||
#include <cmath>
|
||||
#include <optional>
|
||||
|
||||
namespace omath::projectile_prediction
|
||||
@@ -17,9 +18,8 @@ namespace omath::projectile_prediction
|
||||
concept PredEngineConcept =
|
||||
requires(const Projectile<ArithmeticType>& projectile, const Target<ArithmeticType>& target,
|
||||
const Vector3<ArithmeticType>& vec_a, const Vector3<ArithmeticType>& vec_b,
|
||||
Vector3<ArithmeticType> v3,
|
||||
ArithmeticType pitch, ArithmeticType yaw, ArithmeticType time, ArithmeticType gravity,
|
||||
std::optional<ArithmeticType> maybe_pitch) {
|
||||
Vector3<ArithmeticType> v3, ArithmeticType pitch, ArithmeticType yaw, ArithmeticType time,
|
||||
ArithmeticType gravity, std::optional<ArithmeticType> maybe_pitch) {
|
||||
{
|
||||
T::predict_projectile_position(projectile, pitch, yaw, time, gravity)
|
||||
} -> std::same_as<Vector3<ArithmeticType>>;
|
||||
@@ -44,8 +44,7 @@ namespace omath::projectile_prediction
|
||||
class ProjPredEngineLegacy final : public ProjPredEngineInterface<ArithmeticType>
|
||||
{
|
||||
public:
|
||||
explicit ProjPredEngineLegacy(const ArithmeticType gravity_constant,
|
||||
const ArithmeticType simulation_time_step,
|
||||
explicit ProjPredEngineLegacy(const ArithmeticType gravity_constant, const ArithmeticType simulation_time_step,
|
||||
const ArithmeticType maximum_simulation_time,
|
||||
const ArithmeticType distance_tolerance)
|
||||
: m_gravity_constant(gravity_constant), m_simulation_time_step(simulation_time_step),
|
||||
@@ -54,8 +53,9 @@ namespace omath::projectile_prediction
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
std::optional<Vector3<ArithmeticType>> maybe_calculate_aim_point(
|
||||
const Projectile<ArithmeticType>& projectile, const Target<ArithmeticType>& target) const override
|
||||
std::optional<Vector3<ArithmeticType>>
|
||||
maybe_calculate_aim_point(const Projectile<ArithmeticType>& projectile,
|
||||
const Target<ArithmeticType>& target) const override
|
||||
{
|
||||
const auto solution = find_solution(projectile, target);
|
||||
if (!solution)
|
||||
@@ -66,15 +66,16 @@ namespace omath::projectile_prediction
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
std::optional<AimAngles<ArithmeticType>> maybe_calculate_aim_angles(
|
||||
const Projectile<ArithmeticType>& projectile, const Target<ArithmeticType>& target) const override
|
||||
std::optional<AimAngles<ArithmeticType>>
|
||||
maybe_calculate_aim_angles(const Projectile<ArithmeticType>& projectile,
|
||||
const Target<ArithmeticType>& target) const override
|
||||
{
|
||||
const auto solution = find_solution(projectile, target);
|
||||
if (!solution)
|
||||
return std::nullopt;
|
||||
|
||||
const auto yaw = EngineTrait::calc_direct_yaw_angle(
|
||||
projectile.m_origin + projectile.m_launch_offset, solution->predicted_target_position);
|
||||
const auto yaw = EngineTrait::calc_direct_yaw_angle(projectile.m_origin + projectile.m_launch_offset,
|
||||
solution->predicted_target_position);
|
||||
return AimAngles<ArithmeticType>{solution->pitch, yaw};
|
||||
}
|
||||
|
||||
@@ -89,23 +90,39 @@ namespace omath::projectile_prediction
|
||||
std::optional<Solution> find_solution(const Projectile<ArithmeticType>& projectile,
|
||||
const Target<ArithmeticType>& target) const
|
||||
{
|
||||
for (ArithmeticType time = ArithmeticType{0}; time < m_maximum_simulation_time;
|
||||
time += m_simulation_time_step)
|
||||
if (!std::isfinite(m_simulation_time_step) || m_simulation_time_step <= ArithmeticType{0}
|
||||
|| !std::isfinite(m_maximum_simulation_time) || m_maximum_simulation_time < ArithmeticType{0}
|
||||
|| !std::isfinite(projectile.m_launch_speed) || projectile.m_launch_speed <= ArithmeticType{0}
|
||||
|| !(m_distance_tolerance >= ArithmeticType{0})) [[unlikely]]
|
||||
return std::nullopt;
|
||||
|
||||
for (ArithmeticType time = ArithmeticType{0}; time <= m_maximum_simulation_time;)
|
||||
{
|
||||
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);
|
||||
if (is_target_potentially_reachable(projectile, predicted_target_position, time))
|
||||
{
|
||||
const auto projectile_pitch =
|
||||
maybe_calculate_projectile_launch_pitch_angle(projectile, predicted_target_position);
|
||||
|
||||
if (!projectile_pitch.has_value()) [[unlikely]]
|
||||
continue;
|
||||
if (projectile_pitch.has_value()) [[likely]]
|
||||
{
|
||||
const auto yaw = EngineTrait::calc_direct_yaw_angle(
|
||||
projectile.m_origin + projectile.m_launch_offset, predicted_target_position);
|
||||
|
||||
if (!is_projectile_reached_target(predicted_target_position, projectile, projectile_pitch.value(),
|
||||
time))
|
||||
continue;
|
||||
if (is_projectile_reached_target(predicted_target_position, projectile,
|
||||
projectile_pitch.value(), yaw, time))
|
||||
return Solution{predicted_target_position, projectile_pitch.value()};
|
||||
}
|
||||
}
|
||||
|
||||
return Solution{predicted_target_position, projectile_pitch.value()};
|
||||
if (time == m_maximum_simulation_time)
|
||||
break;
|
||||
const auto next_time = time + m_simulation_time_step;
|
||||
if (!(next_time > time)) [[unlikely]]
|
||||
break;
|
||||
time = next_time < m_maximum_simulation_time ? next_time : m_maximum_simulation_time;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
@@ -144,29 +161,46 @@ namespace omath::projectile_prediction
|
||||
const auto distance2d = EngineTrait::calc_vector_2d_distance(delta);
|
||||
const auto distance2d_sqr = distance2d * distance2d;
|
||||
const auto launch_speed_sqr = projectile.m_launch_speed * projectile.m_launch_speed;
|
||||
const auto ballistic_term =
|
||||
bullet_gravity * distance2d_sqr
|
||||
+ ArithmeticType{2} * EngineTrait::get_vector_height_coordinate(delta) * launch_speed_sqr;
|
||||
|
||||
ArithmeticType root = launch_speed_sqr * launch_speed_sqr
|
||||
- bullet_gravity
|
||||
* (bullet_gravity * distance2d_sqr
|
||||
+ ArithmeticType{2} * EngineTrait::get_vector_height_coordinate(delta)
|
||||
* launch_speed_sqr);
|
||||
ArithmeticType root = launch_speed_sqr * launch_speed_sqr - bullet_gravity * ballistic_term;
|
||||
|
||||
if (root < ArithmeticType{0}) [[unlikely]]
|
||||
return std::nullopt;
|
||||
|
||||
root = std::sqrt(root);
|
||||
const ArithmeticType angle = std::atan((launch_speed_sqr - root) / (bullet_gravity * distance2d));
|
||||
// This rationalized form avoids cancellation in launch_speed_sqr - root for low-angle shots.
|
||||
const ArithmeticType angle = std::atan2(ballistic_term, distance2d * (launch_speed_sqr + root));
|
||||
|
||||
return angles::radians_to_degrees(angle);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
bool is_projectile_reached_target(const Vector3<ArithmeticType>& target_position,
|
||||
const Projectile<ArithmeticType>& projectile,
|
||||
const ArithmeticType pitch, const ArithmeticType time) const noexcept
|
||||
bool is_target_potentially_reachable(const Projectile<ArithmeticType>& projectile,
|
||||
const Vector3<ArithmeticType>& target_position,
|
||||
const ArithmeticType time) const noexcept
|
||||
{
|
||||
if constexpr (requires {
|
||||
{
|
||||
EngineTrait::can_projectile_reach_target_at_time(
|
||||
projectile, target_position, time, m_gravity_constant, m_distance_tolerance)
|
||||
} -> std::same_as<bool>;
|
||||
requires noexcept(EngineTrait::can_projectile_reach_target_at_time(
|
||||
projectile, target_position, time, m_gravity_constant, m_distance_tolerance));
|
||||
})
|
||||
return EngineTrait::can_projectile_reach_target_at_time(projectile, target_position, time,
|
||||
m_gravity_constant, m_distance_tolerance);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
bool is_projectile_reached_target(const Vector3<ArithmeticType>& target_position,
|
||||
const Projectile<ArithmeticType>& projectile, const ArithmeticType pitch,
|
||||
const ArithmeticType yaw, const ArithmeticType time) const noexcept
|
||||
{
|
||||
const auto yaw = EngineTrait::calc_direct_yaw_angle(
|
||||
projectile.m_origin + projectile.m_launch_offset, target_position);
|
||||
const auto projectile_position =
|
||||
EngineTrait::predict_projectile_position(projectile, pitch, yaw, time, m_gravity_constant);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user