Skip to content

Commit 17b1504

Browse files
committed
Refactors camera and prediction engine traits.
Moves camera and prediction engine implementations into traits for each engine, decoupling the engine-specific logic from the core classes, promoting code reuse and maintainability. This change allows for easier addition of new engines and customization of existing ones.
1 parent c7228c9 commit 17b1504

File tree

22 files changed

+267
-228
lines changed

22 files changed

+267
-228
lines changed

include/omath/engines/iw_engine/camera.hpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,9 @@
55
#pragma once
66
#include "omath/engines/iw_engine/constants.hpp"
77
#include "omath/projection/camera.hpp"
8+
#include "traits/camera_trait.hpp"
89

910
namespace omath::iw_engine
1011
{
11-
class Camera final : public projection::Camera<Mat4X4, ViewAngles>
12-
{
13-
public:
14-
Camera(const Vector3<float>& position, const ViewAngles& view_angles, const projection::ViewPort& view_port,
15-
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
16-
void look_at(const Vector3<float>& target) override;
17-
18-
protected:
19-
[[nodiscard]] Mat4X4 calc_view_matrix() const noexcept override;
20-
[[nodiscard]] Mat4X4 calc_projection_matrix() const noexcept override;
21-
};
12+
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>;
2213
} // namespace omath::iw_engine
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// Created by Vlad on 8/10/2025.
3+
//
4+
5+
#pragma once
6+
#include "omath/engines/iw_engine/constants.hpp"
7+
#include "omath/projection/camera.hpp"
8+
9+
namespace omath::iw_engine
10+
{
11+
class CameraTrait final
12+
{
13+
public:
14+
[[nodiscard]]
15+
static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept;
16+
17+
[[nodiscard]]
18+
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
19+
[[nodiscard]]
20+
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
21+
float near, float far) noexcept;
22+
};
23+
24+
} // namespace omath::iw_engine

include/omath/projectile_prediction/engine_traits/iw_engine_trait.hpp renamed to include/omath/engines/iw_engine/traits/pred_engine_trait.hpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,26 @@
88
#include "omath/projectile_prediction/target.hpp"
99
#include <optional>
1010

11-
namespace omath::projectile_prediction::traits
11+
namespace omath::iw_engine
1212
{
13-
class IwEngineTrait final
13+
class PredEngineTrait final
1414
{
1515
public:
16-
constexpr static Vector3<float> predict_projectile_position(const Projectile& projectile, const float pitch,
17-
const float yaw, const float time,
18-
const float gravity) noexcept
16+
constexpr static Vector3<float> predict_projectile_position(const projectile_prediction::Projectile& projectile,
17+
const float pitch, const float yaw,
18+
const float time, const float gravity) noexcept
1919
{
2020
auto current_pos = projectile.m_origin
21-
+ iw_engine::forward_vector({iw_engine::PitchAngle::from_degrees(-pitch),
22-
iw_engine::YawAngle::from_degrees(yaw),
23-
iw_engine::RollAngle::from_degrees(0)})
21+
+ forward_vector({PitchAngle::from_degrees(-pitch), YawAngle::from_degrees(yaw),
22+
RollAngle::from_degrees(0)})
2423
* projectile.m_launch_speed * time;
2524
current_pos.z -= (gravity * projectile.m_gravity_scale) * (time * time) * 0.5f;
2625

2726
return current_pos;
2827
}
2928
[[nodiscard]]
30-
static constexpr Vector3<float> predict_target_position(const Target& target, const float time,
31-
const float gravity) noexcept
29+
static constexpr Vector3<float> predict_target_position(const projectile_prediction::Target& target,
30+
const float time, const float gravity) noexcept
3231
{
3332
auto predicted = target.m_origin + target.m_velocity * time;
3433

@@ -50,7 +49,7 @@ namespace omath::projectile_prediction::traits
5049
}
5150

5251
[[nodiscard]]
53-
static Vector3<float> calc_viewpoint_from_angles(const Projectile& projectile,
52+
static Vector3<float> calc_viewpoint_from_angles(const projectile_prediction::Projectile& projectile,
5453
Vector3<float> predicted_target_position,
5554
const std::optional<float> projectile_pitch) noexcept
5655
{
@@ -77,4 +76,4 @@ namespace omath::projectile_prediction::traits
7776
return angles::radians_to_degrees(std::atan2(delta.y, delta.x));
7877
};
7978
};
80-
} // namespace omath::projectile_prediction::traits
79+
} // namespace omath::iw_engine

include/omath/engines/opengl_engine/camera.hpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,9 @@
44
#pragma once
55
#include "omath/engines/opengl_engine/constants.hpp"
66
#include "omath/projection/camera.hpp"
7+
#include "traits/camera_trait.hpp"
78

89
namespace omath::opengl_engine
910
{
10-
class Camera final : public projection::Camera<Mat4X4, ViewAngles>
11-
{
12-
public:
13-
Camera(const Vector3<float>& position, const ViewAngles& view_angles, const projection::ViewPort& view_port,
14-
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
15-
void look_at(const Vector3<float>& target) override;
16-
[[nodiscard]] Mat4X4 calc_view_matrix() const noexcept override;
17-
[[nodiscard]] Mat4X4 calc_projection_matrix() const noexcept override;
18-
};
11+
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>;
1912
} // namespace omath::opengl_engine
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// Created by Vlad on 8/10/2025.
3+
//
4+
5+
#pragma once
6+
#include "omath/engines/opengl_engine/constants.hpp"
7+
#include "omath/projection/camera.hpp"
8+
9+
namespace omath::opengl_engine
10+
{
11+
class CameraTrait final
12+
{
13+
public:
14+
[[nodiscard]]
15+
static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept;
16+
17+
[[nodiscard]]
18+
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
19+
[[nodiscard]]
20+
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
21+
float near, float far) noexcept;
22+
};
23+
24+
} // namespace omath::opengl_engine

include/omath/projectile_prediction/engine_traits/opengl_engine_trait.hpp renamed to include/omath/engines/opengl_engine/traits/pred_engine_trait.hpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,26 @@
77
#include "omath/projectile_prediction/target.hpp"
88
#include <optional>
99

10-
namespace omath::projectile_prediction::traits
10+
namespace omath::opengl_engine
1111
{
12-
class OpenGlEngineTrait final
12+
class PredEngineTrait final
1313
{
1414
public:
15-
constexpr static Vector3<float> predict_projectile_position(const Projectile& projectile, const float pitch,
16-
const float yaw, const float time,
17-
const float gravity) noexcept
15+
constexpr static Vector3<float> predict_projectile_position(const projectile_prediction::Projectile& projectile,
16+
const float pitch, const float yaw,
17+
const float time, const float gravity) noexcept
1818
{
1919
auto current_pos = projectile.m_origin
20-
+ opengl_engine::forward_vector({opengl_engine::PitchAngle::from_degrees(-pitch),
21-
opengl_engine::YawAngle::from_degrees(yaw),
22-
opengl_engine::RollAngle::from_degrees(0)})
20+
+ forward_vector({PitchAngle::from_degrees(-pitch), YawAngle::from_degrees(yaw),
21+
RollAngle::from_degrees(0)})
2322
* projectile.m_launch_speed * time;
2423
current_pos.y -= (gravity * projectile.m_gravity_scale) * (time * time) * 0.5f;
2524

2625
return current_pos;
2726
}
2827
[[nodiscard]]
29-
static constexpr Vector3<float> predict_target_position(const Target& target, const float time,
30-
const float gravity) noexcept
28+
static constexpr Vector3<float> predict_target_position(const projectile_prediction::Target& target,
29+
const float time, const float gravity) noexcept
3130
{
3231
auto predicted = target.m_origin + target.m_velocity * time;
3332

@@ -49,7 +48,7 @@ namespace omath::projectile_prediction::traits
4948
}
5049

5150
[[nodiscard]]
52-
static Vector3<float> calc_viewpoint_from_angles(const Projectile& projectile,
51+
static Vector3<float> calc_viewpoint_from_angles(const projectile_prediction::Projectile& projectile,
5352
Vector3<float> predicted_target_position,
5453
const std::optional<float> projectile_pitch) noexcept
5554
{
@@ -76,4 +75,4 @@ namespace omath::projectile_prediction::traits
7675
return angles::radians_to_degrees(std::atan2(delta.z, delta.x));
7776
};
7877
};
79-
} // namespace omath::projectile_prediction::traits
78+
} // namespace omath::opengl_engine

include/omath/engines/source_engine/camera.hpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,8 @@
44
#pragma once
55
#include "omath/engines/source_engine/constants.hpp"
66
#include "omath/projection/camera.hpp"
7-
7+
#include "traits/camera_trait.hpp"
88
namespace omath::source_engine
99
{
10-
class Camera final : public projection::Camera<Mat4X4, ViewAngles>
11-
{
12-
public:
13-
Camera(const Vector3<float>& position, const ViewAngles& view_angles, const projection::ViewPort& view_port,
14-
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
15-
void look_at(const Vector3<float>& target) override;
16-
17-
protected:
18-
[[nodiscard]] Mat4X4 calc_view_matrix() const noexcept override;
19-
[[nodiscard]] Mat4X4 calc_projection_matrix() const noexcept override;
20-
};
10+
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>;
2111
} // namespace omath::source_engine
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// Created by Vlad on 8/10/2025.
3+
//
4+
5+
#pragma once
6+
#include "omath/engines/source_engine/constants.hpp"
7+
#include "omath/projection/camera.hpp"
8+
9+
namespace omath::source_engine
10+
{
11+
class CameraTrait final
12+
{
13+
public:
14+
[[nodiscard]]
15+
static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept;
16+
17+
[[nodiscard]]
18+
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
19+
[[nodiscard]]
20+
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
21+
float near, float far) noexcept;
22+
};
23+
24+
} // namespace omath::source_engine

include/omath/projectile_prediction/engine_traits/source_engine_trait.hpp renamed to include/omath/engines/source_engine/traits/pred_engine_trait.hpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,26 @@
88
#include "omath/projectile_prediction/target.hpp"
99
#include <optional>
1010

11-
namespace omath::projectile_prediction::traits
11+
namespace omath::source_engine
1212
{
13-
class SourceEngineTrait final
13+
class PredEngineTrait final
1414
{
1515
public:
16-
constexpr static Vector3<float> predict_projectile_position(const Projectile& projectile, const float pitch,
17-
const float yaw, const float time,
18-
const float gravity) noexcept
16+
constexpr static Vector3<float> predict_projectile_position(const projectile_prediction::Projectile& projectile,
17+
const float pitch, const float yaw,
18+
const float time, const float gravity) noexcept
1919
{
2020
auto current_pos = projectile.m_origin
21-
+ source_engine::forward_vector({source_engine::PitchAngle::from_degrees(-pitch),
22-
source_engine::YawAngle::from_degrees(yaw),
23-
source_engine::RollAngle::from_degrees(0)})
21+
+ forward_vector({PitchAngle::from_degrees(-pitch), YawAngle::from_degrees(yaw),
22+
RollAngle::from_degrees(0)})
2423
* projectile.m_launch_speed * time;
2524
current_pos.z -= (gravity * projectile.m_gravity_scale) * (time * time) * 0.5f;
2625

2726
return current_pos;
2827
}
2928
[[nodiscard]]
30-
static constexpr Vector3<float> predict_target_position(const Target& target, const float time,
31-
const float gravity) noexcept
29+
static constexpr Vector3<float> predict_target_position(const projectile_prediction::Target& target,
30+
const float time, const float gravity) noexcept
3231
{
3332
auto predicted = target.m_origin + target.m_velocity * time;
3433

@@ -50,7 +49,7 @@ namespace omath::projectile_prediction::traits
5049
}
5150

5251
[[nodiscard]]
53-
static Vector3<float> calc_viewpoint_from_angles(const Projectile& projectile,
52+
static Vector3<float> calc_viewpoint_from_angles(const projectile_prediction::Projectile& projectile,
5453
Vector3<float> predicted_target_position,
5554
const std::optional<float> projectile_pitch) noexcept
5655
{
@@ -77,4 +76,4 @@ namespace omath::projectile_prediction::traits
7776
return angles::radians_to_degrees(std::atan2(delta.y, delta.x));
7877
};
7978
};
80-
} // namespace omath::projectile_prediction::traits
79+
} // namespace omath::source_engine

include/omath/engines/unity_engine/camera.hpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,9 @@
55
#pragma once
66
#include "omath/engines/unity_engine/constants.hpp"
77
#include "omath/projection/camera.hpp"
8+
#include "traits/camera_trait.hpp"
89

910
namespace omath::unity_engine
1011
{
11-
class Camera final : public projection::Camera<Mat4X4, ViewAngles>
12-
{
13-
public:
14-
Camera(const Vector3<float>& position, const ViewAngles& view_angles, const projection::ViewPort& view_port,
15-
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
16-
void look_at(const Vector3<float>& target) override;
17-
18-
protected:
19-
[[nodiscard]] Mat4X4 calc_view_matrix() const noexcept override;
20-
[[nodiscard]] Mat4X4 calc_projection_matrix() const noexcept override;
21-
};
12+
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>;
2213
} // namespace omath::unity_engine

0 commit comments

Comments
 (0)