Skip to content

Commit f1984fb

Browse files
authored
Merge pull request #51 from orange-cpp/feature/projectile_pred_custom
Refactors projectile prediction engine
2 parents 4b44ce0 + f1fbea2 commit f1984fb

File tree

6 files changed

+63
-26
lines changed

6 files changed

+63
-26
lines changed

include/omath/projectile_prediction/proj_pred_engine.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
namespace omath::projectile_prediction
1010
{
11-
class ProjPredEngine
11+
class ProjPredEngineInterface
1212
{
1313
public:
1414
[[nodiscard]]
1515
virtual std::optional<Vector3<float>> maybe_calculate_aim_point(const Projectile& projectile,
1616
const Target& target) const = 0;
17-
virtual ~ProjPredEngine() = default;
17+
virtual ~ProjPredEngineInterface() = default;
1818
};
1919
} // namespace omath::projectile_prediction

include/omath/projectile_prediction/proj_pred_engine_avx2.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace omath::projectile_prediction
88
{
9-
class ProjPredEngineAvx2 final : public ProjPredEngine
9+
class ProjPredEngineAvx2 final : public ProjPredEngineInterface
1010
{
1111
public:
1212
[[nodiscard]] std::optional<Vector3<float>>

include/omath/projectile_prediction/proj_pred_engine_legacy.hpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
namespace omath::projectile_prediction
1414
{
15-
class ProjPredEngineLegacy final : public ProjPredEngine
15+
// ReSharper disable once CppClassCanBeFinal
16+
class ProjPredEngineLegacy : public ProjPredEngineInterface
1617
{
1718
public:
1819
explicit ProjPredEngineLegacy(float gravity_constant, float simulation_time_step, float maximum_simulation_time,
@@ -48,5 +49,25 @@ namespace omath::projectile_prediction
4849
[[nodiscard]]
4950
bool is_projectile_reached_target(const Vector3<float>& target_position, const Projectile& projectile,
5051
float pitch, float time) const noexcept;
52+
53+
protected:
54+
// NOTE: Override this if you need to use engine with different coordinate system
55+
// Like where Z is not height coordinate
56+
// ===============================================================================================
57+
[[nodiscard]]
58+
virtual float calc_vector_2d_distance(const Vector3<float>& delta) const;
59+
60+
[[nodiscard]]
61+
virtual float get_vector_height_coordinate(const Vector3<float>& vec) const;
62+
63+
[[nodiscard]]
64+
virtual Vector3<float> calc_viewpoint_from_angles(const Projectile& projectile,
65+
Vector3<float> predicted_target_position,
66+
std::optional<float> projectile_pitch) const;
67+
68+
[[nodiscard]]
69+
virtual Vector3<float> predict_projectile_position(const Projectile& projectile, float pitch, float yaw,
70+
float time, float gravity) const;
71+
// ===============================================================================================
5172
};
5273
} // namespace omath::projectile_prediction

include/omath/projectile_prediction/projectile.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ namespace omath::projectile_prediction
1010
class Projectile final
1111
{
1212
public:
13-
[[nodiscard]]
14-
Vector3<float> predict_position(float pitch, float yaw, float time, float gravity) const noexcept;
15-
1613
Vector3<float> m_origin;
1714
float m_launch_speed{};
1815
float m_gravity_scale{};

source/projectile_prediction/proj_pred_engine_legacy.cpp

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ namespace omath::projectile_prediction
2727
if (!is_projectile_reached_target(predicted_target_position, projectile, projectile_pitch.value(), time))
2828
continue;
2929

30-
const auto delta2d = (predicted_target_position - projectile.m_origin).length_2d();
31-
const auto height = delta2d * std::tan(angles::degrees_to_radians(projectile_pitch.value()));
32-
33-
return Vector3(predicted_target_position.x, predicted_target_position.y, projectile.m_origin.z + height);
30+
return calc_viewpoint_from_angles(projectile, predicted_target_position, projectile_pitch);
3431
}
3532
return std::nullopt;
3633
}
@@ -41,12 +38,14 @@ namespace omath::projectile_prediction
4138
const auto bullet_gravity = m_gravity_constant * projectile.m_gravity_scale;
4239
const auto delta = target_position - projectile.m_origin;
4340

44-
const auto distance2d = delta.length_2d();
41+
const auto distance2d = calc_vector_2d_distance(delta);
4542
const auto distance2d_sqr = distance2d * distance2d;
4643
const auto launch_speed_sqr = projectile.m_launch_speed * projectile.m_launch_speed;
4744

4845
float root = launch_speed_sqr * launch_speed_sqr
49-
- bullet_gravity * (bullet_gravity * distance2d_sqr + 2.0f * delta.z * launch_speed_sqr);
46+
- bullet_gravity
47+
* (bullet_gravity * distance2d_sqr
48+
+ 2.0f * get_vector_height_coordinate(delta) * launch_speed_sqr);
5049

5150
if (root < 0.0f) [[unlikely]]
5251
return std::nullopt;
@@ -62,8 +61,40 @@ namespace omath::projectile_prediction
6261
const float time) const noexcept
6362
{
6463
const auto yaw = projectile.m_origin.view_angle_to(target_position).y;
65-
const auto projectile_position = projectile.predict_position(pitch, yaw, time, m_gravity_constant);
64+
const auto projectile_position = predict_projectile_position(projectile, pitch, yaw, time, m_gravity_constant);
6665

6766
return projectile_position.distance_to(target_position) <= m_distance_tolerance;
6867
}
68+
69+
float ProjPredEngineLegacy::calc_vector_2d_distance(const Vector3<float>& delta) const
70+
{
71+
return std::sqrt(delta.x * delta.x + delta.y * delta.y);
72+
}
73+
74+
float ProjPredEngineLegacy::get_vector_height_coordinate(const Vector3<float>& vec) const
75+
{
76+
return vec.z;
77+
}
78+
Vector3<float> ProjPredEngineLegacy::calc_viewpoint_from_angles(const Projectile& projectile,
79+
const Vector3<float> predicted_target_position,
80+
const std::optional<float> projectile_pitch) const
81+
{
82+
const auto delta2d = calc_vector_2d_distance(predicted_target_position - projectile.m_origin);
83+
const auto height = delta2d * std::tan(angles::degrees_to_radians(projectile_pitch.value()));
84+
85+
return {predicted_target_position.x, predicted_target_position.y, projectile.m_origin.z + height};
86+
}
87+
Vector3<float> ProjPredEngineLegacy::predict_projectile_position(const Projectile& projectile, const float pitch,
88+
const float yaw, const float time,
89+
const float gravity) const
90+
{
91+
auto current_pos = projectile.m_origin
92+
+ source_engine::forward_vector({source_engine::PitchAngle::from_degrees(-pitch),
93+
source_engine::YawAngle::from_degrees(yaw),
94+
source_engine::RollAngle::from_degrees(0)})
95+
* projectile.m_launch_speed * time;
96+
current_pos.z -= (gravity * projectile.m_gravity_scale) * (time * time) * 0.5f;
97+
98+
return current_pos;
99+
}
69100
} // namespace omath::projectile_prediction

source/projectile_prediction/projectile.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,4 @@
77

88
namespace omath::projectile_prediction
99
{
10-
Vector3<float> Projectile::predict_position(const float pitch, const float yaw, const float time,
11-
const float gravity) const noexcept
12-
{
13-
auto current_pos = m_origin
14-
+ source_engine::forward_vector({source_engine::PitchAngle::from_degrees(-pitch),
15-
source_engine::YawAngle::from_degrees(yaw),
16-
source_engine::RollAngle::from_degrees(0)})
17-
* m_launch_speed * time;
18-
current_pos.z -= (gravity * m_gravity_scale) * (time * time) * 0.5f;
19-
20-
return current_pos;
21-
}
2210
} // namespace omath::projectile_prediction

0 commit comments

Comments
 (0)