changed code style

This commit is contained in:
2025-05-03 20:31:59 +03:00
parent be3fae63b8
commit df6d75e554
70 changed files with 1258 additions and 1312 deletions

View File

@@ -3,27 +3,25 @@
//
#include "omath/3d_primitives/box.hpp"
namespace omath::primitives
{
std::array<Triangle<Vector3<float>>, 12> CreateBox(const Vector3<float>& top, const Vector3<float>& bottom,
const Vector3<float>& dirForward,
const Vector3<float>& dirRight,
const float ratio)
std::array<Triangle<Vector3<float>>, 12> create_box(const Vector3<float>& top, const Vector3<float>& bottom,
const Vector3<float>& dir_forward,
const Vector3<float>& dir_right, const float ratio)
{
const auto height = top.DistTo(bottom);
const auto sideSize = height / ratio;
const auto height = top.distance_to(bottom);
const auto side_size = height / ratio;
// corner layout (03 bottom, 47 top)
std::array<Vector3<float>, 8> p;
p[0] = bottom + (dirForward + dirRight) * sideSize; // frontrightbottom
p[1] = bottom + (dirForward - dirRight) * sideSize; // frontleftbottom
p[2] = bottom + (-dirForward + dirRight) * sideSize; // backrightbottom
p[3] = bottom + (-dirForward - dirRight) * sideSize; // backleftbottom
p[4] = top + (dirForward + dirRight) * sideSize; // frontrighttop
p[5] = top + (dirForward - dirRight) * sideSize; // frontlefttop
p[6] = top + (-dirForward + dirRight) * sideSize; // backrighttop
p[7] = top + (-dirForward - dirRight) * sideSize; // backlefttop
p[0] = bottom + (dir_forward + dir_right) * side_size; // frontrightbottom
p[1] = bottom + (dir_forward - dir_right) * side_size; // frontleftbottom
p[2] = bottom + (-dir_forward + dir_right) * side_size; // backrightbottom
p[3] = bottom + (-dir_forward - dir_right) * side_size; // backleftbottom
p[4] = top + (dir_forward + dir_right) * side_size; // frontrighttop
p[5] = top + (dir_forward - dir_right) * side_size; // frontlefttop
p[6] = top + (-dir_forward + dir_right) * side_size; // backrighttop
p[7] = top + (-dir_forward - dir_right) * side_size; // backlefttop
std::array<Triangle<Vector3<float>>, 12> poly;
@@ -53,4 +51,4 @@ namespace omath::primitives
return poly;
}
}
} // namespace omath::primitives

View File

@@ -5,63 +5,58 @@
namespace omath::collision
{
bool LineTracer::CanTraceLine(const Ray& ray, const Triangle<Vector3<float>>& triangle)
bool LineTracer::can_trace_line(const Ray& ray, const Triangle<Vector3<float>>& triangle)
{
return GetRayHitPoint(ray, triangle) == ray.end;
return get_ray_hit_point(ray, triangle) == ray.end;
}
Vector3<float> Ray::DirectionVector() const
Vector3<float> Ray::direction_vector() const
{
return end - start;
}
Vector3<float> Ray::DirectionVectorNormalized() const
Vector3<float> Ray::direction_vector_normalized() const
{
return DirectionVector().Normalized();
return direction_vector().normalized();
}
Vector3<float> LineTracer::GetRayHitPoint(const Ray& ray, const Triangle<Vector3<float>>& triangle)
Vector3<float> LineTracer::get_ray_hit_point(const Ray& ray, const Triangle<Vector3<float>>& triangle)
{
constexpr float kEpsilon = std::numeric_limits<float>::epsilon();
constexpr float k_epsilon = std::numeric_limits<float>::epsilon();
const auto sideA = triangle.SideAVector();
const auto sideB = triangle.SideBVector();
const auto side_a = triangle.side_a_vector();
const auto side_b = triangle.side_b_vector();
const auto ray_dir = ray.direction_vector();
const auto rayDir = ray.DirectionVector();
const auto p = ray_dir.cross(side_b);
const auto det = side_a.dot(p);
const auto p = rayDir.Cross(sideB);
const auto det = sideA.Dot(p);
if (std::abs(det) < kEpsilon)
if (std::abs(det) < k_epsilon)
return ray.end;
const auto invDet = 1.0f / det;
const auto inv_det = 1.0f / det;
const auto t = ray.start - triangle.m_vertex2;
const auto u = t.Dot(p) * invDet;
const auto u = t.dot(p) * inv_det;
if ((u < 0 && std::abs(u) > kEpsilon) || (u > 1 && std::abs(u - 1) > kEpsilon))
if ((u < 0 && std::abs(u) > k_epsilon) || (u > 1 && std::abs(u - 1) > k_epsilon))
return ray.end;
const auto q = t.Cross(sideA);
const auto v = rayDir.Dot(q) * invDet;
const auto q = t.cross(side_a);
const auto v = ray_dir.dot(q) * inv_det;
if ((v < 0 && std::abs(v) > kEpsilon) || (u + v > 1 && std::abs(u + v - 1) > kEpsilon))
if ((v < 0 && std::abs(v) > k_epsilon) || (u + v > 1 && std::abs(u + v - 1) > k_epsilon))
return ray.end;
const auto tHit = sideB.Dot(q) * invDet;
const auto t_hit = side_b.dot(q) * inv_det;
if (ray.infinite_length)
{
if (tHit <= kEpsilon)
if (t_hit <= k_epsilon)
return ray.end;
}
else if (tHit <= kEpsilon || tHit > 1.0f - kEpsilon)
else if (t_hit <= k_epsilon || t_hit > 1.0f - k_epsilon)
return ray.end;
return ray.start + rayDir * tHit;
return ray.start + ray_dir * t_hit;
}
} // namespace omath::collision

View File

@@ -6,7 +6,6 @@
#include <algorithm>
#include <cmath>
namespace omath
{

View File

@@ -7,28 +7,27 @@
namespace omath::iw_engine
{
Camera::Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, const float near, const float far) :
projection::Camera<Mat4x4, ViewAngles>(position, viewAngles, viewPort, fov, near, far)
Camera::Camera(const Vector3<float>& position, const ViewAngles& view_angles, const projection::ViewPort& view_port,
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, const float near, const float far)
: projection::Camera<Mat4X4, ViewAngles>(position, view_angles, view_port, fov, near, far)
{
}
void Camera::LookAt([[maybe_unused]] const Vector3<float>& target)
void Camera::look_at([[maybe_unused]] const Vector3<float>& target)
{
const float distance = m_origin.DistTo(target);
const float distance = m_origin.distance_to(target);
const auto delta = target - m_origin;
m_viewAngles.pitch = PitchAngle::FromRadians(std::asin(delta.z / distance));
m_viewAngles.yaw = -YawAngle::FromRadians(std::atan2(delta.y, delta.x));
m_viewAngles.roll = RollAngle::FromRadians(0.f);
m_view_angles.pitch = PitchAngle::from_radians(std::asin(delta.z / distance));
m_view_angles.yaw = -YawAngle::from_radians(std::atan2(delta.y, delta.x));
m_view_angles.roll = RollAngle::from_radians(0.f);
}
Mat4x4 Camera::CalcViewMatrix() const
Mat4X4 Camera::calc_view_matrix() const
{
return iw_engine::CalcViewMatrix(m_viewAngles, m_origin);
return iw_engine::calc_view_matrix(m_view_angles, m_origin);
}
Mat4x4 Camera::CalcProjectionMatrix() const
Mat4X4 Camera::calc_projection_matrix() const
{
return CalcPerspectiveProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance,
m_farPlaneDistance);
return calc_perspective_projection_matrix(m_field_of_view.as_degrees(), m_view_port.AspectRatio(),
m_near_plane_distance, m_far_plane_distance);
}
} // namespace omath::openg
} // namespace omath::iw_engine

View File

@@ -3,50 +3,49 @@
//
#include "omath/engines/iw_engine/formulas.hpp"
namespace omath::iw_engine
{
Vector3<float> ForwardVector(const ViewAngles& angles)
Vector3<float> forward_vector(const ViewAngles& angles)
{
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsForward);
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<float> RightVector(const ViewAngles& angles)
Vector3<float> right_vector(const ViewAngles& angles)
{
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsRight);
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<float> UpVector(const ViewAngles& angles)
Vector3<float> up_vector(const ViewAngles& angles)
{
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsUp);
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Mat4x4 RotationMatrix(const ViewAngles& angles)
Mat4X4 rotation_matrix(const ViewAngles& angles)
{
return MatRotationAxisZ(angles.yaw) * MatRotationAxisY(angles.pitch) * MatRotationAxisX(angles.roll);
return mat_rotation_axis_z(angles.yaw) * mat_rotation_axis_y(angles.pitch) * mat_rotation_axis_x(angles.roll);
}
Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
{
return MatCameraView(ForwardVector(angles), RightVector(angles), UpVector(angles), cam_origin);
return mat_camera_view(forward_vector(angles), right_vector(angles), up_vector(angles), cam_origin);
}
Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
const float far)
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near,
const float far)
{
// NOTE: Need magic number to fix fov calculation, since IW engine inherit Quake proj matrix calculation
constexpr auto kMultiplyFactor = 0.75f;
constexpr auto k_multiply_factor = 0.75f;
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f) * kMultiplyFactor;
const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f) * k_multiply_factor;
return {
{1.f / (aspectRatio * fovHalfTan), 0, 0, 0},
{0, 1.f / (fovHalfTan), 0, 0},
{1.f / (aspect_ratio * fov_half_tan), 0, 0, 0},
{0, 1.f / (fov_half_tan), 0, 0},
{0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)},
{0, 0, 1, 0},
};

View File

@@ -4,32 +4,30 @@
#include "omath/engines/opengl_engine/camera.hpp"
#include "omath/engines/opengl_engine/formulas.hpp"
namespace omath::opengl_engine
{
Camera::Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, const float near, const float far) :
projection::Camera<Mat4x4, ViewAngles>(position, viewAngles, viewPort, fov, near, far)
Camera::Camera(const Vector3<float>& position, const ViewAngles& view_angles, const projection::ViewPort& view_port,
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, const float near, const float far)
: projection::Camera<Mat4X4, ViewAngles>(position, view_angles, view_port, fov, near, far)
{
}
void Camera::LookAt([[maybe_unused]] const Vector3<float>& target)
void Camera::look_at([[maybe_unused]] const Vector3<float>& target)
{
const float distance = m_origin.DistTo(target);
const float distance = m_origin.distance_to(target);
const auto delta = target - m_origin;
m_viewAngles.pitch = PitchAngle::FromRadians(std::asin(delta.z / distance));
m_viewAngles.yaw = -YawAngle::FromRadians(std::atan2(delta.y, delta.x));
m_viewAngles.roll = RollAngle::FromRadians(0.f);
m_view_angles.pitch = PitchAngle::from_radians(std::asin(delta.z / distance));
m_view_angles.yaw = -YawAngle::from_radians(std::atan2(delta.y, delta.x));
m_view_angles.roll = RollAngle::from_radians(0.f);
}
Mat4x4 Camera::CalcViewMatrix() const
Mat4X4 Camera::calc_view_matrix() const
{
return opengl_engine::CalcViewMatrix(m_viewAngles, m_origin);
return opengl_engine::calc_view_matrix(m_view_angles, m_origin);
}
Mat4x4 Camera::CalcProjectionMatrix() const
Mat4X4 Camera::calc_projection_matrix() const
{
return CalcPerspectiveProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance,
m_farPlaneDistance);
return calc_perspective_projection_matrix(m_field_of_view.as_degrees(), m_view_port.AspectRatio(),
m_near_plane_distance, m_far_plane_distance);
}
} // namespace omath::opengl
} // namespace omath::opengl_engine

View File

@@ -3,47 +3,48 @@
//
#include "omath/engines/opengl_engine/formulas.hpp"
namespace omath::opengl_engine
{
Vector3<float> ForwardVector(const ViewAngles& angles)
Vector3<float> forward_vector(const ViewAngles& angles)
{
const auto vec = RotationMatrix(angles) * MatColumnFromVector<float, MatStoreType::COLUMN_MAJOR>(kAbsForward);
const auto vec
= rotation_matrix(angles) * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>(k_abs_forward);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<float> RightVector(const ViewAngles& angles)
Vector3<float> right_vector(const ViewAngles& angles)
{
const auto vec = RotationMatrix(angles) * MatColumnFromVector<float, MatStoreType::COLUMN_MAJOR>(kAbsRight);
const auto vec
= rotation_matrix(angles) * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>(k_abs_right);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<float> UpVector(const ViewAngles& angles)
Vector3<float> up_vector(const ViewAngles& angles)
{
const auto vec = RotationMatrix(angles) * MatColumnFromVector<float, MatStoreType::COLUMN_MAJOR>(kAbsUp);
const auto vec = rotation_matrix(angles) * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>(k_abs_up);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
{
return MatCameraView<float, MatStoreType::COLUMN_MAJOR>(-ForwardVector(angles), RightVector(angles),
UpVector(angles), cam_origin);
return mat_camera_view<float, MatStoreType::COLUMN_MAJOR>(-forward_vector(angles), right_vector(angles),
up_vector(angles), cam_origin);
}
Mat4x4 RotationMatrix(const ViewAngles& angles)
Mat4X4 rotation_matrix(const ViewAngles& angles)
{
return MatRotationAxisX<float, MatStoreType::COLUMN_MAJOR>(-angles.pitch) *
MatRotationAxisY<float, MatStoreType::COLUMN_MAJOR>(-angles.yaw) *
MatRotationAxisZ<float, MatStoreType::COLUMN_MAJOR>(angles.roll);
return mat_rotation_axis_x<float, MatStoreType::COLUMN_MAJOR>(-angles.pitch)
* mat_rotation_axis_y<float, MatStoreType::COLUMN_MAJOR>(-angles.yaw)
* mat_rotation_axis_z<float, MatStoreType::COLUMN_MAJOR>(angles.roll);
}
Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
const float far)
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near,
const float far)
{
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);
const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f);
return {
{1.f / (aspectRatio * fovHalfTan), 0, 0, 0},
{0, 1.f / (fovHalfTan), 0, 0},
{1.f / (aspect_ratio * fov_half_tan), 0, 0, 0},
{0, 1.f / (fov_half_tan), 0, 0},
{0, 0, -(far + near) / (far - near), -(2.f * far * near) / (far - near)},
{0, 0, -1, 0},
};

View File

@@ -4,34 +4,32 @@
#include "omath/engines/source_engine/camera.hpp"
#include "omath/engines/source_engine/formulas.hpp"
namespace omath::source_engine
{
Camera::Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
const projection::FieldOfView& fov, const float near, const float far) :
projection::Camera<Mat4x4, ViewAngles>(position, viewAngles, viewPort, fov, near, far)
Camera::Camera(const Vector3<float>& position, const ViewAngles& view_angles, const projection::ViewPort& view_port,
const projection::FieldOfView& fov, const float near, const float far)
: projection::Camera<Mat4X4, ViewAngles>(position, view_angles, view_port, fov, near, far)
{
}
void Camera::LookAt(const Vector3<float>& target)
void Camera::look_at(const Vector3<float>& target)
{
const float distance = m_origin.DistTo(target);
const float distance = m_origin.distance_to(target);
const auto delta = target - m_origin;
m_viewAngles.pitch = PitchAngle::FromRadians(std::asin(delta.z / distance));
m_viewAngles.yaw = -YawAngle::FromRadians(std::atan2(delta.y, delta.x));
m_viewAngles.roll = RollAngle::FromRadians(0.f);
m_view_angles.pitch = PitchAngle::from_radians(std::asin(delta.z / distance));
m_view_angles.yaw = -YawAngle::from_radians(std::atan2(delta.y, delta.x));
m_view_angles.roll = RollAngle::from_radians(0.f);
}
Mat4x4 Camera::CalcViewMatrix() const
Mat4X4 Camera::calc_view_matrix() const
{
return source_engine::CalcViewMatrix(m_viewAngles, m_origin);
return source_engine::calc_view_matrix(m_view_angles, m_origin);
}
Mat4x4 Camera::CalcProjectionMatrix() const
Mat4X4 Camera::calc_projection_matrix() const
{
return CalcPerspectiveProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance,
m_farPlaneDistance);
return calc_perspective_projection_matrix(m_field_of_view.as_degrees(), m_view_port.AspectRatio(),
m_near_plane_distance, m_far_plane_distance);
}
} // namespace omath::source
} // namespace omath::source_engine

View File

@@ -3,50 +3,49 @@
//
#include <omath/engines/source_engine/formulas.hpp>
namespace omath::source_engine
{
Vector3<float> ForwardVector(const ViewAngles& angles)
Vector3<float> forward_vector(const ViewAngles& angles)
{
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsForward);
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Mat4x4 RotationMatrix(const ViewAngles& angles)
Mat4X4 rotation_matrix(const ViewAngles& angles)
{
return MatRotationAxisZ(angles.yaw) * MatRotationAxisY(angles.pitch) * MatRotationAxisX(angles.roll);
return mat_rotation_axis_z(angles.yaw) * mat_rotation_axis_y(angles.pitch) * mat_rotation_axis_x(angles.roll);
}
Vector3<float> RightVector(const ViewAngles& angles)
Vector3<float> right_vector(const ViewAngles& angles)
{
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsRight);
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<float> UpVector(const ViewAngles& angles)
Vector3<float> up_vector(const ViewAngles& angles)
{
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsUp);
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
{
return MatCameraView(ForwardVector(angles), RightVector(angles), UpVector(angles), cam_origin);
return mat_camera_view(forward_vector(angles), right_vector(angles), up_vector(angles), cam_origin);
}
Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
const float far)
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near,
const float far)
{
// NOTE: Need magic number to fix fov calculation, since source inherit Quake proj matrix calculation
constexpr auto kMultiplyFactor = 0.75f;
constexpr auto k_multiply_factor = 0.75f;
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f) * kMultiplyFactor;
const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f) * k_multiply_factor;
return {
{1.f / (aspectRatio * fovHalfTan), 0, 0, 0},
{0, 1.f / (fovHalfTan), 0, 0},
{1.f / (aspect_ratio * fov_half_tan), 0, 0, 0},
{0, 1.f / (fov_half_tan), 0, 0},
{0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)},
{0, 0, 1, 0},
};

View File

@@ -4,25 +4,24 @@
#include <omath/engines/unity_engine/camera.hpp>
#include <omath/engines/unity_engine/formulas.hpp>
namespace omath::unity_engine
{
Camera::Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
const projection::FieldOfView& fov, const float near, const float far) :
projection::Camera<Mat4x4, ViewAngles>(position, viewAngles, viewPort, fov, near, far)
Camera::Camera(const Vector3<float>& position, const ViewAngles& view_angles, const projection::ViewPort& view_port,
const projection::FieldOfView& fov, const float near, const float far)
: projection::Camera<Mat4X4, ViewAngles>(position, view_angles, view_port, fov, near, far)
{
}
void Camera::LookAt([[maybe_unused]] const Vector3<float>& target)
void Camera::look_at([[maybe_unused]] const Vector3<float>& target)
{
throw std::runtime_error("Not implemented");
}
Mat4x4 Camera::CalcViewMatrix() const
Mat4X4 Camera::calc_view_matrix() const
{
return unity_engine::CalcViewMatrix(m_viewAngles, m_origin);
return unity_engine::calc_view_matrix(m_view_angles, m_origin);
}
Mat4x4 Camera::CalcProjectionMatrix() const
Mat4X4 Camera::calc_projection_matrix() const
{
return CalcPerspectiveProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance,
m_farPlaneDistance);
return calc_perspective_projection_matrix(m_field_of_view.as_degrees(), m_view_port.AspectRatio(),
m_near_plane_distance, m_far_plane_distance);
}
} // namespace omath::unity_engine

View File

@@ -3,47 +3,45 @@
//
#include "omath/engines/unity_engine/formulas.hpp"
namespace omath::unity_engine
{
Vector3<float> ForwardVector(const ViewAngles& angles)
Vector3<float> forward_vector(const ViewAngles& angles)
{
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsForward);
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<float> RightVector(const ViewAngles& angles)
Vector3<float> right_vector(const ViewAngles& angles)
{
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsRight);
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Vector3<float> UpVector(const ViewAngles& angles)
Vector3<float> up_vector(const ViewAngles& angles)
{
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsUp);
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
}
Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
{
return MatCameraView<float, MatStoreType::ROW_MAJOR>(ForwardVector(angles), -RightVector(angles),
UpVector(angles), cam_origin);
return mat_camera_view<float, MatStoreType::ROW_MAJOR>(forward_vector(angles), -right_vector(angles),
up_vector(angles), cam_origin);
}
Mat4x4 RotationMatrix(const ViewAngles& angles)
Mat4X4 rotation_matrix(const ViewAngles& angles)
{
return MatRotationAxisX<float, MatStoreType::ROW_MAJOR>(angles.pitch) *
MatRotationAxisY<float, MatStoreType::ROW_MAJOR>(angles.yaw) *
MatRotationAxisZ<float, MatStoreType::ROW_MAJOR>(angles.roll);
return mat_rotation_axis_x<float, MatStoreType::ROW_MAJOR>(angles.pitch)
* mat_rotation_axis_y<float, MatStoreType::ROW_MAJOR>(angles.yaw)
* mat_rotation_axis_z<float, MatStoreType::ROW_MAJOR>(angles.roll);
}
Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
const float far)
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near,
const float far)
{
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);
const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f);
return {
{1.f / (aspectRatio * fovHalfTan), 0, 0, 0},
{0, 1.f / (fovHalfTan), 0, 0},
{1.f / (aspect_ratio * fov_half_tan), 0, 0, 0},
{0, 1.f / (fov_half_tan), 0, 0},
{0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)},
{0, 0, -1.f, 0},
};

View File

@@ -1,14 +1,11 @@
#include "omath/matrix.hpp"
#include "omath/angles.hpp"
#include "omath/vector3.hpp"
#include <complex>
#include <format>
#include <stdexcept>
#include <utility>
namespace omath
{
Matrix::Matrix(const size_t rows, const size_t columns)
@@ -21,7 +18,7 @@ namespace omath
m_data = std::make_unique<float[]>(m_rows * m_columns);
Set(0.f);
set(0.f);
}
Matrix::Matrix(const std::initializer_list<std::initializer_list<float>>& rows)
@@ -29,7 +26,6 @@ namespace omath
m_rows = rows.size();
m_columns = rows.begin()->size();
for (const auto& row: rows)
if (row.size() != m_columns)
throw std::invalid_argument("All rows must have the same number of columns.");
@@ -41,7 +37,7 @@ namespace omath
{
size_t j = 0;
for (const auto& value: row)
At(i, j++) = value;
at(i, j++) = value;
++i;
}
}
@@ -55,29 +51,28 @@ namespace omath
for (size_t i = 0; i < m_rows; ++i)
for (size_t j = 0; j < m_columns; ++j)
At(i, j) = other.At(i, j);
at(i, j) = other.at(i, j);
}
Matrix::Matrix(const size_t rows, const size_t columns, const float* pRaw)
Matrix::Matrix(const size_t rows, const size_t columns, const float* raw_data)
{
m_rows = rows;
m_columns = columns;
m_data = std::make_unique<float[]>(m_rows * m_columns);
for (size_t i = 0; i < rows * columns; ++i)
At(i / rows, i % columns) = pRaw[i];
at(i / rows, i % columns) = raw_data[i];
}
size_t Matrix::RowCount() const noexcept
size_t Matrix::row_count() const noexcept
{
return m_rows;
}
float& Matrix::operator[](const size_t row, const size_t column)
{
return At(row, column);
return at(row, column);
}
Matrix::Matrix(Matrix&& other) noexcept
@@ -92,35 +87,35 @@ namespace omath
other.m_data = nullptr;
}
size_t Matrix::ColumnsCount() const noexcept
size_t Matrix::columns_count() const noexcept
{
return m_columns;
}
std::pair<size_t, size_t> Matrix::Size() const noexcept
std::pair<size_t, size_t> Matrix::size() const noexcept
{
return {RowCount(), ColumnsCount()};
return {row_count(), columns_count()};
}
float& Matrix::At(const size_t iRow, const size_t iCol)
float& Matrix::at(const size_t row, const size_t col)
{
return const_cast<float&>(std::as_const(*this).At(iRow, iCol));
return const_cast<float&>(std::as_const(*this).at(row, col));
}
float Matrix::Sum()
float Matrix::sum()
{
float sum = 0;
for (size_t i = 0; i < RowCount(); i++)
for (size_t j = 0; j < ColumnsCount(); j++)
sum += At(i, j);
for (size_t i = 0; i < row_count(); i++)
for (size_t j = 0; j < columns_count(); j++)
sum += at(i, j);
return sum;
}
const float& Matrix::At(const size_t iRow, const size_t iCol) const
const float& Matrix::at(const size_t row, const size_t col) const
{
return m_data[iRow * m_columns + iCol];
return m_data[row * m_columns + col];
}
Matrix Matrix::operator*(const Matrix& other) const
@@ -128,15 +123,14 @@ namespace omath
if (m_columns != other.m_rows)
throw std::runtime_error("n != m");
auto outMat = Matrix(m_rows, other.m_columns);
auto out_mat = Matrix(m_rows, other.m_columns);
for (size_t d = 0; d < m_rows; ++d)
for (size_t i = 0; i < other.m_columns; ++i)
for (size_t j = 0; j < other.m_rows; ++j)
outMat.At(d, i) += At(d, j) * other.At(j, i);
out_mat.at(d, i) += at(d, j) * other.at(j, i);
return outMat;
return out_mat;
}
Matrix& Matrix::operator*=(const Matrix& other)
@@ -150,22 +144,22 @@ namespace omath
auto out = *this;
for (size_t i = 0; i < m_rows; ++i)
for (size_t j = 0; j < m_columns; ++j)
out.At(i, j) *= f;
out.at(i, j) *= f;
return out;
}
Matrix& Matrix::operator*=(const float f)
{
for (size_t i = 0; i < RowCount(); i++)
for (size_t j = 0; j < ColumnsCount(); j++)
At(i, j) *= f;
for (size_t i = 0; i < row_count(); i++)
for (size_t j = 0; j < columns_count(); j++)
at(i, j) *= f;
return *this;
}
void Matrix::Clear()
void Matrix::clear()
{
Set(0.f);
set(0.f);
}
Matrix& Matrix::operator=(const Matrix& other)
@@ -175,7 +169,7 @@ namespace omath
for (size_t i = 0; i < m_rows; ++i)
for (size_t j = 0; j < m_columns; ++j)
At(i, j) = other.At(i, j);
at(i, j) = other.at(i, j);
return *this;
}
@@ -199,7 +193,7 @@ namespace omath
{
for (size_t i = 0; i < m_rows; ++i)
for (size_t j = 0; j < m_columns; ++j)
At(i, j) /= f;
at(i, j) /= f;
return *this;
}
@@ -209,12 +203,12 @@ namespace omath
auto out = *this;
for (size_t i = 0; i < m_rows; ++i)
for (size_t j = 0; j < m_columns; ++j)
out.At(i, j) /= f;
out.at(i, j) /= f;
return out;
}
std::string Matrix::ToString() const
std::string Matrix::to_string() const
{
std::string str;
@@ -222,7 +216,7 @@ namespace omath
{
for (size_t j = 0; j < m_columns; ++j)
{
str += std::format("{:.1f}", At(i, j));
str += std::format("{:.1f}", at(i, j));
if (j == m_columns - 1)
str += '\n';
@@ -233,89 +227,89 @@ namespace omath
return str;
}
float Matrix::Determinant() const
float Matrix::determinant() const // NOLINT(*-no-recursion)
{
if (m_rows + m_columns == 2)
return At(0, 0);
return at(0, 0);
if (m_rows == 2 and m_columns == 2)
return At(0, 0) * At(1, 1) - At(0, 1) * At(1, 0);
return at(0, 0) * at(1, 1) - at(0, 1) * at(1, 0);
float fDet = 0;
float det = 0;
for (size_t i = 0; i < m_columns; i++)
fDet += AlgComplement(0, i) * At(0, i);
det += alg_complement(0, i) * at(0, i);
return fDet;
return det;
}
float Matrix::AlgComplement(const size_t i, const size_t j) const
float Matrix::alg_complement(const size_t i, const size_t j) const // NOLINT(*-no-recursion)
{
const auto tmp = Minor(i, j);
const auto tmp = minor(i, j);
return ((i + j + 2) % 2 == 0) ? tmp : -tmp;
}
Matrix Matrix::Transpose() const
Matrix Matrix::transpose() const
{
Matrix transposed = {m_columns, m_rows};
for (size_t i = 0; i < m_rows; ++i)
for (size_t j = 0; j < m_columns; ++j)
transposed.At(j, i) = At(i, j);
transposed.at(j, i) = at(i, j);
return transposed;
}
Matrix::~Matrix() = default;
void Matrix::Set(const float val)
void Matrix::set(const float val)
{
for (size_t i = 0; i < m_rows; ++i)
for (size_t j = 0; j < m_columns; ++j)
At(i, j) = val;
at(i, j) = val;
}
Matrix Matrix::Strip(const size_t row, const size_t column) const
Matrix Matrix::strip(const size_t row, const size_t column) const
{
Matrix stripped = {m_rows - 1, m_columns - 1};
size_t iStripRowIndex = 0;
size_t strip_row_index = 0;
for (size_t i = 0; i < m_rows; i++)
{
if (i == row)
continue;
size_t iStripColumnIndex = 0;
size_t strip_column_index = 0;
for (size_t j = 0; j < m_columns; ++j)
{
if (j == column)
continue;
stripped.At(iStripRowIndex, iStripColumnIndex) = At(i, j);
iStripColumnIndex++;
stripped.at(strip_row_index, strip_column_index) = at(i, j);
strip_column_index++;
}
iStripRowIndex++;
strip_row_index++;
}
return stripped;
}
float Matrix::Minor(const size_t i, const size_t j) const
float Matrix::minor(const size_t i, const size_t j) const // NOLINT(*-no-recursion)
{
return Strip(i, j).Determinant();
return strip(i, j).determinant();
}
Matrix Matrix::ToScreenMatrix(const float screenWidth, const float screenHeight)
Matrix Matrix::to_screen_matrix(const float screen_width, const float screen_height)
{
return {
{screenWidth / 2.f, 0.f, 0.f, 0.f},
{0.f, -screenHeight / 2.f, 0.f, 0.f},
{screen_width / 2.f, 0.f, 0.f, 0.f},
{0.f, -screen_height / 2.f, 0.f, 0.f},
{0.f, 0.f, 1.f, 0.f},
{screenWidth / 2.f, screenHeight / 2.f, 0.f, 1.f},
{screen_width / 2.f, screen_height / 2.f, 0.f, 1.f},
};
}
Matrix Matrix::TranslationMatrix(const Vector3<float>& diff)
Matrix Matrix::translation_matrix(const Vector3<float>& diff)
{
return {
{1.f, 0.f, 0.f, 0.f},
@@ -325,7 +319,8 @@ namespace omath
};
}
Matrix Matrix::OrientationMatrix(const Vector3<float>& forward, const Vector3<float>& right, const Vector3<float>& up)
Matrix Matrix::orientation_matrix(const Vector3<float>& forward, const Vector3<float>& right,
const Vector3<float>& up)
{
return {
{right.x, up.x, forward.x, 0.f},
@@ -335,25 +330,26 @@ namespace omath
};
}
Matrix Matrix::ProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near, const float far)
Matrix Matrix::projection_matrix(const float field_of_view, const float aspect_ratio, const float near,
const float far)
{
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);
const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f);
return {{1.f / (aspectRatio * fovHalfTan), 0.f, 0.f, 0.f},
{0.f, 1.f / fovHalfTan, 0.f, 0.f},
return {{1.f / (aspect_ratio * fov_half_tan), 0.f, 0.f, 0.f},
{0.f, 1.f / fov_half_tan, 0.f, 0.f},
{0.f, 0.f, (far + near) / (far - near), 2.f * near * far / (far - near)},
{0.f, 0.f, -1.f, 0.f}};
}
const float* Matrix::Raw() const
const float* Matrix::raw() const
{
return m_data.get();
}
void Matrix::SetDataFromRaw(const float* pRawMatrix)
void Matrix::set_data_from_raw(const float* raw_matrix)
{
for (size_t i = 0; i < m_columns * m_rows; ++i)
At(i / m_rows, i % m_columns) = pRawMatrix[i];
at(i / m_rows, i % m_columns) = raw_matrix[i];
}
Matrix::Matrix()

View File

@@ -2,96 +2,94 @@
// Created by Vlad on 28.07.2024.
//
#include "omath/pathfinding/a_star.hpp"
#include <algorithm>
#include <optional>
#include <unordered_map>
#include <unordered_set>
namespace omath::pathfinding
{
struct PathNode final
{
std::optional<Vector3<float>> cameFrom;
float gCost = 0.f;
std::optional<Vector3<float>> came_from;
float g_cost = 0.f;
};
std::vector<Vector3<float>> Astar::ReconstructFinalPath(const std::unordered_map<Vector3<float>, PathNode>& closedList,
const Vector3<float>& current)
std::vector<Vector3<float>>
Astar::reconstruct_final_path(const std::unordered_map<Vector3<float>, PathNode>& closed_list,
const Vector3<float>& current)
{
std::vector<Vector3<float>> path;
std::optional currentOpt = current;
std::optional current_opt = current;
while (currentOpt)
while (current_opt)
{
path.push_back(*currentOpt);
path.push_back(*current_opt);
auto it = closedList.find(*currentOpt);
auto it = closed_list.find(*current_opt);
if (it == closedList.end())
if (it == closed_list.end())
break;
currentOpt = it->second.cameFrom;
current_opt = it->second.came_from;
}
std::ranges::reverse(path);
return path;
}
auto Astar::GetPerfectNode(const std::unordered_map<Vector3<float>, PathNode>& openList, const Vector3<float>& endVertex)
auto Astar::get_perfect_node(const std::unordered_map<Vector3<float>, PathNode>& open_list,
const Vector3<float>& endVertex)
{
return std::ranges::min_element(openList,
return std::ranges::min_element(open_list,
[&endVertex](const auto& a, const auto& b)
{
const float fA = a.second.gCost + a.first.DistTo(endVertex);
const float fB = b.second.gCost + b.first.DistTo(endVertex);
return fA < fB;
const float fa = a.second.g_cost + a.first.distance_to(endVertex);
const float fb = b.second.g_cost + b.first.distance_to(endVertex);
return fa < fb;
});
}
std::vector<Vector3<float>> Astar::FindPath(const Vector3<float>& start, const Vector3<float>& end, const NavigationMesh& navMesh)
std::vector<Vector3<float>> Astar::find_path(const Vector3<float>& start, const Vector3<float>& end,
const NavigationMesh& nav_mesh)
{
std::unordered_map<Vector3<float>, PathNode> closedList;
std::unordered_map<Vector3<float>, PathNode> openList;
std::unordered_map<Vector3<float>, PathNode> closed_list;
std::unordered_map<Vector3<float>, PathNode> open_list;
auto maybeStartVertex = navMesh.GetClosestVertex(start);
auto maybeEndVertex = navMesh.GetClosestVertex(end);
auto maybe_start_vertex = nav_mesh.get_closest_vertex(start);
auto maybe_end_vertex = nav_mesh.get_closest_vertex(end);
if (!maybeStartVertex || !maybeEndVertex)
if (!maybe_start_vertex || !maybe_end_vertex)
return {};
const auto startVertex = maybeStartVertex.value();
const auto endVertex = maybeEndVertex.value();
const auto start_vertex = maybe_start_vertex.value();
const auto end_vertex = maybe_end_vertex.value();
open_list.emplace(start_vertex, PathNode{std::nullopt, 0.f});
openList.emplace(startVertex, PathNode{std::nullopt, 0.f});
while (!openList.empty())
while (!open_list.empty())
{
auto currentIt = GetPerfectNode(openList, endVertex);
auto current_it = get_perfect_node(open_list, end_vertex);
const auto current = currentIt->first;
const auto currentNode = currentIt->second;
const auto current = current_it->first;
const auto current_node = current_it->second;
if (current == endVertex)
return ReconstructFinalPath(closedList, current);
if (current == end_vertex)
return reconstruct_final_path(closed_list, current);
closed_list.emplace(current, current_node);
open_list.erase(current_it);
closedList.emplace(current, currentNode);
openList.erase(currentIt);
for (const auto& neighbor: navMesh.GetNeighbors(current))
for (const auto& neighbor: nav_mesh.get_neighbors(current))
{
if (closedList.contains(neighbor))
if (closed_list.contains(neighbor))
continue;
const float tentativeGCost = currentNode.gCost + neighbor.DistTo(current);
const float tentative_g_cost = current_node.g_cost + neighbor.distance_to(current);
const auto openIt = openList.find(neighbor);
const auto open_it = open_list.find(neighbor);
if (openIt == openList.end() || tentativeGCost < openIt->second.gCost)
openList[neighbor] = PathNode{current, tentativeGCost};
if (open_it == open_list.end() || tentative_g_cost < open_it->second.g_cost)
open_list[neighbor] = PathNode{current, tentative_g_cost};
}
}

View File

@@ -2,94 +2,89 @@
// Created by Vlad on 28.07.2024.
//
#include "omath/pathfinding/navigation_mesh.hpp"
#include <algorithm>
#include <stdexcept>
namespace omath::pathfinding
{
std::expected<Vector3<float>, std::string> NavigationMesh::GetClosestVertex(const Vector3<float> &point) const
std::expected<Vector3<float>, std::string> NavigationMesh::get_closest_vertex(const Vector3<float>& point) const
{
const auto res = std::ranges::min_element(m_verTextMap,
[&point](const auto& a, const auto& b)
{
return a.first.DistTo(point) < b.first.DistTo(point);
});
const auto res = std::ranges::min_element(m_vertex_map, [&point](const auto& a, const auto& b)
{ return a.first.distance_to(point) < b.first.distance_to(point); });
if (res == m_verTextMap.cend())
if (res == m_vertex_map.cend())
return std::unexpected("Failed to get clossest point");
return res->first;
}
const std::vector<Vector3<float>>& NavigationMesh::GetNeighbors(const Vector3<float> &vertex) const
const std::vector<Vector3<float>>& NavigationMesh::get_neighbors(const Vector3<float>& vertex) const
{
return m_verTextMap.at(vertex);
return m_vertex_map.at(vertex);
}
bool NavigationMesh::Empty() const
bool NavigationMesh::empty() const
{
return m_verTextMap.empty();
return m_vertex_map.empty();
}
std::vector<uint8_t> NavigationMesh::Serialize() const
std::vector<uint8_t> NavigationMesh::serialize() const
{
auto dumpToVector =[]<typename T>(const T& t, std::vector<uint8_t>& vec){
auto dump_to_vector = []<typename T>(const T& t, std::vector<uint8_t>& vec)
{
for (size_t i = 0; i < sizeof(t); i++)
vec.push_back(*(reinterpret_cast<const uint8_t*>(&t)+i));
vec.push_back(*(reinterpret_cast<const uint8_t*>(&t) + i));
};
std::vector<uint8_t> raw;
for (const auto& [vertex, neighbors] : m_verTextMap)
for (const auto& [vertex, neighbors]: m_vertex_map)
{
const auto neighborsCount = neighbors.size();
const auto neighbors_count = neighbors.size();
dumpToVector(vertex, raw);
dumpToVector(neighborsCount, raw);
dump_to_vector(vertex, raw);
dump_to_vector(neighbors_count, raw);
for (const auto& neighbor : neighbors)
dumpToVector(neighbor, raw);
for (const auto& neighbor: neighbors)
dump_to_vector(neighbor, raw);
}
return raw;
}
void NavigationMesh::Deserialize(const std::vector<uint8_t> &raw)
void NavigationMesh::deserialize(const std::vector<uint8_t>& raw)
{
auto loadFromVector = [](const std::vector<uint8_t>& vec, size_t& offset, auto& value)
auto load_from_vector = [](const std::vector<uint8_t>& vec, size_t& offset, auto& value)
{
if (offset + sizeof(value) > vec.size())
{
throw std::runtime_error("Deserialize: Invalid input data size.");
}
std::copy_n(vec.data() + offset, sizeof(value), (uint8_t*)&value);
std::copy_n(vec.data() + offset, sizeof(value), reinterpret_cast<uint8_t*>(&value));
offset += sizeof(value);
};
m_verTextMap.clear();
m_vertex_map.clear();
size_t offset = 0;
while (offset < raw.size())
{
Vector3<float> vertex;
loadFromVector(raw, offset, vertex);
load_from_vector(raw, offset, vertex);
uint16_t neighborsCount;
loadFromVector(raw, offset, neighborsCount);
uint16_t neighbors_count;
load_from_vector(raw, offset, neighbors_count);
std::vector<Vector3<float>> neighbors;
neighbors.reserve(neighborsCount);
neighbors.reserve(neighbors_count);
for (size_t i = 0; i < neighborsCount; ++i)
for (size_t i = 0; i < neighbors_count; ++i)
{
Vector3<float> neighbor;
loadFromVector(raw, offset, neighbor);
load_from_vector(raw, offset, neighbor);
neighbors.push_back(neighbor);
}
m_verTextMap.emplace(vertex, std::move(neighbors));
m_vertex_map.emplace(vertex, std::move(neighbors));
}
}
}
} // namespace omath::pathfinding

View File

@@ -3,7 +3,6 @@
//
#include "omath/projectile_prediction/proj_pred_engine.hpp"
namespace omath::projectile_prediction
{

View File

@@ -14,8 +14,8 @@
namespace omath::projectile_prediction
{
std::optional<Vector3<float>>
ProjPredEngineAVX2::MaybeCalculateAimPoint([[maybe_unused]] const Projectile& projectile,
[[maybe_unused]] const Target& target) const
ProjPredEngineAvx2::maybe_calculate_aim_point([[maybe_unused]] const Projectile& projectile,
[[maybe_unused]] const Target& target) const
{
#if defined(OMATH_USE_AVX2) && defined(__i386__) && defined(__x86_64__)
const float bulletGravity = m_gravityConstant * projectile.m_gravityScale;
@@ -28,16 +28,16 @@ namespace omath::projectile_prediction
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 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 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,
@@ -112,25 +112,32 @@ namespace omath::projectile_prediction
}
return std::nullopt;
#else
throw std::runtime_error(
std::format("{} AVX2 feature is not enabled!", std::source_location::current().function_name()));
#endif
}
ProjPredEngineAVX2::ProjPredEngineAVX2(const float gravityConstant, const float simulationTimeStep,
const float maximumSimulationTime) :
m_gravityConstant(gravityConstant), m_simulationTimeStep(simulationTimeStep),
m_maximumSimulationTime(maximumSimulationTime)
ProjPredEngineAvx2::ProjPredEngineAvx2(const float gravity_constant, const float simulation_time_step,
const float maximum_simulation_time)
: m_gravity_constant(gravity_constant), m_simulation_time_step(simulation_time_step),
m_maximum_simulation_time(maximum_simulation_time)
{
}
std::optional<float> ProjPredEngineAVX2::CalculatePitch(const Vector3<float>& projOrigin,
const Vector3<float>& targetPos, const float bulletGravity,
const float v0, const float time)
std::optional<float> ProjPredEngineAvx2::calculate_pitch([[maybe_unused]] const Vector3<float>& proj_origin,
[[maybe_unused]] const Vector3<float>& target_pos,
[[maybe_unused]] const float bullet_gravity,
[[maybe_unused]] const float v0,
[[maybe_unused]] const float time)
{
#if defined(OMATH_USE_AVX2) && defined(__i386__) && defined(__x86_64__)
if (time <= 0.0f)
return std::nullopt;
const Vector3 delta = targetPos - projOrigin;
const Vector3 delta = target_pos - proj_origin;
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 term = h + 0.5f * bullet_gravity * time * time;
const float requiredV0Sqr = (dSqr + term * term) / (time * time);
const float v0Sqr = v0 * v0;
@@ -140,7 +147,6 @@ namespace omath::projectile_prediction
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));

View File

@@ -4,65 +4,67 @@
namespace omath::projectile_prediction
{
ProjPredEngineLegacy::ProjPredEngineLegacy(const float gravityConstant, const float simulationTimeStep,
const float maximumSimulationTime, const float distanceTolerance) :
m_gravityConstant(gravityConstant), m_simulationTimeStep(simulationTimeStep),
m_maximumSimulationTime(maximumSimulationTime), m_distanceTolerance(distanceTolerance)
ProjPredEngineLegacy::ProjPredEngineLegacy(const float gravity_constant, const float simulation_time_step,
const float maximum_simulation_time, const float distance_tolerance)
: m_gravity_constant(gravity_constant), m_simulation_time_step(simulation_time_step),
m_maximum_simulation_time(maximum_simulation_time), m_distance_tolerance(distance_tolerance)
{
}
std::optional<Vector3<float>> ProjPredEngineLegacy::MaybeCalculateAimPoint(const Projectile& projectile,
const Target& target) const
std::optional<Vector3<float>> ProjPredEngineLegacy::maybe_calculate_aim_point(const Projectile& projectile,
const Target& target) const
{
for (float time = 0.f; time < m_maximumSimulationTime; time += m_simulationTimeStep)
for (float time = 0.f; time < m_maximum_simulation_time; time += m_simulation_time_step)
{
const auto predictedTargetPosition = target.PredictPosition(time, m_gravityConstant);
const auto predicted_target_position = target.predict_position(time, m_gravity_constant);
const auto projectilePitch = MaybeCalculateProjectileLaunchPitchAngle(projectile, predictedTargetPosition);
const auto projectile_pitch
= maybe_calculate_projectile_launch_pitch_angle(projectile, predicted_target_position);
if (!projectilePitch.has_value()) [[unlikely]]
if (!projectile_pitch.has_value()) [[unlikely]]
continue;
if (!IsProjectileReachedTarget(predictedTargetPosition, projectile, projectilePitch.value(), time))
if (!is_projectile_reached_target(predicted_target_position, projectile, projectile_pitch.value(), time))
continue;
const auto delta2d = (predictedTargetPosition - projectile.m_origin).Length2D();
const auto height = delta2d * std::tan(angles::DegreesToRadians(projectilePitch.value()));
const auto delta2d = (predicted_target_position - projectile.m_origin).length_2d();
const auto height = delta2d * std::tan(angles::degrees_to_radians(projectile_pitch.value()));
return Vector3(predictedTargetPosition.x, predictedTargetPosition.y, projectile.m_origin.z + height);
return Vector3(predicted_target_position.x, predicted_target_position.y, projectile.m_origin.z + height);
}
return std::nullopt;
}
std::optional<float>
ProjPredEngineLegacy::MaybeCalculateProjectileLaunchPitchAngle(const Projectile& projectile,
const Vector3<float>& targetPosition) const
ProjPredEngineLegacy::maybe_calculate_projectile_launch_pitch_angle(const Projectile& projectile,
const Vector3<float>& target_position) const
{
const auto bulletGravity = m_gravityConstant * projectile.m_gravityScale;
const auto delta = targetPosition - projectile.m_origin;
const auto bullet_gravity = m_gravity_constant * projectile.m_gravity_scale;
const auto delta = target_position - projectile.m_origin;
const auto distance2d = delta.Length2D();
const auto distance2dSqr = distance2d * distance2d;
const auto launchSpeedSqr = projectile.m_launchSpeed * projectile.m_launchSpeed;
const auto distance2d = delta.length_2d();
const auto distance2d_sqr = distance2d * distance2d;
const auto launch_speed_sqr = projectile.m_launch_speed * projectile.m_launch_speed;
float root = launchSpeedSqr * launchSpeedSqr -
bulletGravity * (bulletGravity * distance2dSqr + 2.0f * delta.z * launchSpeedSqr);
float root = launch_speed_sqr * launch_speed_sqr
- bullet_gravity * (bullet_gravity * distance2d_sqr + 2.0f * delta.z * launch_speed_sqr);
if (root < 0.0f) [[unlikely]]
return std::nullopt;
root = std::sqrt(root);
const float angle = std::atan((launchSpeedSqr - root) / (bulletGravity * distance2d));
const float angle = std::atan((launch_speed_sqr - root) / (bullet_gravity * distance2d));
return angles::RadiansToDegrees(angle);
return angles::radians_to_degrees(angle);
}
bool ProjPredEngineLegacy::IsProjectileReachedTarget(const Vector3<float>& targetPosition, const Projectile& projectile,
const float pitch, const float time) const
bool ProjPredEngineLegacy::is_projectile_reached_target(const Vector3<float>& target_position,
const Projectile& projectile, const float pitch,
const float time) const
{
const auto yaw = projectile.m_origin.ViewAngleTo(targetPosition).y;
const auto projectilePosition = projectile.PredictPosition(pitch, yaw, time, m_gravityConstant);
const auto yaw = projectile.m_origin.view_angle_to(target_position).y;
const auto projectile_position = projectile.predict_position(pitch, yaw, time, m_gravity_constant);
return projectilePosition.DistTo(targetPosition) <= m_distanceTolerance;
return projectile_position.distance_to(target_position) <= m_distance_tolerance;
}
} // namespace omath::projectile_prediction

View File

@@ -3,19 +3,20 @@
//
#include "omath/projectile_prediction/projectile.hpp"
#include <omath/engines/source_engine/formulas.hpp>
namespace omath::projectile_prediction
{
Vector3<float> Projectile::PredictPosition(const float pitch, const float yaw, const float time, const float gravity) const
Vector3<float> Projectile::predict_position(const float pitch, const float yaw, const float time,
const float gravity) const
{
auto currentPos = m_origin + source_engine::ForwardVector({source_engine::PitchAngle::FromDegrees(-pitch),
source_engine::YawAngle::FromDegrees(yaw),
source_engine::RollAngle::FromDegrees(0)}) *
m_launchSpeed * time;
currentPos.z -= (gravity * m_gravityScale) * (time * time) * 0.5f;
auto current_pos = m_origin
+ source_engine::forward_vector({source_engine::PitchAngle::from_degrees(-pitch),
source_engine::YawAngle::from_degrees(yaw),
source_engine::RollAngle::from_degrees(0)})
* m_launch_speed * time;
current_pos.z -= (gravity * m_gravity_scale) * (time * time) * 0.5f;
return currentPos;
return current_pos;
}
} // namespace omath::prediction
} // namespace omath::projectile_prediction

View File

@@ -4,7 +4,6 @@
#include "omath/projectile_prediction/projectile.hpp"
namespace omath::prediction
{

View File

@@ -3,7 +3,6 @@
//
#include "omath/projection/camera.hpp"
namespace omath::projection
{
}