From adcf6927588026d672fb0f0d09e0faee6be2617a Mon Sep 17 00:00:00 2001 From: Miran Date: Mon, 6 Apr 2026 13:55:29 +0200 Subject: [PATCH 1/4] Corrected "normaliSe" to "normaliZe". Added Normalized method. --- examples/UnitTests/source/Test_CVector.h | 30 ++++++-- shared/game/CVector.h | 89 ++++++++++++------------ shared/game/CVectorImplementation.h | 12 +++- 3 files changed, 77 insertions(+), 54 deletions(-) diff --git a/examples/UnitTests/source/Test_CVector.h b/examples/UnitTests/source/Test_CVector.h index a608ecd5..efc77525 100644 --- a/examples/UnitTests/source/Test_CVector.h +++ b/examples/UnitTests/source/Test_CVector.h @@ -449,6 +449,22 @@ UTEST(CVector, MagnitudeSqr2D) EXPECT_NEAR(5.0f, m, F_EPS); } +UTEST(CVector, Normalized) +{ + CVector v; + + v.Set(0.1f, 0.0f, 0.0f); + CVector b = v.Normalized(); + + EXPECT_NEAR(0.1f, v.x, F_EPS); + EXPECT_NEAR(0.0f, v.y, F_EPS); + EXPECT_NEAR(0.0f, v.z, F_EPS); + + EXPECT_NEAR(1.0f, b.x, F_EPS); + EXPECT_NEAR(0.0f, b.y, F_EPS); + EXPECT_NEAR(0.0f, b.z, F_EPS); +} + UTEST(CVector, IsNormalized) { CVector v; @@ -550,45 +566,45 @@ UTEST(CVector, Normalize) CVector v; v.Set(0.1f, 0.0f, 0.0f); - v.Normalise(); + v.Normalize(); EXPECT_NEAR(1.0f, v.x, F_EPS); EXPECT_NEAR(0.0f, v.y, F_EPS); EXPECT_NEAR(0.0f, v.z, F_EPS); v.Set(0.0f, 2.0f, 0.0f); - v.Normalise(); + v.Normalize(); EXPECT_NEAR(0.0f, v.x, F_EPS); EXPECT_NEAR(1.0f, v.y, F_EPS); EXPECT_NEAR(0.0f, v.z, F_EPS); v.Set(1.0f, 2.0f, 3.0f); - v.Normalise(); + v.Normalize(); EXPECT_NEAR(0.267261f, v.x, F_EPS); EXPECT_NEAR(0.534522f, v.y, F_EPS); EXPECT_NEAR(0.801784f, v.z, F_EPS); } -UTEST(CVector, NormaliseAndMag) +UTEST(CVector, NormalizeAndMag) { CVector v; float m; v.Set(0.1f, 0.0f, 0.0f); - m = v.NormaliseAndMag(); + m = v.NormalizeAndMag(); EXPECT_NEAR(0.1f, m, F_EPS); EXPECT_NEAR(1.0f, v.x, F_EPS); EXPECT_NEAR(0.0f, v.y, F_EPS); EXPECT_NEAR(0.0f, v.z, F_EPS); v.Set(0.0f, 2.0f, 0.0f); - m = v.NormaliseAndMag(); + m = v.NormalizeAndMag(); EXPECT_NEAR(2.0f, m, F_EPS); EXPECT_NEAR(0.0f, v.x, F_EPS); EXPECT_NEAR(1.0f, v.y, F_EPS); EXPECT_NEAR(0.0f, v.z, F_EPS); v.Set(1.0f, 2.0f, 3.0f); - m = v.NormaliseAndMag(); + m = v.NormalizeAndMag(); EXPECT_NEAR(3.741657f, m, F_EPS); EXPECT_NEAR(0.267261f, v.x, F_EPS); EXPECT_NEAR(0.534522f, v.y, F_EPS); diff --git a/shared/game/CVector.h b/shared/game/CVector.h index 9db66fb3..3e6e7d88 100644 --- a/shared/game/CVector.h +++ b/shared/game/CVector.h @@ -42,7 +42,7 @@ struct CVector : public RwV3d void operator =(const CVector& src); void From2D(const CVector2D& xy, float z = 0.0f); void FromSum(const CVector& left, const CVector& right); // store sum of two vectors - void FromDiff(const CVector& left, const CVector& right); // store left - right substraction result + void FromDiff(const CVector& left, const CVector& right); // store left - right subtraction result void FromLerp(const CVector& begin, const CVector& end, float progress); // store result of linear interpolation between points void FromCross(const CVector& left, const CVector& right); // store result of cross product #ifdef HAS_CMATRIX @@ -51,68 +51,69 @@ struct CVector : public RwV3d #endif // conversions - operator RwV3d&(); - operator const RwV3d&() const; - operator RwV3d*(); - operator const RwV3d*() const; - CVector2D To2D() const; // get XY + [[nodiscard]] operator RwV3d&(); + [[nodiscard]] operator const RwV3d&() const; + [[nodiscard]] operator RwV3d*(); + [[nodiscard]] operator const RwV3d*() const; + [[nodiscard]] CVector2D To2D() const; // get XY // properties - bool operator ==(const CVector& other) const; - bool operator !=(const CVector& other) const; - CVector operator -() const; // opposite vector - float Distance(const CVector& other) const; // distance to other - float Distance2D(const CVector& other) const; // XY distance to other - float Distance2D(const CVector2D& other) const; // XY distance to other - float Dot(const CVector& other) const; // dot product - CVector Cross(const CVector& other) const; // cross product - float Heading() const; // direction of XY vec in radians - float Magnitude() const; // length - float MagnitudeSqr() const; // length^2 - float Magnitude2D() const; // XY vec length - float MagnitudeSqr2D() const; // XY vec length^2 - bool IsNormalized() const; // length is 1.0 +/- 0.001 - bool IsZero() const; // all components are 0.0 + [[nodiscard]] bool operator ==(const CVector& other) const; + [[nodiscard]] bool operator !=(const CVector& other) const; + [[nodiscard]] CVector operator -() const; // opposite vector + [[nodiscard]] float Distance(const CVector& other) const; // distance to other + [[nodiscard]] float Distance2D(const CVector& other) const; // XY distance to other + [[nodiscard]] float Distance2D(const CVector2D& other) const; // XY distance to other + [[nodiscard]] float Dot(const CVector& other) const; // dot product + [[nodiscard]] CVector Cross(const CVector& other) const; // cross product + [[nodiscard]] float Heading() const; // direction of XY vec in radians + [[nodiscard]] float Magnitude() const; // length + [[nodiscard]] float MagnitudeSqr() const; // length^2 + [[nodiscard]] float Magnitude2D() const; // XY vec length + [[nodiscard]] float MagnitudeSqr2D() const; // XY vec length^2 + [[nodiscard]] CVector Normalized() const; // this vector scaled to 1.0 length + [[nodiscard]] bool IsNormalized() const; // length is 1.0 +/- 0.001 + [[nodiscard]] bool IsZero() const; // all components are 0.0 // modifiers void operator +=(float value); // add to all components void operator +=(const CVector& other); - void operator -=(float value); // substract from all components + void operator -=(float value); // subtract from all components void operator -=(const CVector& other); void operator *=(float multiplier); // multiply all components void operator /=(float divisor); // divide all components - void Normalise(); // scale to 1.0 length - float NormaliseAndMag(); // normalize and return previous length + void Normalize(); // scale to 1.0 length + float NormalizeAndMag(); // normalize and return previous length // static functions - static CVector Sum(const CVector& left, const CVector& right); // result of left + right - static CVector Diff(const CVector& left, const CVector& right); // result of left - right - static float Distance(const CVector& left, const CVector& right); // distance between points - static float Distance2D(const CVector& left, const CVector& right); // XY distance between points - static float Distance2D(const CVector& left, const CVector2D& right); // XY distance between points - static CVector Lerp(const CVector& begin, const CVector& end, float progress); // result of linear interpolation between points - static float Dot(const CVector& left, const CVector& right); // result of dot product - static CVector Cross(const CVector& left, const CVector& right); // result of cross product + [[nodiscard]] static CVector Sum(const CVector& left, const CVector& right); // result of left + right + [[nodiscard]] static CVector Diff(const CVector& left, const CVector& right); // result of left - right + [[nodiscard]] static float Distance(const CVector& left, const CVector& right); // distance between points + [[nodiscard]] static float Distance2D(const CVector& left, const CVector& right); // XY distance between points + [[nodiscard]] static float Distance2D(const CVector& left, const CVector2D& right); // XY distance between points + [[nodiscard]] static CVector Lerp(const CVector& begin, const CVector& end, float progress); // result of linear interpolation between points + [[nodiscard]] static float Dot(const CVector& left, const CVector& right); // result of dot product + [[nodiscard]] static CVector Cross(const CVector& left, const CVector& right); // result of cross product #ifdef HAS_CMATRIX - static CVector Multiply(const CMatrix& matrix, const CVector& point); // result of matrix and point multiplication - static CVector Multiply3x3(const CMatrix& matrix, const CVector& vector); // result of matrix and vector multiplication + [[nodiscard]] static CVector Multiply(const CMatrix& matrix, const CVector& point); // result of matrix and point multiplication + [[nodiscard]] static CVector Multiply3x3(const CMatrix& matrix, const CVector& vector); // result of matrix and vector multiplication #endif }; VALIDATE_SIZE(CVector, 0xC); // static operators -CVector operator +(float value, const CVector& vec); -CVector operator +(const CVector& vec, float value); -CVector operator +(const CVector& left, const CVector& right); +[[nodiscard]] CVector operator +(float value, const CVector& vec); +[[nodiscard]] CVector operator +(const CVector& vec, float value); +[[nodiscard]] CVector operator +(const CVector& left, const CVector& right); -CVector operator -(float value, const CVector& vec); -CVector operator -(const CVector& vec, float value); -CVector operator -(const CVector& left, const CVector& right); +[[nodiscard]] CVector operator -(float value, const CVector& vec); +[[nodiscard]] CVector operator -(const CVector& vec, float value); +[[nodiscard]] CVector operator -(const CVector& left, const CVector& right); -CVector operator *(float value, const CVector& vec); -CVector operator *(const CVector& vec, float value); +[[nodiscard]] CVector operator *(float value, const CVector& vec); +[[nodiscard]] CVector operator *(const CVector& vec, float value); -CVector operator /(float value, const CVector& vec); -CVector operator /(const CVector& vec, float value); +[[nodiscard]] CVector operator /(float value, const CVector& vec); +[[nodiscard]] CVector operator /(const CVector& vec, float value); #include "CVectorImplementation.h" // inlined functions diff --git a/shared/game/CVectorImplementation.h b/shared/game/CVectorImplementation.h index 01dc3563..1f407acd 100644 --- a/shared/game/CVectorImplementation.h +++ b/shared/game/CVectorImplementation.h @@ -161,6 +161,12 @@ inline float CVector::MagnitudeSqr2D() const { return x * x + y * y; } +inline CVector CVector::Normalized() const { + auto result = *this; + result.Normalize(); + return result; +} + inline bool CVector::IsNormalized() const { return fabs(MagnitudeSqr() - 1.0f) < 0.001f; } @@ -207,11 +213,11 @@ inline void CVector::operator /=(float divisor) { z /= divisor; } -inline void CVector::Normalise() { - NormaliseAndMag(); +inline void CVector::Normalize() { + NormalizeAndMag(); } -inline float CVector::NormaliseAndMag() { +inline float CVector::NormalizeAndMag() { auto length = Magnitude(); if (length > 0.0f) *this /= length; return length; From a3391a351f73a14d4ab2b2908a7f77091d9fa92a Mon Sep 17 00:00:00 2001 From: Miran Date: Mon, 6 Apr 2026 15:27:51 +0200 Subject: [PATCH 2/4] Moving CVector2D to shared --- plugin_II/game_II/CVector2D.cpp | 99 ------ plugin_II/game_II/CVector2D.h | 71 ----- plugin_III/game_III/CVector2D.cpp | 21 -- plugin_III/game_III/CVector2D.h | 170 ----------- plugin_IV/game_IV/CVector2D.h | 83 ------ .../game_iii_unreal/CVector2D.cpp | 69 ----- plugin_iii_unreal/game_iii_unreal/CVector2D.h | 42 --- plugin_sa/game_sa/CPlaceable.h | 1 - plugin_sa/game_sa/CVector2D.cpp | 35 --- plugin_sa/game_sa/CVector2D.h | 156 ---------- plugin_sa_unreal/game_sa_unreal/CVector2D.cpp | 69 ----- plugin_sa_unreal/game_sa_unreal/CVector2D.h | 42 --- plugin_vc/game_vc/CVector2D.cpp | 21 -- plugin_vc/game_vc/CVector2D.h | 162 ---------- plugin_vc_unreal/game_vc_unreal/CVector2D.cpp | 69 ----- plugin_vc_unreal/game_vc_unreal/CVector2D.h | 42 --- shared/game/CVector.h | 2 +- shared/game/CVector2D.h | 110 +++++++ shared/game/CVector2DImplementation.h | 282 ++++++++++++++++++ shared/game/CompressedVector2D.h | 2 +- tools/premake/premake5.lua | 2 - tools/premake/stdafx_template.h | 10 - 22 files changed, 394 insertions(+), 1166 deletions(-) delete mode 100644 plugin_II/game_II/CVector2D.cpp delete mode 100644 plugin_II/game_II/CVector2D.h delete mode 100644 plugin_III/game_III/CVector2D.cpp delete mode 100644 plugin_III/game_III/CVector2D.h delete mode 100644 plugin_IV/game_IV/CVector2D.h delete mode 100644 plugin_iii_unreal/game_iii_unreal/CVector2D.cpp delete mode 100644 plugin_iii_unreal/game_iii_unreal/CVector2D.h delete mode 100644 plugin_sa/game_sa/CVector2D.cpp delete mode 100644 plugin_sa/game_sa/CVector2D.h delete mode 100644 plugin_sa_unreal/game_sa_unreal/CVector2D.cpp delete mode 100644 plugin_sa_unreal/game_sa_unreal/CVector2D.h delete mode 100644 plugin_vc/game_vc/CVector2D.cpp delete mode 100644 plugin_vc/game_vc/CVector2D.h delete mode 100644 plugin_vc_unreal/game_vc_unreal/CVector2D.cpp delete mode 100644 plugin_vc_unreal/game_vc_unreal/CVector2D.h create mode 100644 shared/game/CVector2D.h create mode 100644 shared/game/CVector2DImplementation.h diff --git a/plugin_II/game_II/CVector2D.cpp b/plugin_II/game_II/CVector2D.cpp deleted file mode 100644 index 3eb96bd2..00000000 --- a/plugin_II/game_II/CVector2D.cpp +++ /dev/null @@ -1,99 +0,0 @@ -/* - Plugin-SDK (Grand Theft Auto 2) source file - Authors: GTA Community. See more here - https://github.com/DK22Pac/plugin-sdk - Do not delete this comment block. Respect others' work! -*/ - -#include "CVector2D.h" -#include - -CEncodedVector::CEncodedVector() { - x = 0; y = 0; z = 0; -} - -CEncodedVector::CEncodedVector(int X, int Y, int Z) { - x = X; y = Y; z = Z; -} - -CVector CEncodedVector::FromInt16() { - return CVector((this->x / 16384.0f), (this->y / 16384.0f), (this->z / 16384.0f)); -} - -CEncodedVector2D::CEncodedVector2D() { - x = 0; y = 0; -} - -CEncodedVector2D::CEncodedVector2D(int X, int Y) { - x = X; y = Y; -} - -CVector2D CEncodedVector2D::FromInt16() { - return CVector2D((this->x / 16384.0f), (this->y / 16384.0f)); -} - -CVector2D::CVector2D() { - x = 0.0f; y = 0.0f; -} - -CVector2D::CVector2D(float X, float Y) { - x = X; y = Y; -} - -CVector2D::CVector2D(CVector2D const& src) { - x = src.x; y = src.y; -} - -float CVector2D::Magnitude() { - return sqrtf(this->x * this->x + this->y * this->y); -} - -float CVector2D::MagnitudeSqr() { - return x * x + y * y; -} - -void CVector2D::Sum(CVector2D& a, CVector2D& b) { - this->x = a.x + b.x; - this->y = a.y + b.y; -} - -void CVector2D::Difference(CVector2D& a, CVector2D& b) { - this->x = a.x - b.x; - this->y = a.y - b.y; -} - -void CVector2D::Normalise() { - float sq = MagnitudeSqr(); - float invsqrt = 1.0f / sqrt(sq); - x *= invsqrt; - y *= invsqrt; -} - -void CVector2D::operator=(const CVector2D& right) { - this->x = right.x; - this->y = right.y; -} - -void CVector2D::operator+=(const CVector2D& right) { - this->x += right.x; - this->y += right.y; -} - -void CVector2D::operator-=(const CVector2D& right) { - this->x -= right.x; - this->y -= right.y; -} - -void CVector2D::operator*=(float multiplier) { - this->x *= multiplier; - this->y *= multiplier; -} - -void CVector2D::operator/=(float divisor) { - this->x /= divisor; - this->y /= divisor; -} - -CEncodedVector2D CVector2D::ToInt16() { - return CEncodedVector2D((int)(this->x * 16384), (int)(this->y * 16384)); -} diff --git a/plugin_II/game_II/CVector2D.h b/plugin_II/game_II/CVector2D.h deleted file mode 100644 index 9a384efd..00000000 --- a/plugin_II/game_II/CVector2D.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - Plugin-SDK (Grand Theft Auto 2) header file - Authors: GTA Community. See more here - https://github.com/DK22Pac/plugin-sdk - Do not delete this comment block. Respect others' work! -*/ - -#pragma once -#include "PluginBase.h" - -struct CVector; -class CVector2D; - -class CEncodedVector { -public: - int x, y, z; - -public: - CEncodedVector(); - CEncodedVector(int X, int Y, int Z); - CVector FromInt16(); -}; - -class CEncodedVector2D { -public: - int x, y; - -public: - CEncodedVector2D(); - CEncodedVector2D(int X, int Y); - CVector2D FromInt16(); -}; - -class CVector2D { -public: - float x, y; - -public: - CVector2D(); - CVector2D(float X, float Y); - CVector2D(CVector2D const& src); - float Magnitude(); - float MagnitudeSqr(); - void Sum(CVector2D& a, CVector2D& b); - void Difference(CVector2D& a, CVector2D& b); - void Normalise(); - void operator=(const CVector2D& right); - void operator+=(const CVector2D& right); - void operator-=(const CVector2D& right); - void operator*=(float multiplier); - void operator/=(float divisor); - CEncodedVector2D ToInt16(); - - CVector2D operator-(const CVector2D& rhs) const { - return CVector2D(x - rhs.x, y - rhs.y); - } - CVector2D operator+(const CVector2D& rhs) const { - return CVector2D(x + rhs.x, y + rhs.y); - } - CVector2D operator/(float t) const { - return CVector2D(x / t, y / t); - } -}; - -inline CVector2D operator*(const CVector2D& left, float right) { - return CVector2D(left.x * right, left.y * right); -} - -inline CVector2D operator-(const CVector2D& left, const CVector2D& right) { - return CVector2D(left.x - right.x, left.y - right.y); -} diff --git a/plugin_III/game_III/CVector2D.cpp b/plugin_III/game_III/CVector2D.cpp deleted file mode 100644 index be51849a..00000000 --- a/plugin_III/game_III/CVector2D.cpp +++ /dev/null @@ -1,21 +0,0 @@ -/* - Plugin-SDK (Grand Theft Auto 3) source file - Authors: GTA Community. See more here - https://github.com/DK22Pac/plugin-sdk - Do not delete this comment block. Respect others' work! -*/ -#include "CVector2D.h" -#include "CVector.h" - -CVector CVector2D::To3D(float zValue) const { - return CVector(x, y, zValue); -} - -void CVector2D::From3D(const CVector& vec3d) { - x = vec3d.x; - y = vec3d.y; -} - -CVector2D::CVector2D(CVector const& src) { - x = src.x; y = src.y; -} \ No newline at end of file diff --git a/plugin_III/game_III/CVector2D.h b/plugin_III/game_III/CVector2D.h deleted file mode 100644 index ec152970..00000000 --- a/plugin_III/game_III/CVector2D.h +++ /dev/null @@ -1,170 +0,0 @@ -/* - Plugin-SDK (Grand Theft Auto 3) header file - Authors: GTA Community. See more here - https://github.com/DK22Pac/plugin-sdk - Do not delete this comment block. Respect others' work! -*/ -#pragma once - -#include "PluginBase.h" -#include - -struct CVector; - -class CVector2D { -public: - float x, y; - - inline CVector2D() { - x = 0.0f; y = 0.0f; - } - - inline CVector2D(float X, float Y) { - x = X; y = Y; - } - - inline CVector2D(CVector2D const& src) { - x = src.x; y = src.y; - } - - CVector2D(CVector const& src); - CVector To3D(float zValue = 0.0f) const; - void From3D(const CVector& vec3d); - - inline float Magnitude() const { - return sqrtf(x * x + y * y); - } - - inline float MagnitudeSqr() const { - return x * x + y * y; - } - - inline void Sum(CVector2D &a, CVector2D &b) { - x = a.x + b.x; - y = a.y + b.y; - } - - inline void Difference(CVector2D &a, CVector2D &b) { - x = a.x - b.x; - y = a.y - b.y; - } - - inline void operator=(const CVector2D& right) { - x = right.x; - y = right.y; - } - - inline CVector2D operator-() { - return CVector2D(-x, -y); - } - - inline void operator+=(const CVector2D& right) { - x += right.x; - y += right.y; - } - - inline void operator-=(const CVector2D& right) { - x -= right.x; - y -= right.y; - } - - inline void operator *= (float multiplier) { - x *= multiplier; - y *= multiplier; - } - - inline void operator*=(const CVector2D& right) { - x *= right.x; - y *= right.y; - } - - inline void operator /= (float divisor) { - x /= divisor; - y /= divisor; - } - - inline bool operator!=(const CVector2D& right) { - return x != right.x || y != right.y; - } - - inline bool operator==(const CVector2D& right) { - return x == right.x && y == right.y; - } - - inline void Normalise() { - float sq = x * x + y * y; - float invsqrt = 1.0f / sqrt(sq); - x *= invsqrt; - y *= invsqrt; - } - - inline float NormaliseAndMag() { - float magSqr = MagnitudeSqr(); - if (magSqr > 0.0f) { - float mag = std::sqrt(magSqr); - float invMag = 1.0f / mag; - x *= invMag; - y *= invMag; - return mag; - } - return 0.0f; - } - - inline void Zero() { - x = 0.0f; - y = 0.0f; - } - - inline bool IsZero() const { - return x == 0.0f && y == 0.0f; - } - - inline bool IsNormalized() { - return std::fabs(MagnitudeSqr() - 1.0f) < 0.001f; - } - - inline float Heading() { - return std::atan2(-x, y); - } -}; - -inline CVector2D operator-(const CVector2D& right) { - return CVector2D(-right.x, -right.y); -} - -inline CVector2D operator/(const CVector2D& vec, float div) { - return CVector2D(vec.x / div, vec.y / div); -} - -inline CVector2D operator-(const CVector2D& vecOne, const CVector2D& vecTwo) { - return CVector2D(vecOne.x - vecTwo.x, vecOne.y - vecTwo.y); -} - -inline CVector2D operator+(const CVector2D& vecOne, const CVector2D& vecTwo) { - return CVector2D(vecOne.x + vecTwo.x, vecOne.y + vecTwo.y); -} - -inline CVector2D operator*(const CVector2D& vec, float multiplier) { - return CVector2D(vec.x * multiplier, vec.y * multiplier); -} - -inline CVector2D operator*(const CVector2D& vecOne, const CVector2D& vecTwo) { - return CVector2D(vecOne.x * vecTwo.x, vecOne.y * vecTwo.y); -} - -inline CVector2D operator*(float multiplier, const CVector2D& vec) { - return CVector2D(vec.x * multiplier, vec.y * multiplier); -} - -inline float DistanceBetweenPoints(const CVector2D& pointOne, const CVector2D& pointTwo) { - CVector2D diff = pointTwo - pointOne; - return diff.Magnitude(); -} - -inline float DotProduct(const CVector2D& a, const CVector2D& b) { - return a.x * b.x + a.y * b.y; -} - -inline float CrossProduct(const CVector2D& a, const CVector2D& b) { - return a.x * b.y - a.y * b.x; -} \ No newline at end of file diff --git a/plugin_IV/game_IV/CVector2D.h b/plugin_IV/game_IV/CVector2D.h deleted file mode 100644 index 1e7b6456..00000000 --- a/plugin_IV/game_IV/CVector2D.h +++ /dev/null @@ -1,83 +0,0 @@ -/* - Plugin-SDK (Grand Theft Auto IV) header file - Authors: GTA Community. See more here - https://github.com/DK22Pac/plugin-sdk - Do not delete this comment block. Respect others' work! -*/ -#pragma once -#include "PluginBase.h" - -class CVector2D { -public: - float x, y; - -public: - CVector2D() = default; - CVector2D(float x, float y) : x(x), y(y) {} - - CVector2D operator+(const CVector2D& other) const { - return CVector2D(x + other.x, y + other.y); - } - - CVector2D operator-(const CVector2D& other) const { - return CVector2D(x - other.x, y - other.y); - } - - CVector2D operator*(float scalar) const { - return CVector2D(x * scalar, y * scalar); - } - - CVector2D operator/(float scalar) const { - if (scalar != 0.0f) - return CVector2D(x / scalar, y / scalar); - else - throw std::runtime_error("Division by zero"); - } - - CVector2D& operator=(const CVector2D& other) { - if (this != &other) { - x = other.x; - y = other.y; - } - return *this; - } - - CVector2D& operator+=(const CVector2D& other) { - x += other.x; - y += other.y; - return *this; - } - - CVector2D& operator-=(const CVector2D& other) { - x -= other.x; - y -= other.y; - return *this; - } - - CVector2D& operator*=(float scalar) { - x *= scalar; - y *= scalar; - return *this; - } - - bool operator==(const CVector2D& other) const { - return x == other.x && y == other.y; - } - - bool operator!=(const CVector2D& other) const { - return !(*this == other); - } - - float Heading() const { - return atan2f(-x, y); - } - - float Magnitude() { - return sqrt(x * x + y * y); - } - - void Translate(float x, float y) { - this->x += x; - this->y += y; - } -}; diff --git a/plugin_iii_unreal/game_iii_unreal/CVector2D.cpp b/plugin_iii_unreal/game_iii_unreal/CVector2D.cpp deleted file mode 100644 index 7ff2a018..00000000 --- a/plugin_iii_unreal/game_iii_unreal/CVector2D.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - Plugin-SDK (Grand Theft Auto 3 Unreal) source file - Authors: GTA Community. See more here - https://github.com/DK22Pac/plugin-sdk - Do not delete this comment block. Respect others' work! -*/ -#include "CVector2D.h" - -CVector2D::CVector2D() { - x = 0.0f; y = 0.0f; -} - -CVector2D::CVector2D(float X, float Y) { - x = X; y = Y; -} - -CVector2D::CVector2D(CVector2D const& src) { - x = src.x; y = src.y; -} - -float CVector2D::Magnitude() { - return sqrtf(this->x * this->x + this->y * this->y); -} - -float CVector2D::MagnitudeSqr() { - return x * x + y * y; -} - -void CVector2D::Sum(CVector2D& a, CVector2D& b) { - this->x = a.x + b.x; - this->y = a.y + b.y; -} - -void CVector2D::Difference(CVector2D& a, CVector2D& b) { - this->x = a.x - b.x; - this->y = a.y - b.y; -} - -void CVector2D::Normalise() { - float sq = MagnitudeSqr(); - float invsqrt = 1.0f / sqrt(sq); - x *= invsqrt; - y *= invsqrt; -} - -void CVector2D::operator=(const CVector2D& right) { - this->x = right.x; - this->y = right.y; -} - -void CVector2D::operator+=(const CVector2D& right) { - this->x += right.x; - this->y += right.y; -} - -void CVector2D::operator-=(const CVector2D& right) { - this->x -= right.x; - this->y -= right.y; -} - -void CVector2D::operator*=(float multiplier) { - this->x *= multiplier; - this->y *= multiplier; -} - -void CVector2D::operator/=(float divisor) { - this->x /= divisor; - this->y /= divisor; -} diff --git a/plugin_iii_unreal/game_iii_unreal/CVector2D.h b/plugin_iii_unreal/game_iii_unreal/CVector2D.h deleted file mode 100644 index df1565ea..00000000 --- a/plugin_iii_unreal/game_iii_unreal/CVector2D.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - Plugin-SDK (Grand Theft Auto 3 Unreal) header file - Authors: GTA Community. See more here - https://github.com/DK22Pac/plugin-sdk - Do not delete this comment block. Respect others' work! -*/ -#pragma once -#include "PluginBase.h" - -class CVector2D { -public: - float x, y; - -public: - CVector2D(); - CVector2D(float X, float Y); - CVector2D(CVector2D const& src); - float Magnitude(); - float MagnitudeSqr(); - void Sum(CVector2D& a, CVector2D& b); - void Difference(CVector2D& a, CVector2D& b); - void Normalise(); - void operator=(const CVector2D& right); - void operator+=(const CVector2D& right); - void operator-=(const CVector2D& right); - void operator*=(float multiplier); - void operator/=(float divisor); - - CVector2D operator-(const CVector2D& rhs) const { - return CVector2D(x - rhs.x, y - rhs.y); - } - CVector2D operator+(const CVector2D& rhs) const { - return CVector2D(x + rhs.x, y + rhs.y); - } - CVector2D operator/(float t) const { - return CVector2D(x / t, y / t); - } -}; - -inline CVector2D operator*(const CVector2D& left, float right) { - return CVector2D(left.x * right, left.y * right); -} diff --git a/plugin_sa/game_sa/CPlaceable.h b/plugin_sa/game_sa/CPlaceable.h index a459cb7b..bd977413 100644 --- a/plugin_sa/game_sa/CPlaceable.h +++ b/plugin_sa/game_sa/CPlaceable.h @@ -48,7 +48,6 @@ class PLUGIN_API CPlaceable { inline CVector& GetUp() const { return m_matrix->GetUp(); } inline const CVector& GetPosition() const { return m_matrix ? m_matrix->GetPosition() : m_placement.m_vPosn; } inline CVector& GetPosition() { return m_matrix ? m_matrix->GetPosition() : m_placement.m_vPosn; } - inline CVector2D GetPosition2D() { return { GetPosition() }; } }; VALIDATE_SIZE(CPlaceable, 0x18); \ No newline at end of file diff --git a/plugin_sa/game_sa/CVector2D.cpp b/plugin_sa/game_sa/CVector2D.cpp deleted file mode 100644 index b35ed5d9..00000000 --- a/plugin_sa/game_sa/CVector2D.cpp +++ /dev/null @@ -1,35 +0,0 @@ -/* - Plugin-SDK (Grand Theft Auto San Andreas) source file - Authors: GTA Community. See more here - https://github.com/DK22Pac/plugin-sdk - Do not delete this comment block. Respect others' work! -*/ -#include "CVector2D.h" -#include "CVector.h" - -float CVector2D::Magnitude() -{ - return ((float (__thiscall *)(CVector2D *))0x420860)(this); -} - -void CVector2D::operator=(CVector2D const& right) -{ - ((void (__thiscall *)(CVector2D *, CVector2D const&))0x43E110)(this, right); -} - -CVector CVector2D::To3D(float zValue) -{ - return CVector(x, y, zValue); -} - -void CVector2D::From3D(const CVector& vec3d) -{ - x = vec3d.x; - y = vec3d.y; -} - -CVector2D::CVector2D(const CVector& vec3d) -{ - x = vec3d.x; - y = vec3d.y; -} \ No newline at end of file diff --git a/plugin_sa/game_sa/CVector2D.h b/plugin_sa/game_sa/CVector2D.h deleted file mode 100644 index 9fadaa24..00000000 --- a/plugin_sa/game_sa/CVector2D.h +++ /dev/null @@ -1,156 +0,0 @@ -/* - Plugin-SDK (Grand Theft Auto San Andreas) header file - Authors: GTA Community. See more here - https://github.com/DK22Pac/plugin-sdk - Do not delete this comment block. Respect others' work! -*/ -#pragma once - -#include "PluginBase.h" - -struct CVector; - -class PLUGIN_API CVector2D -{ -public: - float x, y; - - inline CVector2D() { - x = 0.0f; - y = 0.0f; - } - - inline CVector2D(float X, float Y) { - x = X; - y = Y; - } - - CVector2D(const CVector& vec3d); - CVector To3D(float zValue = 0.0f); - void From3D(const CVector& vec3d); - - // Returns length of vector - float Magnitude(); - float MagnitudeSqr() const { return x * x + y * y; } - - inline CVector2D(CVector2D const& src) { - x = src.x; y = src.y; - } - - inline void Sum(CVector2D &a, CVector2D &b) { - this->x = a.x + b.x; - this->y = a.y + b.y; - } - - inline void Difference(CVector2D &a, CVector2D &b) { - this->x = a.x - b.x; - this->y = a.y - b.y; - } - - inline CVector2D operator-() const { - return CVector2D(-x, -y); - } - - void operator=(const CVector2D& right); - - inline bool operator==(const CVector2D& other) { - return x == other.x && y == other.y; - } - - inline bool operator!=(const CVector2D& other) { - return x != other.x || y != other.y; - } - - inline void operator+=(const CVector2D& right) { - this->x += right.x; - this->y += right.y; - } - - inline void operator-=(const CVector2D& right) { - this->x -= right.x; - this->y -= right.y; - } - - inline void operator *= (float multiplier) { - this->x *= multiplier; - this->y *= multiplier; - } - - inline void operator /= (float divisor) { - this->x /= divisor; - this->y /= divisor; - } - - inline void Normalize() { - float mag = Magnitude(); - if (mag > 0.0f) { - x /= mag; - y /= mag; - } - } - - inline float NormaliseAndMag() { - float magSqr = MagnitudeSqr(); - if (magSqr > 0.0f) { - float mag = std::sqrt(magSqr); - float invMag = 1.0f / mag; - x *= invMag; - y *= invMag; - return mag; - } - return 0.0f; - } - - inline void Zero() { - x = 0.0f; - y = 0.0f; - } - - inline bool IsZero() const { - return x == 0.0f && y == 0.0f; - } - - inline bool IsNormalized() const { - return std::fabs(MagnitudeSqr() - 1.0f) < 0.001f; - } - - inline float Heading() { - return std::atan2(-x, y); - } -}; - -inline CVector2D operator-(const CVector2D& vecOne, const CVector2D& vecTwo) { - return CVector2D(vecOne.x - vecTwo.x, vecOne.y - vecTwo.y); -} - -inline CVector2D operator+(const CVector2D& vecOne, const CVector2D& vecTwo) { - return CVector2D(vecOne.x + vecTwo.x, vecOne.y + vecTwo.y); -} - -inline CVector2D operator*(const CVector2D& vecOne, const CVector2D& vecTwo) { - return CVector2D(vecOne.x * vecTwo.x, vecOne.y * vecTwo.y); -} - -inline CVector2D operator*(const CVector2D& vec, float multiplier) { - return CVector2D(vec.x * multiplier, vec.y * multiplier); -} - -inline CVector2D operator*(float multiplier, const CVector2D& vec) { - return CVector2D(vec.x * multiplier, vec.y * multiplier); -} - -inline CVector2D operator/(const CVector2D& vec, float divisor) { - return CVector2D(vec.x / divisor, vec.y / divisor); -} - -inline float DistanceBetweenPoints(const CVector2D& pointOne, const CVector2D& pointTwo) { - return (pointTwo - pointOne).Magnitude(); -} - -inline float DotProduct(const CVector2D& a, const CVector2D& b) { - return a.x * b.x + a.y * b.y; -} - -inline float CrossProduct(const CVector2D& a, const CVector2D& b) { - return a.x * b.y - a.y * b.x; -} \ No newline at end of file diff --git a/plugin_sa_unreal/game_sa_unreal/CVector2D.cpp b/plugin_sa_unreal/game_sa_unreal/CVector2D.cpp deleted file mode 100644 index f4aa0dd3..00000000 --- a/plugin_sa_unreal/game_sa_unreal/CVector2D.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - Plugin-SDK (Grand Theft Auto San Andreas Unreal) source file - Authors: GTA Community. See more here - https://github.com/DK22Pac/plugin-sdk - Do not delete this comment block. Respect others' work! -*/ -#include "CVector2D.h" - -CVector2D::CVector2D() { - x = 0.0f; y = 0.0f; -} - -CVector2D::CVector2D(float X, float Y) { - x = X; y = Y; -} - -CVector2D::CVector2D(CVector2D const& src) { - x = src.x; y = src.y; -} - -float CVector2D::Magnitude() { - return sqrtf(this->x * this->x + this->y * this->y); -} - -float CVector2D::MagnitudeSqr() { - return x * x + y * y; -} - -void CVector2D::Sum(CVector2D& a, CVector2D& b) { - this->x = a.x + b.x; - this->y = a.y + b.y; -} - -void CVector2D::Difference(CVector2D& a, CVector2D& b) { - this->x = a.x - b.x; - this->y = a.y - b.y; -} - -void CVector2D::Normalise() { - float sq = MagnitudeSqr(); - float invsqrt = 1.0f / sqrt(sq); - x *= invsqrt; - y *= invsqrt; -} - -void CVector2D::operator=(const CVector2D& right) { - this->x = right.x; - this->y = right.y; -} - -void CVector2D::operator+=(const CVector2D& right) { - this->x += right.x; - this->y += right.y; -} - -void CVector2D::operator-=(const CVector2D& right) { - this->x -= right.x; - this->y -= right.y; -} - -void CVector2D::operator*=(float multiplier) { - this->x *= multiplier; - this->y *= multiplier; -} - -void CVector2D::operator/=(float divisor) { - this->x /= divisor; - this->y /= divisor; -} diff --git a/plugin_sa_unreal/game_sa_unreal/CVector2D.h b/plugin_sa_unreal/game_sa_unreal/CVector2D.h deleted file mode 100644 index fe7b462f..00000000 --- a/plugin_sa_unreal/game_sa_unreal/CVector2D.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - Plugin-SDK (Grand Theft Auto San Andreas Unreal) header file - Authors: GTA Community. See more here - https://github.com/DK22Pac/plugin-sdk - Do not delete this comment block. Respect others' work! -*/ -#pragma once -#include "PluginBase.h" - -class CVector2D { -public: - float x, y; - -public: - CVector2D(); - CVector2D(float X, float Y); - CVector2D(CVector2D const& src); - float Magnitude(); - float MagnitudeSqr(); - void Sum(CVector2D& a, CVector2D& b); - void Difference(CVector2D& a, CVector2D& b); - void Normalise(); - void operator=(const CVector2D& right); - void operator+=(const CVector2D& right); - void operator-=(const CVector2D& right); - void operator*=(float multiplier); - void operator/=(float divisor); - - CVector2D operator-(const CVector2D& rhs) const { - return CVector2D(x - rhs.x, y - rhs.y); - } - CVector2D operator+(const CVector2D& rhs) const { - return CVector2D(x + rhs.x, y + rhs.y); - } - CVector2D operator/(float t) const { - return CVector2D(x / t, y / t); - } -}; - -inline CVector2D operator*(const CVector2D& left, float right) { - return CVector2D(left.x * right, left.y * right); -} diff --git a/plugin_vc/game_vc/CVector2D.cpp b/plugin_vc/game_vc/CVector2D.cpp deleted file mode 100644 index b36973bb..00000000 --- a/plugin_vc/game_vc/CVector2D.cpp +++ /dev/null @@ -1,21 +0,0 @@ -/* - Plugin-SDK (Grand Theft Auto Vice City) source file - Authors: GTA Community. See more here - https://github.com/DK22Pac/plugin-sdk - Do not delete this comment block. Respect others' work! -*/ -#include "CVector2D.h" -#include "CVector.h" - -CVector CVector2D::To3D(float zValue) const { - return CVector(x, y, zValue); -} - -void CVector2D::From3D(const CVector& vec3d) { - x = vec3d.x; - y = vec3d.y; -} - -CVector2D::CVector2D(CVector const& src) { - x = src.x; y = src.y; -} \ No newline at end of file diff --git a/plugin_vc/game_vc/CVector2D.h b/plugin_vc/game_vc/CVector2D.h deleted file mode 100644 index 3f1ce5df..00000000 --- a/plugin_vc/game_vc/CVector2D.h +++ /dev/null @@ -1,162 +0,0 @@ -/* - Plugin-SDK (Grand Theft Auto Vice City) header file - Authors: GTA Community. See more here - https://github.com/DK22Pac/plugin-sdk - Do not delete this comment block. Respect others' work! -*/ -#pragma once - -#include "PluginBase.h" -#include - -struct CVector; - -class CVector2D { -public: - float x, y; - - inline CVector2D() { - x = 0.0f; y = 0.0f; - } - - inline CVector2D(float X, float Y) { - x = X; y = Y; - } - - inline CVector2D(CVector2D const& src) { - x = src.x; y = src.y; - } - - CVector2D(CVector const& src); - CVector To3D(float zValue = 0.0f) const; - void From3D(const CVector& vec3d); - - inline float Magnitude() { - return sqrtf(MagnitudeSqr()); - } - - inline float MagnitudeSqr() const { - return x * x + y * y; - } - - inline void Sum(CVector2D &a, CVector2D &b) { - x = a.x + b.x; - y = a.y + b.y; - } - - inline void Difference(CVector2D &a, CVector2D &b) { - x = a.x - b.x; - y = a.y - b.y; - } - - inline void Normalize() { - float mag = Magnitude(); - if (mag > 0.0f) { - x /= mag; - y /= mag; - } - } - - inline float NormaliseAndMag() { - float magSqr = MagnitudeSqr(); - if (magSqr > 0.0f) { - float mag = std::sqrt(magSqr); - float invMag = 1.0f / mag; - x *= invMag; - y *= invMag; - return mag; - } - return 0.0f; - } - - inline CVector2D operator-() const { - return CVector2D(-x, -y); - } - - inline void operator=(const CVector2D& right) { - x = right.x; - y = right.y; - } - - inline bool operator==(const CVector2D& other) { - return x == other.x && y == other.y; - } - - inline bool operator!=(const CVector2D& other) { - return x != other.x || y != other.y; - } - - inline void operator+=(const CVector2D& right) { - x += right.x; - y += right.y; - } - - inline void operator-=(const CVector2D& right) { - x -= right.x; - y -= right.y; - } - - inline void operator *= (float multiplier) { - x *= multiplier; - y *= multiplier; - } - - inline void operator /= (float divisor) { - x /= divisor; - y /= divisor; - } - - inline void Zero() { - x = 0.0f; - y = 0.0f; - } - - inline bool IsZero() const { - return x == 0.0f && y == 0.0f; - } - - inline bool IsNormalized() const { - return std::fabs(MagnitudeSqr() - 1.0f) < 0.001f; - } - - inline float Heading() { - return std::atan2(-x, y); - } -}; - -inline CVector2D operator/(const CVector2D& vec, float div) { - return CVector2D(vec.x / div, vec.y / div); -} - -inline CVector2D operator-(const CVector2D& vecOne, const CVector2D& vecTwo) { - return CVector2D(vecOne.x - vecTwo.x, vecOne.y - vecTwo.y); -} - -inline CVector2D operator+(const CVector2D& vecOne, const CVector2D& vecTwo) { - return CVector2D(vecOne.x + vecTwo.x, vecOne.y + vecTwo.y); -} - -inline CVector2D operator*(const CVector2D& vec, float multiplier) { - return CVector2D(vec.x * multiplier, vec.y * multiplier); -} - -inline CVector2D operator*(const CVector2D& vecOne, const CVector2D& vecTwo) { - return CVector2D(vecOne.x * vecTwo.x, vecOne.y * vecTwo.y); -} - -inline CVector2D operator*(float multiplier, const CVector2D& vec) { - return CVector2D(vec.x * multiplier, vec.y * multiplier); -} - -inline float DistanceBetweenPoints(const CVector2D& pointOne, const CVector2D& pointTwo) { - CVector2D diff = pointTwo - pointOne; - return diff.Magnitude(); -} - -inline float DotProduct(const CVector2D& a, const CVector2D& b) { - return a.x * b.x + a.y * b.y; -} - -inline float CrossProduct(const CVector2D& a, const CVector2D& b) { - return a.x * b.y - a.y * b.x; -} \ No newline at end of file diff --git a/plugin_vc_unreal/game_vc_unreal/CVector2D.cpp b/plugin_vc_unreal/game_vc_unreal/CVector2D.cpp deleted file mode 100644 index 89f7023e..00000000 --- a/plugin_vc_unreal/game_vc_unreal/CVector2D.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - Plugin-SDK (Grand Theft Auto Vice City Unreal) source file - Authors: GTA Community. See more here - https://github.com/DK22Pac/plugin-sdk - Do not delete this comment block. Respect others' work! -*/ -#include "CVector2D.h" - -CVector2D::CVector2D() { - x = 0.0f; y = 0.0f; -} - -CVector2D::CVector2D(float X, float Y) { - x = X; y = Y; -} - -CVector2D::CVector2D(CVector2D const& src) { - x = src.x; y = src.y; -} - -float CVector2D::Magnitude() { - return sqrtf(this->x * this->x + this->y * this->y); -} - -float CVector2D::MagnitudeSqr() { - return x * x + y * y; -} - -void CVector2D::Sum(CVector2D& a, CVector2D& b) { - this->x = a.x + b.x; - this->y = a.y + b.y; -} - -void CVector2D::Difference(CVector2D& a, CVector2D& b) { - this->x = a.x - b.x; - this->y = a.y - b.y; -} - -void CVector2D::Normalise() { - float sq = MagnitudeSqr(); - float invsqrt = 1.0f / sqrt(sq); - x *= invsqrt; - y *= invsqrt; -} - -void CVector2D::operator=(const CVector2D& right) { - this->x = right.x; - this->y = right.y; -} - -void CVector2D::operator+=(const CVector2D& right) { - this->x += right.x; - this->y += right.y; -} - -void CVector2D::operator-=(const CVector2D& right) { - this->x -= right.x; - this->y -= right.y; -} - -void CVector2D::operator*=(float multiplier) { - this->x *= multiplier; - this->y *= multiplier; -} - -void CVector2D::operator/=(float divisor) { - this->x /= divisor; - this->y /= divisor; -} diff --git a/plugin_vc_unreal/game_vc_unreal/CVector2D.h b/plugin_vc_unreal/game_vc_unreal/CVector2D.h deleted file mode 100644 index 84869ad1..00000000 --- a/plugin_vc_unreal/game_vc_unreal/CVector2D.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - Plugin-SDK (Grand Theft Auto Vice City Unreal) header file - Authors: GTA Community. See more here - https://github.com/DK22Pac/plugin-sdk - Do not delete this comment block. Respect others' work! -*/ -#pragma once -#include "PluginBase.h" - -class CVector2D { -public: - float x, y; - -public: - CVector2D(); - CVector2D(float X, float Y); - CVector2D(CVector2D const& src); - float Magnitude(); - float MagnitudeSqr(); - void Sum(CVector2D& a, CVector2D& b); - void Difference(CVector2D& a, CVector2D& b); - void Normalise(); - void operator=(const CVector2D& right); - void operator+=(const CVector2D& right); - void operator-=(const CVector2D& right); - void operator*=(float multiplier); - void operator/=(float divisor); - - CVector2D operator-(const CVector2D& rhs) const { - return CVector2D(x - rhs.x, y - rhs.y); - } - CVector2D operator+(const CVector2D& rhs) const { - return CVector2D(x + rhs.x, y + rhs.y); - } - CVector2D operator/(float t) const { - return CVector2D(x / t, y / t); - } -}; - -inline CVector2D operator*(const CVector2D& left, float right) { - return CVector2D(left.x * right, left.y * right); -} diff --git a/shared/game/CVector.h b/shared/game/CVector.h index 3e6e7d88..a166ce38 100644 --- a/shared/game/CVector.h +++ b/shared/game/CVector.h @@ -21,7 +21,7 @@ class CMatrix; #endif -class CVector2D; +struct CVector2D; struct CVector : public RwV3d { diff --git a/shared/game/CVector2D.h b/shared/game/CVector2D.h new file mode 100644 index 00000000..10447693 --- /dev/null +++ b/shared/game/CVector2D.h @@ -0,0 +1,110 @@ +/* + Plugin-SDK header file + Authors: GTA Community. See more here + https://github.com/DK22Pac/plugin-sdk + Do not delete this comment block. Respect others' work! +*/ +#pragma once +#include "PluginBase.h" + +#ifdef RW + #include "RenderWare.h" +#else + struct RwV2d + { + float x, y; + }; +#endif + +#if defined GTA3 || defined GTAVC || defined GTASA + #define HAS_CMATRIX + class CMatrix; +#endif + +struct CVector; + +struct CVector2D : public RwV2d +{ +public: + // constructors + CVector2D(); + explicit CVector2D(float value); + CVector2D(float x, float y); + CVector2D(const CVector2D& src); + CVector2D(const RwV2d& src); + + // assignments + void Reset(); // set to 0 0 + void Set(float value); // assign value to all components + void Set(float x, float y); + void operator =(const CVector2D& src); + void From3D(const CVector& src); + void FromSum(const CVector2D& left, const CVector2D& right); // store sum of two vectors + void FromDiff(const CVector2D& left, const CVector2D& right); // store left - right subtraction result + void FromLerp(const CVector2D& begin, const CVector2D& end, float progress); // store result of linear interpolation between points + #ifdef HAS_CMATRIX + void FromMultiply(const CMatrix& matrix, const CVector2D& point); // store result of matrix and point multiplication + void FromMultiply3x3(const CMatrix& matrix, const CVector2D& vector); // store result of matrix and vector multiplication +#endif + + // conversions + [[nodiscard]] operator RwV2d&(); + [[nodiscard]] operator const RwV2d&() const; + [[nodiscard]] operator RwV2d*(); + [[nodiscard]] operator const RwV2d*() const; + [[nodiscard]] CVector To3D() const; // get X Y 0 + + // properties + [[nodiscard]] bool operator ==(const CVector2D& other) const; + [[nodiscard]] bool operator !=(const CVector2D& other) const; + [[nodiscard]] CVector2D operator -() const; // opposite vector + [[nodiscard]] float Distance(const CVector2D& other) const; // distance to other + [[nodiscard]] float Dot(const CVector2D& other) const; // dot product + [[nodiscard]] float Cross(const CVector2D& other) const; // cross/wedge product + [[nodiscard]] float Heading() const; // direction of XY vec in radians + [[nodiscard]] float Magnitude() const; // length + [[nodiscard]] float MagnitudeSqr() const; // length^2 + [[nodiscard]] CVector2D Normalized() const; // this vector scaled to 1.0 length + [[nodiscard]] bool IsNormalized() const; // length is 1.0 +/- 0.001 + [[nodiscard]] bool IsZero() const; // all components are 0.0 + + // modifiers + void operator +=(float value); // add to all components + void operator +=(const CVector2D& other); + void operator -=(float value); // subtract from all components + void operator -=(const CVector2D& other); + void operator *=(float multiplier); // multiply all components + void operator /=(float divisor); // divide all components + void Normalize(); // scale to 1.0 length + float NormalizeAndMag(); // normalize and return previous length + + // static functions + [[nodiscard]] static CVector2D Sum(const CVector2D& left, const CVector2D& right); // result of left + right + [[nodiscard]] static CVector2D Diff(const CVector2D& left, const CVector2D& right); // result of left - right + [[nodiscard]] static float Distance(const CVector2D& left, const CVector2D& right); // distance between points + [[nodiscard]] static CVector2D Lerp(const CVector2D& begin, const CVector2D& end, float progress); // result of linear interpolation between points + [[nodiscard]] static float Dot(const CVector2D& left, const CVector2D& right); // result of dot product + [[nodiscard]] static float Cross(const CVector2D& left, const CVector2D& right); // result of cross/wedge product +#ifdef HAS_CMATRIX + [[nodiscard]] static CVector2D Multiply(const CMatrix& matrix, const CVector2D& point); // result of matrix and point multiplication + [[nodiscard]] static CVector2D Multiply3x3(const CMatrix& matrix, const CVector2D& vector); // result of matrix and vector multiplication +#endif +}; +VALIDATE_SIZE(CVector2D, 0x8); + +// static operators +[[nodiscard]] CVector2D operator +(float value, const CVector2D& vec); +[[nodiscard]] CVector2D operator +(const CVector2D& vec, float value); +[[nodiscard]] CVector2D operator +(const CVector2D& left, const CVector2D& right); + +[[nodiscard]] CVector2D operator -(float value, const CVector2D& vec); +[[nodiscard]] CVector2D operator -(const CVector2D& vec, float value); +[[nodiscard]] CVector2D operator -(const CVector2D& left, const CVector2D& right); + +[[nodiscard]] CVector2D operator *(float value, const CVector2D& vec); +[[nodiscard]] CVector2D operator *(const CVector2D& vec, float value); + +[[nodiscard]] CVector2D operator /(float value, const CVector2D& vec); +[[nodiscard]] CVector2D operator /(const CVector2D& vec, float value); + +#include "CVector2DImplementation.h" // inlined functions diff --git a/shared/game/CVector2DImplementation.h b/shared/game/CVector2DImplementation.h new file mode 100644 index 00000000..2de638d2 --- /dev/null +++ b/shared/game/CVector2DImplementation.h @@ -0,0 +1,282 @@ +/* + Plugin-SDK source file + Authors: GTA Community. See more here + https://github.com/DK22Pac/plugin-sdk + Do not delete this comment block. Respect others' work! +*/ +#pragma once +#include "CVector.h" + +// constructors + +inline CVector2D::CVector2D() { + x = 0.0f; + y = 0.0f; +} + +inline CVector2D::CVector2D(float value) { + Set(value); +} + +inline CVector2D::CVector2D(float x, float y) { + Set(x, y); +} + +// assignments + +inline void CVector2D::Reset() { + Set(0.0f); +} + +inline void CVector2D::Set(float value) { + Set(value, value); +} + +inline void CVector2D::Set(float x, float y) { + this->x = x; + this->y = y; +} + +inline void CVector2D::operator =(const CVector2D& src) { + Set(src.x, src.y); +} + +inline void CVector2D::From3D(const CVector& src) { + Set(src.x, src.y); +} + +inline void CVector2D::FromSum(const CVector2D& left, const CVector2D& right) { + *this = left; + *this += right; +} + +inline void CVector2D::FromDiff(const CVector2D& left, const CVector2D& right) { + *this = left; + *this -= right; +} + +inline void CVector2D::FromLerp(const CVector2D& begin, const CVector2D& end, float progress) { + *this = Lerp(begin, end, progress); +} + +// FromMultiply in CVector2D.cpp + +// FromMultiply3x3 in CVector2D.cpp + +// conversions + +inline CVector2D::operator RwV2d&() { + return *this; +} + +inline CVector2D::operator const RwV2d&() const { + return *this; +} + +inline CVector2D::operator RwV2d*() { + return reinterpret_cast(this); +} + +inline CVector2D::operator const RwV2d*() const { + return reinterpret_cast(this); +} + +inline CVector CVector2D::To3D() const { + return CVector(x, y, 0.0f); +} + +// properties + +inline bool CVector2D::operator ==(const CVector2D& other) const { + return x == other.x && y == other.y; +} + +inline bool CVector2D::operator !=(const CVector2D& other) const { + return x != other.x || y != other.y; +} + +inline CVector2D CVector2D::operator -() const { + return CVector2D(-x, -y); +} + +inline float CVector2D::Distance(const CVector2D& other) const { + return Distance(*this, other); +} + +inline float CVector2D::Dot(const CVector2D& other) const { + return Dot(*this, other); +} + +inline float CVector2D::Cross(const CVector2D& other) const { + return Cross(*this, other); +} + +inline float CVector2D::Heading() const { + return atan2(-x, y); +} + +inline float CVector2D::Magnitude() const { + return std::sqrt(MagnitudeSqr()); +} + +inline float CVector2D::MagnitudeSqr() const { + return x * x + y * y; +} + +inline CVector2D CVector2D::Normalized() const { + auto result = *this; + result.Normalize(); + return result; +} + +inline bool CVector2D::IsNormalized() const { + return fabs(MagnitudeSqr() - 1.0f) < 0.001f; +} + +inline bool CVector2D::IsZero() const { + return x == 0.0f && y == 0.0f; +} + +// modifiers + +inline void CVector2D::operator +=(float value) { + x += value; + y += value; +} + +inline void CVector2D::operator +=(const CVector2D& other) { + x += other.x; + y += other.y; +} + +inline void CVector2D::operator -=(float value) { + x -= value; + y -= value; +} + +inline void CVector2D::operator -=(const CVector2D& other) { + x -= other.x; + y -= other.y; +} + +inline void CVector2D::operator *=(float multiplier) { + x *= multiplier; + y *= multiplier; +} + +inline void CVector2D::operator /=(float divisor) { + x /= divisor; + y /= divisor; +} + +inline void CVector2D::Normalize() { + NormalizeAndMag(); +} + +inline float CVector2D::NormalizeAndMag() { + auto length = Magnitude(); + if (length > 0.0f) *this /= length; + return length; +} + +// static functions + +inline CVector2D CVector2D::Sum(const CVector2D& left, const CVector2D& right) { + auto result = left; + result += right; + return result; +} + +inline CVector2D CVector2D::Diff(const CVector2D& left, const CVector2D& right) { + auto result = left; + result -= right; + return result; +} + +inline float CVector2D::Distance(const CVector2D& left, const CVector2D& right) { + auto result = left; + result -= right; + return result.Magnitude(); +} + +inline CVector2D CVector2D::Lerp(const CVector2D& begin, const CVector2D& end, float progress) { + return CVector2D( + std::lerp(begin.x, end.x, progress), + std::lerp(begin.y, end.y, progress) + ); +} + +inline float CVector2D::Dot(const CVector2D& left, const CVector2D& right) { + return left.y * right.y + left.x * right.x; +} + +inline float CVector2D::Cross(const CVector2D& left, const CVector2D &right) { + return left.x * right.y - left.y * right.x; +} + +// Multiply in CVector2D.cpp + +// Multiply3x3 in CVector2D.cpp + +// static operators + +inline CVector2D operator +(float value, const CVector2D& vec) { + auto result = vec; + result += value; + return result; +} + +inline CVector2D operator +(const CVector2D& vec, float value) { + auto result = vec; + result += value; + return result; +} + +inline CVector2D operator +(const CVector2D& left, const CVector2D& right) { + auto result = left; + result += right; + return result; +} + +inline CVector2D operator -(float value, const CVector2D& vec) { + auto result = CVector2D(value); + result -= vec; + return result; +} + +inline CVector2D operator -(const CVector2D& vec, float value) { + auto result = vec; + result -= value; + return result; +} + +inline CVector2D operator -(const CVector2D& left, const CVector2D& right) { + auto result = left; + result -= right; + return result; +} + +inline CVector2D operator *(float value, const CVector2D& vec) { + auto result = vec; + result *= value; + return result; +} + +inline CVector2D operator *(const CVector2D& vec, float value) { + auto result = vec; + result *= value; + return result; +} + +inline CVector2D operator /(float value, const CVector2D& vec) { + auto result = CVector2D(value); + result.x /= vec.x; + result.y /= vec.y; + return result; +} + +inline CVector2D operator /(const CVector2D& vec, float value) { + auto result = vec; + result /= value; + return result; +} diff --git a/shared/game/CompressedVector2D.h b/shared/game/CompressedVector2D.h index badf5351..f50b5551 100644 --- a/shared/game/CompressedVector2D.h +++ b/shared/game/CompressedVector2D.h @@ -8,7 +8,7 @@ #include "PluginBase.h" -class CVector2D; +struct CVector2D; struct RwV2d; class CompressedVector; diff --git a/tools/premake/premake5.lua b/tools/premake/premake5.lua index 7c5d11aa..1fd86196 100644 --- a/tools/premake/premake5.lua +++ b/tools/premake/premake5.lua @@ -282,8 +282,6 @@ function pluginSdkStaticLibProject(projectName, sdkdir, outName, isPluginProject (gameFile(projectPath, gameName, "CQuaternion.*")), (gameFile(projectPath, gameName, "CRect.*")), (gameFile(projectPath, gameName, "CStore.*")), - (gameFile(projectPath, gameName, "CVector.*")), - (gameFile(projectPath, gameName, "CVector2D.*")), (gameFile(projectPath, gameName, "List_c.*")), (gameFile(projectPath, gameName, "ListItem_c.*")), (gameFile(projectPath, gameName, "SArray.*")) }, diff --git a/tools/premake/stdafx_template.h b/tools/premake/stdafx_template.h index d51c8e2a..f8b8db4f 100644 --- a/tools/premake/stdafx_template.h +++ b/tools/premake/stdafx_template.h @@ -31,14 +31,4 @@ #include GENERATED_LIST - - #ifdef GTAIII - #include "game_iii\CVector2D.h" - #endif - #ifdef GTAVC - #include "game_vc\CVector2D.h" - #endif - #ifdef GTASA - #include "game_sa\CVector2D.h" - #endif #endif \ No newline at end of file From ff9904866004f1b56b36be97a60ed4a4c744fb0f Mon Sep 17 00:00:00 2001 From: Miran Date: Mon, 6 Apr 2026 15:28:21 +0200 Subject: [PATCH 3/4] CEncodedVector in separated files --- plugin_II/game_II/CCamera.h | 2 +- plugin_II/game_II/CChar.h | 2 +- plugin_II/game_II/CEncodedVector.cpp | 54 ++++++++++++++++++++++++++++ plugin_II/game_II/CEncodedVector.h | 35 ++++++++++++++++++ plugin_II/game_II/CHud.h | 2 +- plugin_II/game_II/CObject.h | 2 +- plugin_II/game_II/CPhysics.h | 2 +- plugin_II/game_II/CSprite.h | 2 +- 8 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 plugin_II/game_II/CEncodedVector.cpp create mode 100644 plugin_II/game_II/CEncodedVector.h diff --git a/plugin_II/game_II/CCamera.h b/plugin_II/game_II/CCamera.h index 7b16afda..69685565 100644 --- a/plugin_II/game_II/CCamera.h +++ b/plugin_II/game_II/CCamera.h @@ -7,7 +7,7 @@ #pragma once #include "PluginBase.h" -#include "CVector.h" +#include "CEncodedVector.h" class CObject; diff --git a/plugin_II/game_II/CChar.h b/plugin_II/game_II/CChar.h index 3ace57fc..a25995f6 100644 --- a/plugin_II/game_II/CChar.h +++ b/plugin_II/game_II/CChar.h @@ -6,7 +6,7 @@ */ #pragma once #include "PluginBase.h" -#include "CVector.h" +#include "CEncodedVector.h" class CPed; diff --git a/plugin_II/game_II/CEncodedVector.cpp b/plugin_II/game_II/CEncodedVector.cpp new file mode 100644 index 00000000..93f285c9 --- /dev/null +++ b/plugin_II/game_II/CEncodedVector.cpp @@ -0,0 +1,54 @@ +/* + Plugin-SDK (Grand Theft Auto 2) source file + Authors: GTA Community. See more here + https://github.com/DK22Pac/plugin-sdk + Do not delete this comment block. Respect others' work! +*/ +#include "CEncodedVector.h" +#include "CVector2D.h" +#include "CVector.h" + +// 2D + +CEncodedVector2D::CEncodedVector2D() { + x = 0; + y = 0; +} + +CEncodedVector2D::CEncodedVector2D(int x, int y) { + this->x = x; + this->y = y; +} + +CEncodedVector2D::CEncodedVector2D(const CVector2D& src) { + x = (int)(src.x * 16384); + y = (int)(src.y * 16384); +} + +CVector2D CEncodedVector2D::FromInt16() { + return CVector2D(x / 16384.0f, y / 16384.0f); +} + +// 3D + +CEncodedVector::CEncodedVector() { + x = 0; + y = 0; + z = 0; +} + +CEncodedVector::CEncodedVector(int x, int y, int z) { + this->x = x; + this->y = y; + this->z = z; +} + +CEncodedVector::CEncodedVector(const CVector& src) { + x = (int)(src.x * 16384); + y = (int)(src.y * 16384); + z = (int)(src.z * 16384); +} + +CVector CEncodedVector::FromInt16() { + return CVector(x / 16384.0f, y / 16384.0f, z / 16384.0f); +} diff --git a/plugin_II/game_II/CEncodedVector.h b/plugin_II/game_II/CEncodedVector.h new file mode 100644 index 00000000..5a210f82 --- /dev/null +++ b/plugin_II/game_II/CEncodedVector.h @@ -0,0 +1,35 @@ +/* + Plugin-SDK (Grand Theft Auto 2) header file + Authors: GTA Community. See more here + https://github.com/DK22Pac/plugin-sdk + Do not delete this comment block. Respect others' work! +*/ +#pragma once +#include "PluginBase.h" + +struct CVector2D; +struct CVector; + +class CEncodedVector2D { +public: + int x, y; + +public: + CEncodedVector2D(); + CEncodedVector2D(int x, int y); + CEncodedVector2D(const CVector2D& src); + + CVector2D FromInt16(); +}; + +class CEncodedVector { +public: + int x, y, z; + +public: + CEncodedVector(); + CEncodedVector(int x, int y, int z); + CEncodedVector(const CVector& src); + + CVector FromInt16(); +}; diff --git a/plugin_II/game_II/CHud.h b/plugin_II/game_II/CHud.h index 7ab08353..f8190b0a 100644 --- a/plugin_II/game_II/CHud.h +++ b/plugin_II/game_II/CHud.h @@ -9,7 +9,7 @@ #define MAX_HUD_ARROWS 17 #include "PluginBase.h" -#include "CVector.h" +#include "CEncodedVector.h" enum eArrowSprites { SPRITE_BIGARROW, diff --git a/plugin_II/game_II/CObject.h b/plugin_II/game_II/CObject.h index 7d886a23..ab3f3688 100644 --- a/plugin_II/game_II/CObject.h +++ b/plugin_II/game_II/CObject.h @@ -7,7 +7,7 @@ #pragma once #include "PluginBase.h" -#include "CVector.h" +#include "CEncodedVector.h" class CPed; class CSprite; diff --git a/plugin_II/game_II/CPhysics.h b/plugin_II/game_II/CPhysics.h index 23557b06..bc6be117 100644 --- a/plugin_II/game_II/CPhysics.h +++ b/plugin_II/game_II/CPhysics.h @@ -7,7 +7,7 @@ #pragma once #include "PluginBase.h" -#include "CVector.h" +#include "CEncodedVector.h" enum eTileCollisionType { TILE_COLLISION_TYPE_FLAT = 0, diff --git a/plugin_II/game_II/CSprite.h b/plugin_II/game_II/CSprite.h index 5e22e091..b47b418f 100644 --- a/plugin_II/game_II/CSprite.h +++ b/plugin_II/game_II/CSprite.h @@ -7,8 +7,8 @@ #pragma once #include "PluginBase.h" -#include "CVector.h" #include "CCollisionBox.h" +#include "CEncodedVector.h" #ifdef GetObject #undef GetObject From 4f5b2b5081d00fa6139f351ac06f80780b021090 Mon Sep 17 00:00:00 2001 From: Miran Date: Mon, 6 Apr 2026 21:42:35 +0200 Subject: [PATCH 4/4] Fixes and unit tests --- examples/UnitTests/source/Main.cpp | 1 + examples/UnitTests/source/Shared.h | 6 + examples/UnitTests/source/Test_CVector.h | 5 +- examples/UnitTests/source/Test_CVector2D.h | 630 +++++++++++++++++++++ shared/game/CVector2D.h | 13 - shared/game/CVector2DImplementation.h | 12 +- 6 files changed, 646 insertions(+), 21 deletions(-) create mode 100644 examples/UnitTests/source/Shared.h create mode 100644 examples/UnitTests/source/Test_CVector2D.h diff --git a/examples/UnitTests/source/Main.cpp b/examples/UnitTests/source/Main.cpp index 2db54b84..c7e87cc4 100644 --- a/examples/UnitTests/source/Main.cpp +++ b/examples/UnitTests/source/Main.cpp @@ -1,6 +1,7 @@ #include #include "utest.h" #include "Test_CVector.h" +#include "Test_CVector2D.h" using namespace plugin; diff --git a/examples/UnitTests/source/Shared.h b/examples/UnitTests/source/Shared.h new file mode 100644 index 00000000..4f6cfb52 --- /dev/null +++ b/examples/UnitTests/source/Shared.h @@ -0,0 +1,6 @@ +#pragma once +#include "Maths.h" + +inline constexpr float F_EPS = 0.00001f; +inline const float F_PI = (float)PI; +inline const float F_PI2 = (float)(PI/2); diff --git a/examples/UnitTests/source/Test_CVector.h b/examples/UnitTests/source/Test_CVector.h index efc77525..2eb85e26 100644 --- a/examples/UnitTests/source/Test_CVector.h +++ b/examples/UnitTests/source/Test_CVector.h @@ -1,14 +1,11 @@ #pragma once #include #include "utest.h" +#include "Shared.h" #include using namespace plugin; -constexpr float F_EPS = 0.00001f; -const float F_PI = (float)PI; -const float F_PI2 = (float)(PI/2); - // constructors UTEST(CVector, ctor) diff --git a/examples/UnitTests/source/Test_CVector2D.h b/examples/UnitTests/source/Test_CVector2D.h new file mode 100644 index 00000000..0b1df164 --- /dev/null +++ b/examples/UnitTests/source/Test_CVector2D.h @@ -0,0 +1,630 @@ +#pragma once +#include +#include "utest.h" +#include "Shared.h" +#include + +using namespace plugin; + +// constructors + +UTEST(CVector2D, ctor) +{ + auto v = CVector2D(); + EXPECT_EQ(v.x, 0.0f); + EXPECT_EQ(v.y, 0.0f); +} + +UTEST(CVector2D, ctor_F) +{ + auto v = CVector2D(2.0f); + EXPECT_EQ(v.x, 2.0f); + EXPECT_EQ(v.y, 2.0f); +} + +UTEST(CVector2D, ctor_FF) +{ + auto v = CVector2D(1.0f, 2.0f); + EXPECT_EQ(v.x, 1.0f); + EXPECT_EQ(v.y, 2.0f); +} + +UTEST(CVector2D, ctor_CVector2D) +{ + CVector2D src; + src.x = 1.0f; + src.y = 2.0f; + + auto v = CVector2D(src); + EXPECT_EQ(v.x, 1.0f); + EXPECT_EQ(v.y, 2.0f); +} + +UTEST(CVector2D, ctor_RwV2d) +{ + RwV2d src; + src.x = 1.0f; + src.y = 2.0f; + + auto v = CVector2D(src); + EXPECT_EQ(v.x, 1.0f); + EXPECT_EQ(v.y, 2.0f); +} + +// assignments + +UTEST(CVector2D, Reset) +{ + auto v = CVector2D(1.0f, 2.0f); + + v.Reset(); + EXPECT_EQ(v.x, 0.0f); + EXPECT_EQ(v.y, 0.0f); +} + +UTEST(CVector2D, Set_F) +{ + auto v = CVector2D(); + + v.Set(2.0f); + EXPECT_EQ(v.x, 2.0f); + EXPECT_EQ(v.y, 2.0f); +} + +UTEST(CVector2D, Set_FF) +{ + auto v = CVector2D(); + + v.Set(1.0f, 2.0f); + EXPECT_EQ(v.x, 1.0f); + EXPECT_EQ(v.y, 2.0f); +} + +UTEST(CVector2D, operator_assign) +{ + CVector2D src; + src.x = 1.0f; + src.y = 2.0f; + + CVector2D v = src; + EXPECT_EQ(v.x, 1.0f); + EXPECT_EQ(v.y, 2.0f); +} + +UTEST(CVector2D, operator_assign_RwV2d) +{ + RwV2d src; + src.x = 1.0f; + src.y = 2.0f; + CVector2D v; + + v = src; + EXPECT_EQ(v.x, 1.0f); + EXPECT_EQ(v.y, 2.0f); +} + +UTEST(CVector2D, From3D) +{ + CVector src; + src.x = 1.0f; + src.y = 2.0f; + src.z = 3.0f; + CVector2D v; + + v.From3D(src); + EXPECT_EQ(v.x, 1.0f); + EXPECT_EQ(v.y, 2.0f); +} + +UTEST(CVector2D, FromSum) +{ + const auto a = CVector2D(1.0f, 2.0f); + const auto b = CVector2D(4.0f, 5.0f); + CVector2D v; + + v.FromSum(a, b); + EXPECT_EQ(v.x, 5.0f); + EXPECT_EQ(v.y, 7.0f); +} + +UTEST(CVector2D, FromDiff) +{ + const auto a = CVector2D(1.0f, 2.0f); + const auto b = CVector2D(8.0f, 4.0f); + CVector2D v; + + v.FromDiff(a, b); + EXPECT_EQ(v.x, -7.0f); + EXPECT_EQ(v.y, -2.0f); +} + +UTEST(CVector2D, FromLerp) +{ + const auto a = CVector2D(1.0f, 2.0f); + const auto b = CVector2D(8.0f, 4.0f); + CVector2D v; + + v.FromLerp(a, b, 0.0f); + EXPECT_NEAR(v.x, 1.0f, F_EPS); + EXPECT_NEAR(v.y, 2.0f, F_EPS); + + v.FromLerp(a, b, 1.0f); + EXPECT_NEAR(v.x, 8.0f, F_EPS); + EXPECT_NEAR(v.y, 4.0f, F_EPS); + + v.FromLerp(a, b, 0.5f); + EXPECT_NEAR(v.x, 4.5f, F_EPS); + EXPECT_NEAR(v.y, 3.0f, F_EPS); +} + +// conversions + +UTEST(CVector2D, ToRwV2d) +{ + CVector2D src; + src.x = 1.0f; + src.y = 2.0f; + + RwV2d v = src; + EXPECT_EQ(v.x, 1.0f); + EXPECT_EQ(v.y, 2.0f); +} + +UTEST(CVector2D, To3D) +{ + CVector2D src; + src.x = 1.0f; + src.y = 2.0f; + + CVector v = src.To3D(); + EXPECT_EQ(v.x, 1.0f); + EXPECT_EQ(v.y, 2.0f); + EXPECT_EQ(v.z, 0.0f); +} + +// properties + +UTEST(CVector2D, operator_equal) +{ + const auto a = CVector2D(1.0f, 2.0f); + const auto b = CVector2D(8.0f, 4.0f); + const auto c = CVector2D(1.0f, 2.0f); + + EXPECT_TRUE(a == a); + EXPECT_FALSE(a == b); + EXPECT_TRUE(a == c); +} + + +UTEST(CVector2D, operator_inequal) +{ + const auto a = CVector2D(1.0f, 2.0f); + const auto b = CVector2D(8.0f, 4.0f); + const auto c = CVector2D(1.0f, 2.0f); + + EXPECT_FALSE(a != a); + EXPECT_TRUE(a != b); + EXPECT_FALSE(a != c); +} + +UTEST(CVector2D, operator_unaryMinus) +{ + const auto a = CVector2D(1.0f, 2.0f); + + auto v = -a; + EXPECT_EQ(v.x, -1.0f); + EXPECT_EQ(v.y, -2.0f); +} + +UTEST(CVector2D, Distance_CVector2D) +{ + const auto a = CVector2D(1.0f, 2.0f); + const auto b = CVector2D(8.0f, 4.0f); + + auto distA = a.Distance(b); + auto distB = b.Distance(a); + EXPECT_NEAR(distA, 7.2801099f, F_EPS); + EXPECT_NEAR(distB, 7.2801099f, F_EPS); + EXPECT_EQ(distA, distB); +} + +UTEST(CVector2D, Dot_CVector2D) +{ + const auto a = CVector2D(1.0f, 2.0f); + const auto b = CVector2D(8.0f, 4.0f); + + auto d = a.Dot(b); + EXPECT_NEAR(d, 16.0f, F_EPS); + + auto self = a.Dot(a); + EXPECT_NEAR(self, 5.0f, F_EPS); +} + +UTEST(CVector2D, Cross_CVector2D) +{ + const auto a = CVector2D(1.0f, 2.0f); + const auto b = CVector2D(8.0f, 4.0f); + + auto d = a.Cross(b); + EXPECT_NEAR(-12.0f, d, F_EPS); + + d = a.Cross(a); + EXPECT_NEAR(0.0f, d, F_EPS); +} + +UTEST(CVector2D, Heading) +{ + CVector2D v; + float h; + + // north + v.Set(F_EPS / 4, 1.0f); // slightly to the east + h = v.Heading(); + EXPECT_NEAR(0.0f, h, F_EPS); + + // east + v.Set(1.0f, 0.0f); + h = v.Heading(); + EXPECT_NEAR(-F_PI2, h, F_EPS); + + // south + v.Set(F_EPS / 4, -1.0f); // slightly to the east + h = v.Heading(); + EXPECT_NEAR(-F_PI, h, F_EPS); + + // west + v.Set(-1.0f, 0.0f); + h = v.Heading(); + EXPECT_NEAR(F_PI2, h, F_EPS); +} + +UTEST(CVector2D, Magnitude) +{ + CVector2D v; + float m; + + v.Set(0.0f, 0.0f); + m = v.Magnitude(); + EXPECT_NEAR(0.0f, m, F_EPS); + + v.Set(1.0f, 0.0f); + m = v.Magnitude(); + EXPECT_NEAR(1.0f, m, F_EPS); + + v.Set(1.0f, 2.0f); + m = v.Magnitude(); + EXPECT_NEAR(2.236068f, m, F_EPS); +} + +UTEST(CVector2D, MagnitudeSqr) +{ + CVector2D v; + float m; + + v.Set(0.0f, 0.0f); + m = v.MagnitudeSqr(); + EXPECT_NEAR(0.0f, m, F_EPS); + + v.Set(1.0f, 0.0f); + m = v.MagnitudeSqr(); + EXPECT_NEAR(1.0f, m, F_EPS); + + v.Set(1.0f, 2.0f); + m = v.MagnitudeSqr(); + EXPECT_NEAR(5.0f, m, F_EPS); +} + +UTEST(CVector2D, Normalized) +{ + CVector2D v; + + v.Set(0.1f, 0.0f); + CVector2D b = v.Normalized(); + + EXPECT_NEAR(0.1f, v.x, F_EPS); + EXPECT_NEAR(0.0f, v.y, F_EPS); + + EXPECT_NEAR(1.0f, b.x, F_EPS); + EXPECT_NEAR(0.0f, b.y, F_EPS); +} + +UTEST(CVector2D, IsNormalized) +{ + CVector2D v; + + v.Set(0.0f, 0.0f); + EXPECT_FALSE(v.IsNormalized()); + + v.Set(1.0f, 0.0f); + EXPECT_TRUE(v.IsNormalized()); + + v.Set(1.0f, 2.0f); + EXPECT_FALSE(v.IsNormalized()); + + v.Set(0.5350f, 0.8448f); + EXPECT_TRUE(v.IsNormalized()); +} + +UTEST(CVector2D, IsZero) +{ + CVector2D v; + + v.Set(0.0f, 0.0f); + EXPECT_TRUE(v.IsZero()); + + v.Set(F_EPS, 0.0f); + EXPECT_FALSE(v.IsZero()); + + v.Set(0.0f, F_EPS); + EXPECT_FALSE(v.IsZero()); +} + +// modifiers + +UTEST(CVector2D, operator_compoundPlus_F) +{ + auto v = CVector2D(1.0f, 2.0f); + + v += 2.0f; + EXPECT_EQ(v.x, 3.0f); + EXPECT_EQ(v.y, 4.0f); +} + +UTEST(CVector2D, operator_compoundPlus_CVector2D) +{ + auto v = CVector2D(1.0f, 2.0f); + + v += CVector2D(4.0f, 5.0f); + EXPECT_EQ(v.x, 5.0f); + EXPECT_EQ(v.y, 7.0f); +} + +UTEST(CVector2D, operator_compoundMinus_F) +{ + auto v = CVector2D(1.0f, 2.0f); + + v -= 2.0f; + EXPECT_EQ(v.x, -1.0f); + EXPECT_EQ(v.y, 0.0f); +} + +UTEST(CVector2D, operator_compoundMinus_CVector2D) +{ + auto v = CVector2D(1.0f, 2.0f); + + v -= CVector2D(2.0f, 4.0f); + EXPECT_EQ(v.x, -1.0f); + EXPECT_EQ(v.y, -2.0f); +} + +UTEST(CVector2D, operator_compoundAsterix_F) +{ + auto v = CVector2D(1.0f, 2.0f); + + v *= 2.0f; + EXPECT_EQ(v.x, 2.0f); + EXPECT_EQ(v.y, 4.0f); +} + +UTEST(CVector2D, operator_compoundSlash_F) +{ + auto v = CVector2D(1.0f, 2.0f); + + v /= 2.0f; + EXPECT_EQ(v.x, 0.5f); + EXPECT_EQ(v.y, 1.0f); +} + +UTEST(CVector2D, Normalize) +{ + CVector2D v; + + v.Set(0.1f, 0.0f); + v.Normalize(); + EXPECT_NEAR(1.0f, v.x, F_EPS); + EXPECT_NEAR(0.0f, v.y, F_EPS); + + v.Set(0.0f, 2.0f); + v.Normalize(); + EXPECT_NEAR(0.0f, v.x, F_EPS); + EXPECT_NEAR(1.0f, v.y, F_EPS); + + v.Set(1.0f, 2.0f); + v.Normalize(); + EXPECT_NEAR(0.447214f, v.x, F_EPS); + EXPECT_NEAR(0.894427f, v.y, F_EPS); +} + +UTEST(CVector2D, NormalizeAndMag) +{ + CVector2D v; + float m; + + v.Set(0.1f, 0.0f); + m = v.NormalizeAndMag(); + EXPECT_NEAR(0.1f, m, F_EPS); + EXPECT_NEAR(1.0f, v.x, F_EPS); + EXPECT_NEAR(0.0f, v.y, F_EPS); + + v.Set(0.0f, 2.0f); + m = v.NormalizeAndMag(); + EXPECT_NEAR(2.0f, m, F_EPS); + EXPECT_NEAR(0.0f, v.x, F_EPS); + EXPECT_NEAR(1.0f, v.y, F_EPS); + + v.Set(1.0f, 2.0f); + m = v.NormalizeAndMag(); + EXPECT_NEAR(2.236068f, m, F_EPS); + EXPECT_NEAR(0.447213f, v.x, F_EPS); + EXPECT_NEAR(0.894427f, v.y, F_EPS); +} + +// static functions + +UTEST(CVector2D, Sum) +{ + const auto a = CVector2D(1.0f, 2.0f); + const auto b = CVector2D(4.0f, 5.0f); + + CVector2D v = CVector2D::Sum(a, b); + EXPECT_EQ(v.x, 5.0f); + EXPECT_EQ(v.y, 7.0f); +} + +UTEST(CVector2D, Diff) +{ + const auto a = CVector2D(1.0f, 2.0f); + const auto b = CVector2D(8.0f, 4.0f); + + CVector2D v = CVector2D::Diff(a, b); + EXPECT_EQ(v.x, -7.0f); + EXPECT_EQ(v.y, -2.0f); +} + +UTEST(CVector2D, Distance_CVector2DCVector2D) +{ + const auto a = CVector2D(1.0f, 2.0f); + const auto b = CVector2D(8.0f, 4.0f); + + auto dist = CVector2D::Distance(a, b); + EXPECT_NEAR(dist, 7.28011f, F_EPS); +} + +UTEST(CVector2D, Lerp) +{ + const auto a = CVector2D(1.0f, 2.0f); + const auto b = CVector2D(8.0f, 4.0f); + CVector2D v; + + v = CVector2D::Lerp(a, b, 0.0f); + EXPECT_NEAR(v.x, 1.0f, F_EPS); + EXPECT_NEAR(v.y, 2.0f, F_EPS); + + v = CVector2D::Lerp(a, b, 1.0f); + EXPECT_NEAR(v.x, 8.0f, F_EPS); + EXPECT_NEAR(v.y, 4.0f, F_EPS); + + v = CVector2D::Lerp(a, b, 0.5f); + EXPECT_NEAR(v.x, 4.5f, F_EPS); + EXPECT_NEAR(v.y, 3.0f, F_EPS); +} + +UTEST(CVector2D, Dot_CVector2DCVector2D) +{ + const auto a = CVector2D(1.0f, 2.0f); + const auto b = CVector2D(8.0f, 4.0f); + + auto d = CVector2D::Dot(a, b); + EXPECT_NEAR(d, 16.0f, F_EPS); +} + +UTEST(CVector2D, Cross_CVector2DCVector2D) +{ + const auto a = CVector2D(1.0f, 2.0f); + const auto b = CVector2D(8.0f, 4.0f); + + auto v = CVector2D::Cross(a, b); + EXPECT_NEAR(-12.0f, v, F_EPS); +} + +// static operators + +UTEST(CVector2D, operator_plus_FCVector2D) +{ + const auto v = CVector2D(1.0f, 2.0f); + const auto f = 2.0f; + + auto result = f + v; + EXPECT_EQ(result.x, 3.0f); + EXPECT_EQ(result.y, 4.0f); +} + +UTEST(CVector2D, operator_plus_CVector2DF) +{ + const auto v = CVector2D(1.0f, 2.0f); + const auto f = 2.0f; + + auto result = v + f; + EXPECT_EQ(result.x, 3.0f); + EXPECT_EQ(result.y, 4.0f); +} + +UTEST(CVector2D, operator_plus_CVector2DCVector2D) +{ + const auto a = CVector2D(1.0f, 2.0f); + const auto b = CVector2D(4.0f, 5.0f); + + auto v = a + b; + EXPECT_EQ(v.x, 5.0f); + EXPECT_EQ(v.y, 7.0f); +} + +UTEST(CVector2D, operator_minus_FCVector2D) +{ + const auto v = CVector2D(1.0f, 2.0f); + const auto f = 2.0f; + + auto result = f - v; + EXPECT_EQ(result.x, 1.0f); + EXPECT_EQ(result.y, 0.0f); +} + +UTEST(CVector2D, operator_minus_CVector2DF) +{ + const auto v = CVector2D(1.0f, 2.0f); + const auto f = 2.0f; + + auto result = v - f; + EXPECT_EQ(result.x, -1.0f); + EXPECT_EQ(result.y, 0.0f); +} + +UTEST(CVector2D, operator_minus_CVector2DCVector2D) +{ + const auto a = CVector2D(1.0f, 2.0f); + const auto b = CVector2D(8.0f, 4.0f); + + auto v = a - b; + EXPECT_EQ(v.x, -7.0f); + EXPECT_EQ(v.y, -2.0f); +} + +UTEST(CVector2D, operator_Asterix_FCVector2D) +{ + auto v = CVector2D(1.0f, 2.0f); + const auto f = 2.0f; + + auto result = f * v; + EXPECT_EQ(result.x, 2.0f); + EXPECT_EQ(result.y, 4.0f); +} + +UTEST(CVector2D, operator_Asterix_CVector2DF) +{ + auto v = CVector2D(1.0f, 2.0f); + const auto f = 2.0f; + + auto result = v * f; + EXPECT_EQ(result.x, 2.0f); + EXPECT_EQ(result.y, 4.0f); +} + +UTEST(CVector2D, operator_compoundSlash_FCVector2D) +{ + auto v = CVector2D(1.0f, 2.0f); + const auto f = 2.0f; + + auto result = f / v; + EXPECT_EQ(result.x, 2.0f); + EXPECT_EQ(result.y, 1.0f); +} + +UTEST(CVector2D, operator_compoundSlash_CVector2DF) +{ + auto v = CVector2D(1.0f, 2.0f); + const auto f = 2.0f; + + auto result = v / f; + EXPECT_EQ(result.x, 0.5f); + EXPECT_EQ(result.y, 1.0f); +} diff --git a/shared/game/CVector2D.h b/shared/game/CVector2D.h index 10447693..fdb32a6c 100644 --- a/shared/game/CVector2D.h +++ b/shared/game/CVector2D.h @@ -16,11 +16,6 @@ }; #endif -#if defined GTA3 || defined GTAVC || defined GTASA - #define HAS_CMATRIX - class CMatrix; -#endif - struct CVector; struct CVector2D : public RwV2d @@ -42,10 +37,6 @@ struct CVector2D : public RwV2d void FromSum(const CVector2D& left, const CVector2D& right); // store sum of two vectors void FromDiff(const CVector2D& left, const CVector2D& right); // store left - right subtraction result void FromLerp(const CVector2D& begin, const CVector2D& end, float progress); // store result of linear interpolation between points - #ifdef HAS_CMATRIX - void FromMultiply(const CMatrix& matrix, const CVector2D& point); // store result of matrix and point multiplication - void FromMultiply3x3(const CMatrix& matrix, const CVector2D& vector); // store result of matrix and vector multiplication -#endif // conversions [[nodiscard]] operator RwV2d&(); @@ -85,10 +76,6 @@ struct CVector2D : public RwV2d [[nodiscard]] static CVector2D Lerp(const CVector2D& begin, const CVector2D& end, float progress); // result of linear interpolation between points [[nodiscard]] static float Dot(const CVector2D& left, const CVector2D& right); // result of dot product [[nodiscard]] static float Cross(const CVector2D& left, const CVector2D& right); // result of cross/wedge product -#ifdef HAS_CMATRIX - [[nodiscard]] static CVector2D Multiply(const CMatrix& matrix, const CVector2D& point); // result of matrix and point multiplication - [[nodiscard]] static CVector2D Multiply3x3(const CMatrix& matrix, const CVector2D& vector); // result of matrix and vector multiplication -#endif }; VALIDATE_SIZE(CVector2D, 0x8); diff --git a/shared/game/CVector2DImplementation.h b/shared/game/CVector2DImplementation.h index 2de638d2..b3b16972 100644 --- a/shared/game/CVector2DImplementation.h +++ b/shared/game/CVector2DImplementation.h @@ -22,6 +22,14 @@ inline CVector2D::CVector2D(float x, float y) { Set(x, y); } +inline CVector2D::CVector2D(const CVector2D& src) { + Set(src.x, src.y); +} + +inline CVector2D::CVector2D(const RwV2d& src) { + Set(src.x, src.y); +} + // assignments inline void CVector2D::Reset() { @@ -59,10 +67,6 @@ inline void CVector2D::FromLerp(const CVector2D& begin, const CVector2D& end, fl *this = Lerp(begin, end, progress); } -// FromMultiply in CVector2D.cpp - -// FromMultiply3x3 in CVector2D.cpp - // conversions inline CVector2D::operator RwV2d&() {