mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
added more classes
This commit is contained in:
@@ -6,10 +6,10 @@
|
|||||||
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include "omath/Vector3.hpp"
|
#include "omath/Vector3.hpp"
|
||||||
#include "omath/prediction/Projectile.hpp"
|
#include "omath/projectile_prediction/Projectile.hpp"
|
||||||
#include "omath/prediction/Target.hpp"
|
#include "omath/projectile_prediction/Target.hpp"
|
||||||
|
|
||||||
namespace omath::prediction
|
namespace omath::projectile_prediction
|
||||||
{
|
{
|
||||||
class Engine final
|
class Engine final
|
||||||
{
|
{
|
||||||
@@ -26,15 +26,8 @@ namespace omath::prediction
|
|||||||
const float m_maximumSimulationTime;
|
const float m_maximumSimulationTime;
|
||||||
const float m_distanceTolerance;
|
const float m_distanceTolerance;
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
std::optional<float> MaybeCalculateProjectileLaunchPitchAngle(const Projectile& projectile,
|
|
||||||
const Vector3& targetPosition) const;
|
|
||||||
|
|
||||||
|
|
||||||
[[nodiscard]] static std::optional<float> CalculatePitch(const Vector3 &projOrigin, const Vector3 &targetPos,
|
[[nodiscard]] static std::optional<float> CalculatePitch(const Vector3 &projOrigin, const Vector3 &targetPos,
|
||||||
float bulletGravity, float v0, float time);
|
float bulletGravity, float v0, float time);
|
||||||
[[nodiscard]]
|
|
||||||
bool IsProjectileReachedTarget(const Vector3& targetPosition, const Projectile& projectile, float pitch, float time) const;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
18
include/omath/projectile_prediction/ProjPredEngine.hpp
Normal file
18
include/omath/projectile_prediction/ProjPredEngine.hpp
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
//
|
||||||
|
// Created by Vlad on 2/23/2025.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#include "omath/Vector3.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
namespace omath::projectile_prediction
|
||||||
|
{
|
||||||
|
class ProjPredEngine
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
[[nodiscard]]
|
||||||
|
virtual std::optional<Vector3> MaybeCalculateAimPoint(const Projectile& projectile,
|
||||||
|
const Target& target) const = 0;
|
||||||
|
virtual ~ProjPredEngine() = default;
|
||||||
|
};
|
||||||
|
} // namespace omath::projectile_prediction
|
||||||
26
include/omath/projectile_prediction/ProjPredEngineAVX2.hpp
Normal file
26
include/omath/projectile_prediction/ProjPredEngineAVX2.hpp
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
//
|
||||||
|
// Created by Vlad on 2/23/2025.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#include "ProjPredEngine.hpp"
|
||||||
|
|
||||||
|
namespace omath::projectile_prediction
|
||||||
|
{
|
||||||
|
class ProjPredEngineAVX2 final : public ProjPredEngine
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
[[nodiscard]] std::optional<Vector3> MaybeCalculateAimPoint(const Projectile& projectile,
|
||||||
|
const Target& target) const override;
|
||||||
|
|
||||||
|
|
||||||
|
ProjPredEngineAVX2(float gravityConstant, float simulationTimeStep, float maximumSimulationTime);
|
||||||
|
~ProjPredEngineAVX2() override = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
[[nodiscard]] static std::optional<float> CalculatePitch(const Vector3& projOrigin, const Vector3& targetPos,
|
||||||
|
float bulletGravity, float v0, float time);
|
||||||
|
const float m_gravityConstant;
|
||||||
|
const float m_simulationTimeStep;
|
||||||
|
const float m_maximumSimulationTime;
|
||||||
|
};
|
||||||
|
} // namespace omath::projectile_prediction
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "omath/Vector3.hpp"
|
#include "omath/Vector3.hpp"
|
||||||
|
|
||||||
namespace omath::prediction
|
namespace omath::projectile_prediction
|
||||||
{
|
{
|
||||||
class Projectile final
|
class Projectile final
|
||||||
{
|
{
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "omath/Vector3.hpp"
|
#include "omath/Vector3.hpp"
|
||||||
|
|
||||||
namespace omath::prediction
|
namespace omath::projectile_prediction
|
||||||
{
|
{
|
||||||
class Target final
|
class Target final
|
||||||
{
|
{
|
||||||
@@ -6,7 +6,7 @@ target_sources(omath PRIVATE
|
|||||||
Vector2.cpp
|
Vector2.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
add_subdirectory(prediction)
|
add_subdirectory(projectile_prediction)
|
||||||
add_subdirectory(pathfinding)
|
add_subdirectory(pathfinding)
|
||||||
add_subdirectory(projection)
|
add_subdirectory(projection)
|
||||||
add_subdirectory(collision)
|
add_subdirectory(collision)
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
target_sources(omath PRIVATE Engine.cpp Projectile.cpp Target.cpp)
|
target_sources(omath PRIVATE Engine.cpp Projectile.cpp Target.cpp ProjPredEngineAVX2.cpp ProjPredEngine.cpp)
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
#include "omath/prediction/Engine.hpp"
|
#include "omath/projectile_prediction/Engine.hpp"
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <omath/Angles.hpp>
|
#include <omath/Angles.hpp>
|
||||||
|
|
||||||
namespace omath::prediction
|
namespace omath::projectile_prediction
|
||||||
{
|
{
|
||||||
|
|
||||||
Engine::Engine(const float gravityConstant, const float simulationTimeStep, const float maximumSimulationTime,
|
Engine::Engine(const float gravityConstant, const float simulationTimeStep, const float maximumSimulationTime,
|
||||||
10
source/projectile_prediction/ProjPredEngine.cpp
Normal file
10
source/projectile_prediction/ProjPredEngine.cpp
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
//
|
||||||
|
// Created by Vlad on 2/23/2025.
|
||||||
|
//
|
||||||
|
#include "omath/projectile_prediction/ProjPredEngine.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
namespace omath::projectile_prediction
|
||||||
|
{
|
||||||
|
|
||||||
|
} // namespace omath::projectile_prediction
|
||||||
138
source/projectile_prediction/ProjPredEngineAVX2.cpp
Normal file
138
source/projectile_prediction/ProjPredEngineAVX2.cpp
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
//
|
||||||
|
// Created by Vlad on 2/23/2025.
|
||||||
|
//
|
||||||
|
#include "omath/projectile_prediction/ProjPredEngineAVX2.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
namespace omath::projectile_prediction
|
||||||
|
{
|
||||||
|
std::optional<Vector3> ProjPredEngineAVX2::MaybeCalculateAimPoint(const Projectile& projectile,
|
||||||
|
const Target& target) const
|
||||||
|
{
|
||||||
|
const float bulletGravity = m_gravityConstant * projectile.m_gravityScale;
|
||||||
|
const float v0 = projectile.m_launchSpeed;
|
||||||
|
const float v0Sqr = v0 * v0;
|
||||||
|
const Vector3 projOrigin = projectile.m_origin;
|
||||||
|
|
||||||
|
constexpr int SIMD_FACTOR = 8;
|
||||||
|
float currentTime = m_simulationTimeStep;
|
||||||
|
|
||||||
|
for (; currentTime <= m_maximumSimulationTime; currentTime += m_simulationTimeStep * SIMD_FACTOR)
|
||||||
|
{
|
||||||
|
const __m256 times =
|
||||||
|
_mm256_setr_ps(currentTime, currentTime + m_simulationTimeStep,
|
||||||
|
currentTime + m_simulationTimeStep * 2, currentTime + m_simulationTimeStep * 3,
|
||||||
|
currentTime + m_simulationTimeStep * 4, currentTime + m_simulationTimeStep * 5,
|
||||||
|
currentTime + m_simulationTimeStep * 6, currentTime + m_simulationTimeStep * 7);
|
||||||
|
|
||||||
|
const __m256 targetX =
|
||||||
|
_mm256_fmadd_ps(_mm256_set1_ps(target.m_velocity.x), times, _mm256_set1_ps(target.m_origin.x));
|
||||||
|
const __m256 targetY =
|
||||||
|
_mm256_fmadd_ps(_mm256_set1_ps(target.m_velocity.y), times, _mm256_set1_ps(target.m_origin.y));
|
||||||
|
const __m256 timesSq = _mm256_mul_ps(times, times);
|
||||||
|
const __m256 targetZ = _mm256_fmadd_ps(_mm256_set1_ps(target.m_velocity.z), times,
|
||||||
|
_mm256_fnmadd_ps(_mm256_set1_ps(0.5f * m_gravityConstant), timesSq,
|
||||||
|
_mm256_set1_ps(target.m_origin.z)));
|
||||||
|
|
||||||
|
const __m256 deltaX = _mm256_sub_ps(targetX, _mm256_set1_ps(projOrigin.x));
|
||||||
|
const __m256 deltaY = _mm256_sub_ps(targetY, _mm256_set1_ps(projOrigin.y));
|
||||||
|
const __m256 deltaZ = _mm256_sub_ps(targetZ, _mm256_set1_ps(projOrigin.z));
|
||||||
|
|
||||||
|
const __m256 dSqr = _mm256_add_ps(_mm256_mul_ps(deltaX, deltaX), _mm256_mul_ps(deltaY, deltaY));
|
||||||
|
|
||||||
|
const __m256 bgTimesSq = _mm256_mul_ps(_mm256_set1_ps(bulletGravity), timesSq);
|
||||||
|
const __m256 term = _mm256_add_ps(deltaZ, _mm256_mul_ps(_mm256_set1_ps(0.5f), bgTimesSq));
|
||||||
|
const __m256 termSq = _mm256_mul_ps(term, term);
|
||||||
|
const __m256 numerator = _mm256_add_ps(dSqr, termSq);
|
||||||
|
const __m256 denominator = _mm256_add_ps(timesSq, _mm256_set1_ps(1e-8f)); // Avoid division by zero
|
||||||
|
const __m256 requiredV0Sqr = _mm256_div_ps(numerator, denominator);
|
||||||
|
|
||||||
|
const __m256 v0SqrVec = _mm256_set1_ps(v0Sqr + 1e-3f);
|
||||||
|
const __m256 mask = _mm256_cmp_ps(requiredV0Sqr, v0SqrVec, _CMP_LE_OQ);
|
||||||
|
|
||||||
|
const unsigned validMask = _mm256_movemask_ps(mask);
|
||||||
|
|
||||||
|
if (!validMask)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
alignas(32) float validTimes[SIMD_FACTOR];
|
||||||
|
_mm256_store_ps(validTimes, times);
|
||||||
|
|
||||||
|
for (int i = 0; i < SIMD_FACTOR; ++i)
|
||||||
|
{
|
||||||
|
if (!(validMask & (1 << i)))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
const float candidateTime = validTimes[i];
|
||||||
|
|
||||||
|
if (candidateTime > m_maximumSimulationTime)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Fine search around candidate time
|
||||||
|
for (float fineTime = candidateTime - m_simulationTimeStep * 2;
|
||||||
|
fineTime <= candidateTime + m_simulationTimeStep * 2; fineTime += m_simulationTimeStep)
|
||||||
|
{
|
||||||
|
if (fineTime < 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
const Vector3 targetPos = target.PredictPosition(fineTime, m_gravityConstant);
|
||||||
|
const auto pitch = CalculatePitch(projOrigin, targetPos, bulletGravity, v0, fineTime);
|
||||||
|
if (!pitch)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
const Vector3 delta = targetPos - projOrigin;
|
||||||
|
const float d = std::sqrt(delta.x * delta.x + delta.y * delta.y);
|
||||||
|
const float height = d * std::tan(angles::DegreesToRadians(*pitch));
|
||||||
|
return Vector3(targetPos.x, targetPos.y, projOrigin.z + height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback scalar processing for remaining times
|
||||||
|
for (; currentTime <= m_maximumSimulationTime; currentTime += m_simulationTimeStep)
|
||||||
|
{
|
||||||
|
const Vector3 targetPos = target.PredictPosition(currentTime, m_gravityConstant);
|
||||||
|
const auto pitch = CalculatePitch(projOrigin, targetPos, bulletGravity, v0, currentTime);
|
||||||
|
if (!pitch)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
const Vector3 delta = targetPos - projOrigin;
|
||||||
|
const float d = std::sqrt(delta.x * delta.x + delta.y * delta.y);
|
||||||
|
const float height = d * std::tan(angles::DegreesToRadians(*pitch));
|
||||||
|
return Vector3(targetPos.x, targetPos.y, projOrigin.z + height);
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
ProjPredEngineAVX2::ProjPredEngineAVX2(const float gravityConstant, const float simulationTimeStep,
|
||||||
|
const float maximumSimulationTime) :
|
||||||
|
m_gravityConstant(gravityConstant), m_simulationTimeStep(maximumSimulationTime),
|
||||||
|
m_maximumSimulationTime(simulationTimeStep)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
std::optional<float> ProjPredEngineAVX2::CalculatePitch(const Vector3& projOrigin, const Vector3& targetPos,
|
||||||
|
const float bulletGravity, const float v0, const float time)
|
||||||
|
{
|
||||||
|
if (time <= 0.0f)
|
||||||
|
return std::nullopt;
|
||||||
|
|
||||||
|
const Vector3 delta = targetPos - projOrigin;
|
||||||
|
const float dSqr = delta.x * delta.x + delta.y * delta.y;
|
||||||
|
const float h = delta.z;
|
||||||
|
|
||||||
|
const float term = h + 0.5f * bulletGravity * time * time;
|
||||||
|
const float requiredV0Sqr = (dSqr + term * term) / (time * time);
|
||||||
|
const float v0Sqr = v0 * v0;
|
||||||
|
|
||||||
|
if (requiredV0Sqr > v0Sqr + 1e-3f)
|
||||||
|
return std::nullopt;
|
||||||
|
|
||||||
|
if (dSqr == 0.0f)
|
||||||
|
return term >= 0.0f ? 90.0f : -90.0f;
|
||||||
|
|
||||||
|
|
||||||
|
const float d = std::sqrt(dSqr);
|
||||||
|
const float tanTheta = term / d;
|
||||||
|
return angles::RadiansToDegrees(std::atan(tanTheta));
|
||||||
|
}
|
||||||
|
} // namespace omath::projectile_prediction
|
||||||
@@ -2,11 +2,11 @@
|
|||||||
// Created by Vlad on 6/9/2024.
|
// Created by Vlad on 6/9/2024.
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "omath/prediction/Projectile.hpp"
|
#include "omath/projectile_prediction/Projectile.hpp"
|
||||||
#include <cmath>
|
|
||||||
#include <omath/engines/Source/Formulas.hpp>
|
#include <omath/engines/Source/Formulas.hpp>
|
||||||
|
|
||||||
namespace omath::prediction
|
namespace omath::projectile_prediction
|
||||||
{
|
{
|
||||||
Vector3 Projectile::PredictPosition(const float pitch, const float yaw, const float time, const float gravity) const
|
Vector3 Projectile::PredictPosition(const float pitch, const float yaw, const float time, const float gravity) const
|
||||||
{
|
{
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
// Created by Vlad on 6/9/2024.
|
// Created by Vlad on 6/9/2024.
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "omath/prediction/Target.hpp"
|
#include "omath/projectile_prediction/Projectile.hpp"
|
||||||
|
|
||||||
|
|
||||||
namespace omath::prediction
|
namespace omath::prediction
|
||||||
@@ -1,12 +1,14 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <omath/prediction/Engine.hpp>
|
#include <omath/projectile_prediction/Engine.hpp>
|
||||||
|
|
||||||
TEST(UnitTestPrediction, PredictionTest)
|
TEST(UnitTestPrediction, PredictionTest)
|
||||||
{
|
{
|
||||||
constexpr omath::prediction::Target target{
|
constexpr omath::projectile_prediction::Target target{
|
||||||
.m_origin = {100, 0, 90}, .m_velocity = {0, 0, 0}, .m_isAirborne = false};
|
.m_origin = {100, 0, 90}, .m_velocity = {0, 0, 0}, .m_isAirborne = false};
|
||||||
constexpr omath::prediction::Projectile proj = {.m_origin = {3,2,1}, .m_launchSpeed = 5000, .m_gravityScale= 0.4};
|
constexpr omath::projectile_prediction::Projectile proj = {
|
||||||
const auto viewPoint = omath::prediction::Engine(400, 1.f / 1000.f, 50, 5.f).MaybeCalculateAimPoint(proj, target);
|
.m_origin = {3, 2, 1}, .m_launchSpeed = 5000, .m_gravityScale = 0.4};
|
||||||
|
const auto viewPoint =
|
||||||
|
omath::projectile_prediction::Engine(400, 1.f / 1000.f, 50, 5.f).MaybeCalculateAimPoint(proj, target);
|
||||||
|
|
||||||
const auto [pitch, yaw, _] = proj.m_origin.ViewAngleTo(viewPoint.value()).AsTuple();
|
const auto [pitch, yaw, _] = proj.m_origin.ViewAngleTo(viewPoint.value()).AsTuple();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user