Add comprehensive documentation for all game engines

Co-authored-by: orange-cpp <59374393+orange-cpp@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-11-01 09:37:11 +00:00
parent f4df88bb7a
commit e351b64355
20 changed files with 2605 additions and 0 deletions

View File

@@ -0,0 +1,110 @@
# `omath::opengl_engine::CameraTrait` — plug-in trait for `projection::Camera`
> Header: `omath/engines/opengl_engine/traits/camera_trait.hpp` • Impl: `omath/engines/opengl_engine/traits/camera_trait.cpp`
> Namespace: `omath::opengl_engine`
> Purpose: provide OpenGL-style **look-at**, **view**, and **projection** math to the generic `omath::projection::Camera` (satisfies `CameraEngineConcept`).
---
## Summary
`CameraTrait` exposes three `static` functions:
* `calc_look_at_angle(origin, look_at)` computes Euler angles so the camera at `origin` looks at `look_at`. Implementation normalizes the direction, computes **pitch** as `asin(dir.y)` and **yaw** as `-atan2(dir.x, -dir.z)`; **roll** is `0`. Pitch/yaw are returned using the project's strong angle types (`PitchAngle`, `YawAngle`, `RollAngle`).
* `calc_view_matrix(angles, origin)` delegates to OpenGL formulas `opengl_engine::calc_view_matrix`, producing a `Mat4X4` view matrix (column-major) for the given angles and origin.
* `calc_projection_matrix(fov, viewport, near, far)` builds a perspective projection by calling `calc_perspective_projection_matrix(fov_degrees, aspect, near, far)`, where `aspect = viewport.aspect_ratio()`. Accepts `FieldOfView` (degrees).
The trait's types (`ViewAngles`, `Mat4X4`, angle aliases) and helpers live in the OpenGL math headers included by the trait (`formulas.hpp`) and the shared projection header (`projection/camera.hpp`).
---
## API
```cpp
namespace omath::opengl_engine {
class CameraTrait final {
public:
// Compute Euler angles (pitch/yaw/roll) to look from cam_origin to look_at.
static ViewAngles
calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept;
// Build view matrix for given angles and origin (column-major).
static Mat4X4
calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept;
// Build perspective projection from FOV (deg), viewport, near/far (column-major).
static Mat4X4
calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port,
float near, float far) noexcept;
};
} // namespace omath::opengl_engine
```
Uses: `Vector3<float>`, `ViewAngles` (pitch/yaw/roll), `Mat4X4`, `projection::FieldOfView`, `projection::ViewPort`.
---
## Behavior & conventions
* **Angles from look-at** (Y-up, -Z forward coordinate system):
```
dir = normalize(look_at - origin)
pitch = asin(dir.y) // +Y is up
yaw = -atan2(dir.x, -dir.z) // horizontal rotation
roll = 0
```
Returned as `PitchAngle::from_radians(...)`, `YawAngle::from_radians(...)`, etc.
* **View matrix**: built by the OpenGL helper `opengl_engine::calc_view_matrix(angles, origin)` to match OpenGL's right-handed, Y-up, -Z forward conventions. Matrix is **column-major**.
* **Projection**: uses `calc_perspective_projection_matrix(fov.as_degrees(), viewport.aspect_ratio(), near, far)`. Pass your **vertical FOV** in degrees via `FieldOfView`; the helper computes a standard perspective matrix (column-major).
---
## Using with `projection::Camera`
Create a camera whose math is driven by this trait:
```cpp
using Mat4 = Mat4X4; // from OpenGL math headers (column-major)
using Angs = ViewAngles; // pitch/yaw/roll type
using GLcam = omath::projection::Camera<Mat4, Angs, omath::opengl_engine::CameraTrait>;
omath::projection::ViewPort vp{1920.f, 1080.f};
auto fov = omath::projection::FieldOfView::from_degrees(45.f);
GLcam cam(
/*position*/ {5.f, 3.f, 5.f},
/*angles*/ omath::opengl_engine::CameraTrait::calc_look_at_angle({5,3,5},{0,0,0}),
/*viewport*/ vp,
/*fov*/ fov,
/*near*/ 0.1f,
/*far*/ 100.f
);
```
This satisfies `CameraEngineConcept` expected by `projection::Camera` (look-at, view, projection) as declared in the trait header.
---
## Notes & tips
* Ensure your `ViewAngles` aliases (`PitchAngle`, `YawAngle`, `RollAngle`) match the project's angle policy (ranges/normalization). The implementation constructs them **from radians**.
* `aspect_ratio()` is taken directly from `ViewPort` (`width / height`), so keep both positive and non-zero.
* `near` must be > 0 and `< far` for a valid projection matrix (enforced by your math helpers).
* OpenGL uses **Y-up, -Z forward**: pitch angles control vertical look (positive = up), yaw controls horizontal rotation.
* Matrices are **column-major** (no transpose needed for OpenGL shaders).
---
## See also
* OpenGL math helpers in `omath/engines/opengl_engine/formulas.hpp` (view/projection builders used above).
* Generic camera wrapper `omath::projection::Camera` and its `CameraEngineConcept` (this trait is designed to plug straight into it).

View File

@@ -0,0 +1,78 @@
# `omath::opengl_engine` — types & constants
> Header: `omath/engines/opengl_engine/constants.hpp`
> Namespace: `omath::opengl_engine`
> Purpose: define OpenGL coordinate system, matrix types, and angle ranges
---
## Summary
The **OpenGL Engine** uses a **Y-up, right-handed** coordinate system:
* **Up** = `{0, 1, 0}` (Y-axis)
* **Right** = `{1, 0, 0}` (X-axis)
* **Forward** = `{0, 0, -1}` (negative Z-axis)
Matrices are **column-major**. Angles are **clamped pitch** (±90°) and **normalized yaw/roll** (±180°).
---
## Constants
```cpp
namespace omath::opengl_engine {
constexpr Vector3<float> k_abs_up = {0, 1, 0};
constexpr Vector3<float> k_abs_right = {1, 0, 0};
constexpr Vector3<float> k_abs_forward = {0, 0, -1};
}
```
These basis vectors define the engine's **world coordinate frame**.
---
## Matrix types
```cpp
using Mat4X4 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>;
using Mat3X3 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>;
using Mat1X3 = Mat<1, 3, float, MatStoreType::COLUMN_MAJOR>;
```
**Column-major** storage means columns are contiguous in memory. This matches OpenGL's native matrix layout and shader expectations (GLSL).
---
## Angle types
```cpp
using PitchAngle = Angle<float, -90.f, 90.f, AngleFlags::Clamped>;
using YawAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
using RollAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
using ViewAngles = omath::ViewAngles<PitchAngle, YawAngle, RollAngle>;
```
* **PitchAngle**: clamped to **[-90°, +90°]** (looking down vs. up)
* **YawAngle**: normalized to **[-180°, +180°]** (horizontal rotation)
* **RollAngle**: normalized to **[-180°, +180°]** (camera roll)
`ViewAngles` bundles all three into a single type for camera/view transforms.
---
## Coordinate system notes
* **Y-up**: gravity points along `-Y`, height increases with `+Y`
* **Right-handed**: cross product `right × up = forward` (forward is `-Z`)
* **Forward = -Z**: the camera looks down the negative Z-axis (OpenGL convention)
* This matches **OpenGL** conventions for 3D graphics pipelines
---
## See also
* `omath/engines/opengl_engine/formulas.hpp` — view/projection matrix builders
* `omath/trigonometry/angle.hpp` — angle normalization & clamping helpers
* `omath/trigonometry/view_angles.hpp` — generic pitch/yaw/roll wrapper

View File

@@ -0,0 +1,140 @@
# `omath::opengl_engine` — formulas & matrix helpers
> Header: `omath/engines/opengl_engine/formulas.hpp`
> Namespace: `omath::opengl_engine`
> Purpose: compute direction vectors, rotation matrices, view matrices, and perspective projections for OpenGL
---
## Summary
This header provides **OpenGL**-specific math for:
* **Direction vectors** (`forward`, `right`, `up`) from `ViewAngles`
* **Rotation matrices** from Euler angles
* **View matrices** (camera transforms)
* **Perspective projection** matrices
All functions respect OpenGL's **Y-up, right-handed** coordinate system with **forward = -Z**.
---
## API
```cpp
namespace omath::opengl_engine {
// Compute forward direction from pitch/yaw/roll
[[nodiscard]]
Vector3<float> forward_vector(const ViewAngles& angles) noexcept;
// Compute right direction from pitch/yaw/roll
[[nodiscard]]
Vector3<float> right_vector(const ViewAngles& angles) noexcept;
// Compute up direction from pitch/yaw/roll
[[nodiscard]]
Vector3<float> up_vector(const ViewAngles& angles) noexcept;
// Build 3x3 rotation matrix from angles
[[nodiscard]]
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
// Build view matrix (camera space transform)
[[nodiscard]]
Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept;
// Build perspective projection matrix
[[nodiscard]]
Mat4X4 calc_perspective_projection_matrix(float field_of_view,
float aspect_ratio,
float near, float far) noexcept;
} // namespace omath::opengl_engine
```
---
## Direction vectors
Given camera angles (pitch/yaw/roll):
* `forward_vector(angles)` → unit vector pointing where the camera looks (typically `-Z` direction)
* `right_vector(angles)` → unit vector pointing to the camera's right (`+X` direction)
* `up_vector(angles)` → unit vector pointing upward relative to the camera (`+Y` direction)
These are used for movement, aim direction, and building coordinate frames.
---
## Rotation & view matrices
* `rotation_matrix(angles)` → 3×3 (or 4×4) rotation matrix from Euler angles (column-major)
* `calc_view_matrix(angles, origin)` → camera view matrix (column-major)
The view matrix transforms world coordinates into camera space (origin at camera, axes aligned with camera orientation).
**Note**: Matrices are **column-major** to match OpenGL/GLSL conventions. No transpose needed when uploading to shaders.
---
## Perspective projection
```cpp
Mat4X4 proj = calc_perspective_projection_matrix(
fov_degrees, // vertical field of view (e.g., 45)
aspect_ratio, // width / height (e.g., 16/9)
near_plane, // e.g., 0.1
far_plane // e.g., 100.0
);
```
Produces a **perspective projection matrix** suitable for OpenGL rendering. Combined with the view matrix, this implements the standard camera transform chain.
---
## Usage example
```cpp
using namespace omath::opengl_engine;
// Camera setup
ViewAngles angles = {
PitchAngle::from_degrees(-20.0f),
YawAngle::from_degrees(135.0f),
RollAngle::from_degrees(0.0f)
};
Vector3<float> cam_pos{5.0f, 3.0f, 5.0f};
// Compute direction
auto forward = forward_vector(angles);
auto right = right_vector(angles);
auto up = up_vector(angles);
// Build matrices (column-major for OpenGL)
auto view_mat = calc_view_matrix(angles, cam_pos);
auto proj_mat = calc_perspective_projection_matrix(45.0f, 16.0f/9.0f, 0.1f, 100.0f);
// Upload to OpenGL shaders (no transpose needed)
glUniformMatrix4fv(view_loc, 1, GL_FALSE, view_mat.data());
glUniformMatrix4fv(proj_loc, 1, GL_FALSE, proj_mat.data());
```
---
## Conventions
* **Angles**: pitch (up/down), yaw (left/right), roll (tilt)
* **Pitch**: positive = looking up, negative = looking down
* **Yaw**: increases counter-clockwise from the -Z axis
* **Coordinate system**: Y-up, -Z-forward, X-right (right-handed)
* **Matrix storage**: column-major (matches OpenGL/GLSL)
---
## See also
* `omath/engines/opengl_engine/constants.hpp` — coordinate frame & angle types
* `omath/engines/opengl_engine/traits/camera_trait.hpp` — plug-in for generic `Camera`
* `omath/projection/camera.hpp` — generic camera wrapper using these formulas

View File

@@ -0,0 +1,199 @@
# `omath::opengl_engine::PredEngineTrait` — projectile prediction trait
> Header: `omath/engines/opengl_engine/traits/pred_engine_trait.hpp`
> Namespace: `omath::opengl_engine`
> Purpose: provide OpenGL-specific projectile and target prediction for ballistic calculations
---
## Summary
`PredEngineTrait` implements engine-specific helpers for **projectile prediction**:
* `predict_projectile_position` computes where a projectile will be after `time` seconds
* `predict_target_position` computes where a moving target will be after `time` seconds
* `calc_vector_2d_distance` horizontal distance (X/Z plane, ignoring Y)
* `get_vector_height_coordinate` extracts vertical coordinate (Y in OpenGL)
* `calc_viewpoint_from_angles` computes aim point given pitch angle
* `calc_direct_pitch_angle` pitch angle to look from origin to target
* `calc_direct_yaw_angle` yaw angle to look from origin to target
These methods satisfy the `PredEngineTraitConcept` required by generic projectile prediction algorithms.
---
## API
```cpp
namespace omath::opengl_engine {
class PredEngineTrait final {
public:
// Predict projectile position after `time` seconds
static constexpr Vector3<float>
predict_projectile_position(const projectile_prediction::Projectile& projectile,
float pitch, float yaw, float time,
float gravity) noexcept;
// Predict target position after `time` seconds
static constexpr Vector3<float>
predict_target_position(const projectile_prediction::Target& target,
float time, float gravity) noexcept;
// Compute horizontal (2D) distance
static float
calc_vector_2d_distance(const Vector3<float>& delta) noexcept;
// Get vertical coordinate (Y in OpenGL)
static constexpr float
get_vector_height_coordinate(const Vector3<float>& vec) noexcept;
// Compute aim point from angles
static Vector3<float>
calc_viewpoint_from_angles(const projectile_prediction::Projectile& projectile,
Vector3<float> predicted_target_position,
std::optional<float> projectile_pitch) noexcept;
// Compute pitch angle to look at target
static float
calc_direct_pitch_angle(const Vector3<float>& origin,
const Vector3<float>& view_to) noexcept;
// Compute yaw angle to look at target
static float
calc_direct_yaw_angle(const Vector3<float>& origin,
const Vector3<float>& view_to) noexcept;
};
} // namespace omath::opengl_engine
```
---
## Projectile prediction
```cpp
auto pos = PredEngineTrait::predict_projectile_position(
projectile, // initial position, speed, gravity scale
pitch_deg, // launch pitch (positive = up)
yaw_deg, // launch yaw
time, // time in seconds
gravity // gravity constant (e.g., 9.81 m/s²)
);
```
Computes:
1. Forward vector from pitch/yaw (using `forward_vector`)
2. Initial velocity: `forward * launch_speed`
3. Position after `time`: `origin + velocity*time - 0.5*gravity*gravityScale*time²` (Y component only)
**Note**: Negative pitch in `forward_vector` convention → positive pitch looks up.
---
## Target prediction
```cpp
auto pos = PredEngineTrait::predict_target_position(
target, // position, velocity, airborne flag
time, // time in seconds
gravity // gravity constant
);
```
Simple linear extrapolation plus gravity if target is airborne:
```
predicted = origin + velocity * time
if (airborne)
predicted.y -= 0.5 * gravity * time²
```
---
## Distance & height helpers
* `calc_vector_2d_distance(delta)``sqrt(delta.x² + delta.z²)` (horizontal distance)
* `get_vector_height_coordinate(vec)``vec.y` (vertical coordinate in OpenGL)
Used to compute ballistic arc parameters.
---
## Aim angle calculation
* `calc_direct_pitch_angle(origin, target)` → pitch in degrees to look from `origin` to `target`
- Formula: `asin(Δy / distance)` converted to degrees (direction normalized first)
- Positive = looking up, negative = looking down
* `calc_direct_yaw_angle(origin, target)` → yaw in degrees to look from `origin` to `target`
- Formula: `-atan2(Δx, -Δz)` converted to degrees (direction normalized first)
- Horizontal rotation around Y-axis (accounts for -Z forward convention)
---
## Viewpoint from angles
```cpp
auto aim_point = PredEngineTrait::calc_viewpoint_from_angles(
projectile,
predicted_target_pos,
optional_pitch_deg
);
```
Computes where to aim in 3D space given a desired pitch angle. Uses horizontal distance and `tan(pitch)` to compute height offset. Result has adjusted Y coordinate.
---
## Conventions
* **Coordinate system**: Y-up, -Z forward (height increases with Y)
* **Angles**: pitch in [-90°, +90°], yaw in [-180°, +180°]
* **Gravity**: applied downward along -Y axis
* **Pitch convention**: +90° = straight up, -90° = straight down
* **Forward direction**: negative Z-axis
---
## Usage example
```cpp
using namespace omath::opengl_engine;
using namespace omath::projectile_prediction;
Projectile proj{
.m_origin = {0, 2, 0},
.m_launch_speed = 30.0f,
.m_gravity_scale = 1.0f
};
Target tgt{
.m_origin = {10, 2, -15},
.m_velocity = {0.5f, 0, -1.0f},
.m_is_airborne = false
};
float gravity = 9.81f;
float time = 0.5f;
// Predict where target will be
auto target_pos = PredEngineTrait::predict_target_position(tgt, time, gravity);
// Compute aim angles
float pitch = PredEngineTrait::calc_direct_pitch_angle(proj.m_origin, target_pos);
float yaw = PredEngineTrait::calc_direct_yaw_angle(proj.m_origin, target_pos);
// Predict projectile position with those angles
auto proj_pos = PredEngineTrait::predict_projectile_position(proj, pitch, yaw, time, gravity);
```
---
## See also
* `omath/engines/opengl_engine/formulas.hpp` — direction vectors and matrix builders
* `omath/projectile_prediction/projectile.hpp``Projectile` struct
* `omath/projectile_prediction/target.hpp``Target` struct
* Generic projectile prediction algorithms that use `PredEngineTraitConcept`