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:
@@ -1,4 +1,5 @@
|
||||
// Tests for PredEngineTrait
|
||||
#include <array>
|
||||
#include <gtest/gtest.h>
|
||||
#include <omath/engines/source_engine/traits/pred_engine_trait.hpp>
|
||||
#include <omath/projectile_prediction/projectile.hpp>
|
||||
@@ -77,6 +78,77 @@ TEST(PredEngineTrait, PredictProjectilePositionWithLaunchOffset)
|
||||
EXPECT_NEAR(pos_t1.z, -2.f - 9.81f * 0.5f, 1e-3f);
|
||||
}
|
||||
|
||||
TEST(PredEngineTrait, PredictProjectilePositionMatchesRotationMatrix)
|
||||
{
|
||||
constexpr Projectile projectile{
|
||||
.m_origin = {10.f, -20.f, 30.f},
|
||||
.m_launch_offset = {2.f, 3.f, -4.f},
|
||||
.m_launch_speed = 750.f,
|
||||
.m_gravity_scale = 0.6f,
|
||||
};
|
||||
struct TestCase
|
||||
{
|
||||
float pitch;
|
||||
float yaw;
|
||||
float time;
|
||||
};
|
||||
constexpr std::array test_cases{
|
||||
TestCase{0.f, 0.f, 0.f},
|
||||
TestCase{25.f, 45.f, 0.25f},
|
||||
TestCase{-60.f, -135.f, 1.5f},
|
||||
TestCase{120.f, 540.f, 2.f},
|
||||
};
|
||||
constexpr float gravity = 9.81f;
|
||||
|
||||
for (const auto& test_case : test_cases)
|
||||
{
|
||||
const auto launch_position = projectile.m_origin + projectile.m_launch_offset;
|
||||
auto expected_position = launch_position
|
||||
+ forward_vector({PitchAngle::from_degrees(-test_case.pitch),
|
||||
YawAngle::from_degrees(test_case.yaw), RollAngle::from_degrees(0.f)})
|
||||
* projectile.m_launch_speed * test_case.time;
|
||||
expected_position.z -= gravity * projectile.m_gravity_scale * test_case.time * test_case.time * 0.5f;
|
||||
|
||||
const auto actual_position = PredEngineTrait::predict_projectile_position(
|
||||
projectile, test_case.pitch, test_case.yaw, test_case.time, gravity);
|
||||
|
||||
EXPECT_NEAR(actual_position.x, expected_position.x, 1e-5f);
|
||||
EXPECT_NEAR(actual_position.y, expected_position.y, 1e-5f);
|
||||
EXPECT_NEAR(actual_position.z, expected_position.z, 1e-5f);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(PredEngineTrait, ReachabilityCheckUsesDistanceTolerance)
|
||||
{
|
||||
constexpr Projectile projectile{
|
||||
.m_origin = {0.f, 0.f, 0.f},
|
||||
.m_launch_speed = 100.f,
|
||||
.m_gravity_scale = 0.f,
|
||||
};
|
||||
|
||||
EXPECT_FALSE(PredEngineTrait::can_projectile_reach_target_at_time(projectile, {100.f, 0.f, 0.f}, 0.5f, 9.81f, 0.f));
|
||||
EXPECT_TRUE(PredEngineTrait::can_projectile_reach_target_at_time(projectile, {100.f, 0.f, 0.f}, 1.f, 9.81f, 0.f));
|
||||
EXPECT_TRUE(PredEngineTrait::can_projectile_reach_target_at_time(projectile, {101.f, 0.f, 0.f}, 1.f, 9.81f, 1.f));
|
||||
EXPECT_TRUE(PredEngineTrait::can_projectile_reach_target_at_time(projectile, {99.f, 0.f, 0.f}, 1.f, 9.81f, 1.f));
|
||||
EXPECT_FALSE(PredEngineTrait::can_projectile_reach_target_at_time(projectile, {101.f, 0.f, 0.f}, 1.f, 9.81f, 0.f));
|
||||
EXPECT_FALSE(PredEngineTrait::can_projectile_reach_target_at_time(projectile, {98.f, 0.f, 0.f}, 1.f, 9.81f, 1.f));
|
||||
}
|
||||
|
||||
TEST(PredEngineTrait, ReachabilityCheckIncludesFloatingPointError)
|
||||
{
|
||||
constexpr Projectile projectile{
|
||||
.m_origin = {1.f, 2.f, 3.f},
|
||||
.m_launch_offset = {0.1f, -0.2f, 0.3f},
|
||||
.m_launch_speed = 100.f,
|
||||
.m_gravity_scale = 1.f,
|
||||
};
|
||||
constexpr float gravity = 9.81f;
|
||||
constexpr float time = 0.3f;
|
||||
const auto target_position = PredEngineTrait::predict_projectile_position(projectile, 25.f, 45.f, time, gravity);
|
||||
|
||||
EXPECT_TRUE(PredEngineTrait::can_projectile_reach_target_at_time(projectile, target_position, time, gravity, 0.f));
|
||||
}
|
||||
|
||||
TEST(PredEngineTrait, ZeroLaunchOffsetMatchesOriginalBehavior)
|
||||
{
|
||||
Projectile p;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <limits>
|
||||
#include <omath/projectile_prediction/proj_pred_engine_legacy.hpp>
|
||||
#include <omath/projectile_prediction/projectile.hpp>
|
||||
#include <omath/projectile_prediction/target.hpp>
|
||||
@@ -15,25 +16,45 @@ struct FakeEngineZeroGravity
|
||||
{
|
||||
return t.m_origin;
|
||||
}
|
||||
static Vector3<float> predict_projectile_position(const Projectile& /*p*/, float /*pitch*/, float /*yaw*/, float /*time*/, float /*gravity*/) noexcept
|
||||
static Vector3<float> predict_projectile_position(const Projectile& /*p*/, float /*pitch*/, float /*yaw*/,
|
||||
float /*time*/, float /*gravity*/) noexcept
|
||||
{
|
||||
// Return a fixed point matching typical target used in the test
|
||||
return Vector3<float>{100.f, 0.f, 0.f};
|
||||
}
|
||||
static float calc_vector_2d_distance(const Vector3<float>& v) noexcept { return std::hypot(v.x, v.y); }
|
||||
static float get_vector_height_coordinate(const Vector3<float>& v) noexcept { return v.z; }
|
||||
static Vector3<float> calc_viewpoint_from_angles(const Projectile& /*p*/, Vector3<float> /*v*/, std::optional<float> /*maybe_pitch*/) noexcept
|
||||
static float calc_vector_2d_distance(const Vector3<float>& v) noexcept
|
||||
{
|
||||
return std::hypot(v.x, v.y);
|
||||
}
|
||||
static float get_vector_height_coordinate(const Vector3<float>& v) noexcept
|
||||
{
|
||||
return v.z;
|
||||
}
|
||||
static Vector3<float> calc_viewpoint_from_angles(const Projectile& /*p*/, Vector3<float> /*v*/,
|
||||
std::optional<float> /*maybe_pitch*/) noexcept
|
||||
{
|
||||
return Vector3<float>{1.f, 2.f, 3.f};
|
||||
}
|
||||
static float calc_direct_pitch_angle(const Vector3<float>& /*a*/, const Vector3<float>& /*b*/) noexcept { return 12.5f; }
|
||||
static float calc_direct_yaw_angle(const Vector3<float>& /*a*/, const Vector3<float>& /*b*/) noexcept { return 0.f; }
|
||||
static float calc_direct_pitch_angle(const Vector3<float>& /*a*/, const Vector3<float>& /*b*/) noexcept
|
||||
{
|
||||
return 12.5f;
|
||||
}
|
||||
static float calc_direct_yaw_angle(const Vector3<float>& /*a*/, const Vector3<float>& /*b*/) noexcept
|
||||
{
|
||||
return 0.f;
|
||||
}
|
||||
static bool can_projectile_reach_target_at_time(const Projectile& /*projectile*/,
|
||||
const Vector3<float>& /*target_position*/, float /*time*/,
|
||||
float /*gravity*/, float /*distance_tolerance*/)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
TEST(ProjPredLegacyMore, ZeroGravityUsesDirectPitchAndReturnsViewpoint)
|
||||
{
|
||||
constexpr Projectile proj{ .m_origin = {0.f, 0.f, 0.f}, .m_launch_speed = 10.f, .m_gravity_scale = 0.f };
|
||||
constexpr Target target{ .m_origin = {100.f, 0.f, 0.f}, .m_velocity = {0.f,0.f,0.f}, .m_is_airborne = false };
|
||||
constexpr Projectile proj{.m_origin = {0.f, 0.f, 0.f}, .m_launch_speed = 10.f, .m_gravity_scale = 0.f};
|
||||
constexpr Target target{.m_origin = {100.f, 0.f, 0.f}, .m_velocity = {0.f, 0.f, 0.f}, .m_is_airborne = false};
|
||||
|
||||
using Engine = omath::projectile_prediction::ProjPredEngineLegacy<FakeEngineZeroGravity>;
|
||||
const Engine engine(9.8f, 0.1f, 5.f, 1e-3f);
|
||||
@@ -117,3 +138,77 @@ TEST(ProjPredLegacyMore, AngleComputedButMissReturnsNullopt)
|
||||
const auto res = engine.maybe_calculate_aim_point(proj, target);
|
||||
EXPECT_FALSE(res.has_value());
|
||||
}
|
||||
|
||||
TEST(ProjPredLegacyMore, IncludesMaximumSimulationTime)
|
||||
{
|
||||
constexpr Projectile projectile{.m_origin = {0.f, 0.f, 0.f}, .m_launch_speed = 10.f, .m_gravity_scale = 0.f};
|
||||
constexpr Target target{.m_origin = {10.f, 0.f, 0.f}, .m_velocity = {0.f, 0.f, 0.f}, .m_is_airborne = false};
|
||||
const omath::projectile_prediction::ProjPredEngineLegacy<> engine(0.f, 0.1f, 1.f, 0.f);
|
||||
|
||||
const auto result = engine.maybe_calculate_aim_point(projectile, target);
|
||||
|
||||
ASSERT_TRUE(result.has_value());
|
||||
EXPECT_NEAR(result->x, target.m_origin.x, 1e-6f);
|
||||
EXPECT_NEAR(result->y, target.m_origin.y, 1e-6f);
|
||||
EXPECT_NEAR(result->z, target.m_origin.z, 1e-6f);
|
||||
}
|
||||
|
||||
TEST(ProjPredLegacyMore, RejectsInvalidSimulationSteps)
|
||||
{
|
||||
constexpr Projectile projectile{.m_origin = {0.f, 0.f, 0.f}, .m_launch_speed = 10.f, .m_gravity_scale = 0.f};
|
||||
constexpr Target target{.m_origin = {10.f, 0.f, 0.f}, .m_velocity = {0.f, 0.f, 0.f}, .m_is_airborne = false};
|
||||
|
||||
EXPECT_FALSE(omath::projectile_prediction::ProjPredEngineLegacy<>(0.f, 0.f, 1.f, 1.f)
|
||||
.maybe_calculate_aim_point(projectile, target));
|
||||
EXPECT_FALSE(omath::projectile_prediction::ProjPredEngineLegacy<>(0.f, -0.1f, 1.f, 1.f)
|
||||
.maybe_calculate_aim_point(projectile, target));
|
||||
EXPECT_FALSE(
|
||||
omath::projectile_prediction::ProjPredEngineLegacy<>(0.f, std::numeric_limits<float>::infinity(), 1.f, 1.f)
|
||||
.maybe_calculate_aim_point(projectile, target));
|
||||
}
|
||||
|
||||
TEST(ProjPredLegacyMore, RejectsInvalidProjectileSpeedAndTolerance)
|
||||
{
|
||||
constexpr Target target{.m_origin = {1.f, 0.f, 0.f}, .m_velocity = {0.f, 0.f, 0.f}, .m_is_airborne = false};
|
||||
constexpr Projectile stopped_projectile{.m_origin = {0.f, 0.f, 0.f}, .m_launch_speed = 0.f, .m_gravity_scale = 0.f};
|
||||
constexpr Projectile moving_projectile{.m_origin = {0.f, 0.f, 0.f}, .m_launch_speed = 10.f, .m_gravity_scale = 0.f};
|
||||
|
||||
EXPECT_FALSE(omath::projectile_prediction::ProjPredEngineLegacy<>(0.f, 0.1f, 1.f, 2.f)
|
||||
.maybe_calculate_aim_point(stopped_projectile, target));
|
||||
EXPECT_FALSE(omath::projectile_prediction::ProjPredEngineLegacy<>(0.f, 0.1f, 1.f, -1.f)
|
||||
.maybe_calculate_aim_point(moving_projectile, target));
|
||||
}
|
||||
|
||||
TEST(ProjPredLegacyMore, StablePitchFindsHighSpeedLowArc)
|
||||
{
|
||||
constexpr Projectile projectile{.m_origin = {0.f, 0.f, 0.f}, .m_launch_speed = 10'000.f, .m_gravity_scale = 1.f};
|
||||
constexpr Target target{.m_origin = {100.f, 0.f, 0.f}, .m_velocity = {0.f, 0.f, 0.f}, .m_is_airborne = false};
|
||||
const omath::projectile_prediction::ProjPredEngineLegacy<> engine(9.81f, 0.01f, 0.02f, 0.0001f);
|
||||
|
||||
const auto result = engine.maybe_calculate_aim_angles(projectile, target);
|
||||
|
||||
ASSERT_TRUE(result.has_value());
|
||||
EXPECT_GT(result->pitch, 0.f);
|
||||
EXPECT_NEAR(result->yaw, 0.f, 1e-6f);
|
||||
}
|
||||
|
||||
TEST(ProjPredLegacyMore, CoincidentTargetReturnsLaunchOrigin)
|
||||
{
|
||||
constexpr Projectile projectile{.m_origin = {5.f, 4.f, 3.f}, .m_launch_speed = 100.f, .m_gravity_scale = 1.f};
|
||||
constexpr Target target{.m_origin = projectile.m_origin, .m_velocity = {0.f, 0.f, 0.f}, .m_is_airborne = false};
|
||||
const omath::projectile_prediction::ProjPredEngineLegacy<> engine(9.81f, 0.01f, 0.01f, 0.f);
|
||||
|
||||
const auto result = engine.maybe_calculate_aim_point(projectile, target);
|
||||
|
||||
ASSERT_TRUE(result.has_value());
|
||||
EXPECT_EQ(result.value(), projectile.m_origin);
|
||||
}
|
||||
|
||||
TEST(ProjPredLegacyMore, TinyDistanceDoesNotUnderflowToAHit)
|
||||
{
|
||||
constexpr Projectile projectile{.m_origin = {0.f, 0.f, 0.f}, .m_launch_speed = 1.f, .m_gravity_scale = 0.f};
|
||||
constexpr Target target{.m_origin = {1e-30f, 0.f, 0.f}, .m_velocity = {0.f, 0.f, 0.f}, .m_is_airborne = false};
|
||||
const omath::projectile_prediction::ProjPredEngineLegacy<> engine(0.f, 0.1f, 0.f, 0.f);
|
||||
|
||||
EXPECT_FALSE(engine.maybe_calculate_aim_point(projectile, target));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user