Skip to content

Commit ec76a72

Browse files
committed
Adds direct pitch angle calculation
Implements a direct pitch angle calculation for scenarios with zero gravity, ensuring accurate projectile trajectory predictions in such conditions. Also marks several methods as noexcept for better performance and exception safety.
1 parent 2758f54 commit ec76a72

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

include/omath/projectile_prediction/engine_traits/source_engine_trait.hpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,36 @@ namespace omath::projectile_prediction::traits
3737
return projectile_position.distance_to(target_position) <= tolerance;
3838
}
3939
[[nodiscard]]
40-
static float calc_vector_2d_distance(const Vector3<float>& delta)
40+
static float calc_vector_2d_distance(const Vector3<float>& delta) noexcept
4141
{
4242
return std::sqrt(delta.x * delta.x + delta.y * delta.y);
4343
}
4444

4545
[[nodiscard]]
46-
constexpr static float get_vector_height_coordinate(const Vector3<float>& vec)
46+
constexpr static float get_vector_height_coordinate(const Vector3<float>& vec) noexcept
4747
{
4848
return vec.z;
4949
}
5050

5151
[[nodiscard]]
5252
static Vector3<float> calc_viewpoint_from_angles(const Projectile& projectile,
5353
Vector3<float> predicted_target_position,
54-
const std::optional<float> projectile_pitch)
54+
const std::optional<float> projectile_pitch) noexcept
5555
{
5656
const auto delta2d = calc_vector_2d_distance(predicted_target_position - projectile.m_origin);
5757
const auto height = delta2d * std::tan(angles::degrees_to_radians(projectile_pitch.value()));
5858

5959
return {predicted_target_position.x, predicted_target_position.y, projectile.m_origin.z + height};
6060
}
61+
// Due to specification of maybe_calculate_projectile_launch_pitch_angle, pitch angle must be:
62+
// 89 look up, -89 look down
63+
[[nodiscard]]
64+
static float calc_direct_pitch_angle(const Vector3<float>& origin, const Vector3<float>& view_to) noexcept
65+
{
66+
const auto distance = origin.distance_to(view_to);
67+
const auto delta = view_to - origin;
68+
69+
return angles::radians_to_degrees(std::asin(delta.z / distance));
70+
}
6171
};
6272
} // namespace omath::projectile_prediction::traits

include/omath/projectile_prediction/proj_pred_engine_legacy.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ namespace omath::projectile_prediction
7373
const Vector3<float>& target_position) const noexcept
7474
{
7575
const auto bullet_gravity = m_gravity_constant * projectile.m_gravity_scale;
76+
77+
if (bullet_gravity == 0.f)
78+
return EngineTrait::calc_direct_pitch_angle(projectile.m_origin, target_position);
79+
80+
7681
const auto delta = target_position - projectile.m_origin;
7782

7883
const auto distance2d = EngineTrait::calc_vector_2d_distance(delta);

0 commit comments

Comments
 (0)