added vec2

This commit is contained in:
2024-09-02 16:14:03 +03:00
parent a40bf98763
commit de7ef015dd
6 changed files with 277 additions and 62 deletions

74
include/omath/Vector2.h Normal file
View File

@@ -0,0 +1,74 @@
//
// Created by Vlad on 02.09.2024.
//
#pragma once
#include <tuple>
namespace omath
{
class Vector2
{
public:
float x = 0.f;
float y = 0.f;
// Constructors
Vector2() = default;
Vector2(float x, float y);
// Equality operators
bool operator==(const Vector2& src) const;
bool operator!=(const Vector2& src) const;
// Compound assignment operators
Vector2& operator+=(const Vector2& v);
Vector2& operator-=(const Vector2& v);
Vector2& operator*=(const Vector2& v);
Vector2& operator/=(const Vector2& v);
Vector2& operator*=(float fl);
Vector2& operator/=(float fl);
Vector2& operator+=(float fl);
Vector2& operator-=(float fl);
// Basic vector operations
[[nodiscard]] float DistTo(const Vector2& vOther) const;
[[nodiscard]] float DistToSqr(const Vector2& vOther) const;
[[nodiscard]] float Dot(const Vector2& vOther) const;
[[nodiscard]] float Length() const;
[[nodiscard]] float LengthSqr() const;
Vector2& Abs();
template<class type>
const type& As() const
{
return *reinterpret_cast<const type*>(this);
}
template<class type>
type& As()
{
return *reinterpret_cast<type*>(this);
}
// Unary negation operator
Vector2 operator-() const;
// Binary arithmetic operators
Vector2 operator+(const Vector2& v) const;
Vector2 operator-(const Vector2& v) const;
Vector2 operator*(float fl) const;
Vector2 operator/(float fl) const;
// Normalize the vector
[[nodiscard]] Vector2 Normalized() const;
// Sum of elements
[[nodiscard]] float Sum() const;
[[nodiscard]]
std::tuple<float, float> AsTuple() const;
};
}

View File

@@ -6,20 +6,16 @@
#include <cstdint>
#include <functional>
#include "omath/Vector2.h"
namespace omath
{
class Vector3 {
class Vector3 : public Vector2
{
public:
float x = 0.f;
float y = 0.f;
float z = 0.f;
Vector3(const float x, const float y, const float z)
{
this->x = x;
this->y = y;
this->z = z;
}
Vector3(float x, float y, float z);
Vector3() = default;
bool operator==(const Vector3& src) const;
@@ -50,16 +46,6 @@ namespace omath
Vector3 operator/(float fl) const;
Vector3 operator/(const Vector3& v) const;
template<class type>
const type& As() const
{
return *reinterpret_cast<const type*>(this);
}
template<class type>
type& As()
{
return *reinterpret_cast<type*>(this);
}
[[nodiscard]] Vector3 Cross(const Vector3 &v) const;
[[nodiscard]] static Vector3 CreateVelocity(float pitch, float yaw, float speed);
@@ -74,6 +60,8 @@ namespace omath
[[nodiscard]]
Vector3 Normalized() const;
[[nodiscard]] std::tuple<float, float, float> AsTuple() const;
};
}
// ReSharper disable once CppRedundantNamespaceDefinition

View File

@@ -2,7 +2,8 @@ target_sources(omath PRIVATE
Vector3.cpp
matrix.cpp
color.cpp
Vector4.cpp)
Vector4.cpp
Vector2.cpp)
add_subdirectory(prediction)
add_subdirectory(pathfinding)

163
source/Vector2.cpp Normal file
View File

@@ -0,0 +1,163 @@
//
// Created by Vlad on 02.09.2024.
//
#include "omath/Vector2.h"
#include <cmath>
namespace omath
{
// Constructors
Vector2::Vector2(const float x, const float y) : x(x), y(y) {}
// Equality operators
bool Vector2::operator==(const Vector2& src) const
{
return x == src.x && y == src.y;
}
bool Vector2::operator!=(const Vector2& src) const
{
return !(*this == src);
}
// Compound assignment operators
Vector2& Vector2::operator+=(const Vector2& v)
{
x += v.x;
y += v.y;
return *this;
}
Vector2& Vector2::operator-=(const Vector2& v)
{
x -= v.x;
y -= v.y;
return *this;
}
Vector2& Vector2::operator*=(float fl)
{
x *= fl;
y *= fl;
return *this;
}
Vector2& Vector2::operator/=(float fl)
{
x /= fl;
y /= fl;
return *this;
}
Vector2& Vector2::operator+=(float fl)
{
x += fl;
y += fl;
return *this;
}
Vector2& Vector2::operator-=(float fl)
{
x -= fl;
y -= fl;
return *this;
}
// Basic vector operations
float Vector2::DistTo(const Vector2& vOther) const
{
return std::sqrt(DistToSqr(vOther));
}
float Vector2::DistToSqr(const Vector2& vOther) const
{
return (x - vOther.x) * (x - vOther.x) + (y - vOther.y) * (y - vOther.y);
}
float Vector2::Dot(const Vector2& vOther) const
{
return x * vOther.x + y * vOther.y;
}
float Vector2::Length() const
{
return std::sqrt(x * x + y * y);
}
float Vector2::LengthSqr() const
{
return x * x + y * y;
}
Vector2& Vector2::Abs()
{
x = std::abs(x);
y = std::abs(y);
return *this;
}
// Unary negation operator
Vector2 Vector2::operator-() const
{
return {-x, -y};
}
// Binary arithmetic operators
Vector2 Vector2::operator+(const Vector2& v) const
{
return {x + v.x, y + v.y};
}
Vector2 Vector2::operator-(const Vector2& v) const
{
return {x - v.x, y - v.y};
}
Vector2 Vector2::operator*(float fl) const
{
return {x * fl, y * fl};
}
Vector2 Vector2::operator/(float fl) const
{
return {x / fl, y / fl};
}
// Normalize the vector
Vector2 Vector2::Normalized() const
{
float len = Length();
if (len > 0.f) {
return {x / len, y / len};
}
return {0.f, 0.f};
}
// Sum of elements
float Vector2::Sum() const
{
return x + y;
}
Vector2 &Vector2::operator*=(const Vector2 &v)
{
x *= v.x;
y *= v.y;
return *this;
}
Vector2 &Vector2::operator/=(const Vector2 &v)
{
x /= v.x;
y /= v.y;
return *this;
}
std::tuple<float, float> Vector2::AsTuple() const
{
return std::make_tuple(x, y);
}
}

View File

@@ -8,20 +8,25 @@
namespace omath
{
Vector3::Vector3(const float x, const float y, const float z) : Vector2(x, y), z(z)
{
}
bool Vector3::operator==(const Vector3 &src) const
{
return (src.x == x) and (src.y == y) and (src.z == z);
return Vector2::operator==(src) && (src.z == z);
}
bool Vector3::operator!=(const Vector3 &src) const
{
return (src.x != x) or (src.y != y) or (src.z != z);
return !(*this == src);
}
Vector3 &Vector3::operator+=(const Vector3 &v)
{
x += v.x;
y += v.y;
Vector2::operator+=(v);
z += v.z;
return *this;
@@ -29,8 +34,7 @@ namespace omath
Vector3 &Vector3::operator-=(const Vector3 &v)
{
x -= v.x;
y -= v.y;
Vector2::operator-=(v);
z -= v.z;
return *this;
@@ -38,8 +42,7 @@ namespace omath
Vector3 &Vector3::operator*=(const float fl)
{
x *= fl;
y *= fl;
Vector2::operator*=(fl);
z *= fl;
return *this;
@@ -47,8 +50,7 @@ namespace omath
Vector3 &Vector3::operator*=(const Vector3 &v)
{
x *= v.x;
y *= v.y;
Vector2::operator*=(v);
z *= v.z;
return *this;
@@ -56,8 +58,7 @@ namespace omath
Vector3 &Vector3::operator/=(const Vector3 &v)
{
x /= v.x;
y /= v.y;
Vector2::operator/=(v);
z /= v.z;
return *this;
@@ -65,8 +66,7 @@ namespace omath
Vector3 &Vector3::operator+=(const float fl)
{
x += fl;
y += fl;
Vector2::operator+=(fl);
z += fl;
return *this;
@@ -74,8 +74,7 @@ namespace omath
Vector3 &Vector3::operator/=(const float fl)
{
x /= fl;
y /= fl;
Vector2::operator/=(fl);
z /= fl;
return *this;
@@ -83,8 +82,7 @@ namespace omath
Vector3 &Vector3::operator-=(const float fl)
{
x -= fl;
y -= fl;
Vector2::operator-=(fl);
z -= fl;
return *this;
@@ -92,19 +90,12 @@ namespace omath
float Vector3::DistTo(const Vector3 &vOther) const
{
Vector3 delta;
delta.x = x - vOther.x;
delta.y = y - vOther.y;
delta.z = z - vOther.z;
return delta.Length();
return (*this - vOther).Length();
}
Vector3 &Vector3::Abs()
{
x = std::abs(x);
y = std::abs(y);
Vector2::Abs();
z = std::abs(z);
return *this;
@@ -112,34 +103,27 @@ namespace omath
float Vector3::DistToSqr(const Vector3 &vOther) const
{
Vector3 delta;
delta.x = x - vOther.x;
delta.y = y - vOther.y;
delta.z = z - vOther.z;
return delta.LengthSqr();
return (*this - vOther).LengthSqr();
}
float Vector3::Dot(const Vector3 &vOther) const
{
return (x * vOther.x + y * vOther.y + z * vOther.z);
return Vector2::Dot(vOther) + z * vOther.z;
}
float Vector3::Length() const
{
return std::sqrt(x * x + y * y + z * z);
return std::sqrt(Vector2::LengthSqr() + z * z);
}
float Vector3::LengthSqr() const
{
return (x * x + y * y + z * z);
return Vector2::LengthSqr() + z * z;
}
float Vector3::Length2D() const
{
return std::sqrt(x * x + y * y);
return Vector2::Length();
}
Vector3 Vector3::operator-() const
@@ -189,12 +173,12 @@ namespace omath
float Vector3::Sum() const
{
return x + y + z;
return Vector3::Sum2D() + z;
}
float Vector3::Sum2D() const
{
return x + y;
return Vector2::Sum();
}
Vector3 Vector3::ViewAngleTo(const Vector3 &other) const
@@ -265,4 +249,9 @@ namespace omath
return length != 0 ? *this / length : *this;
}
std::tuple<float, float, float> Vector3::AsTuple() const
{
return std::make_tuple(x, y, z);
}
}

View File

@@ -7,7 +7,7 @@ TEST(UnitTestPrediction, PredictionTest)
const omath::prediction::Projectile proj = {.m_origin = {3,2,1}, .m_launchSpeed = 5000, .m_gravityScale= 0.4};
const auto viewPoint = omath::prediction::Engine(400, 1.f / 1000.f, 50, 5.f).MaybeCalculateAimPoint(proj, target);
const auto [pitch, yaw, _] = proj.m_origin.ViewAngleTo(viewPoint.value());
const auto [pitch, yaw, _] = proj.m_origin.ViewAngleTo(viewPoint.value()).AsTuple();
EXPECT_NEAR(42.547142, pitch, 0.0001f);
EXPECT_NEAR(-1.181189, yaw, 0.0001f);