From 3887e8cce5bd4d2369108200add774fef1306924 Mon Sep 17 00:00:00 2001 From: maybegreat48 <96936658+maybegreat48@users.noreply.github.com> Date: Mon, 15 Jul 2024 15:12:40 +0000 Subject: [PATCH] Vector Improvements (#25) Co-authored-by: maybegreat48 --- rage/vector.hpp | 117 +++++++++++++++++++++++++++--------------------- 1 file changed, 65 insertions(+), 52 deletions(-) diff --git a/rage/vector.hpp b/rage/vector.hpp index d3a1c82..d3a7317 100644 --- a/rage/vector.hpp +++ b/rage/vector.hpp @@ -1,12 +1,14 @@ #pragma once +#include +#pragma pack(push, 0x10) namespace rage { template - union vector2 + class vector2 { - T data[2]; - struct { T x, y; }; + public: + T x, y; constexpr vector2(T x, T y) : x(x), @@ -22,10 +24,10 @@ namespace rage }; template - union vector3 + class vector3 { - T data[3]; - struct { T x, y, z; }; + public: + T x, y, z; constexpr vector3(T x, T y, T z) : x(x), @@ -42,63 +44,23 @@ namespace rage } template - vector3 operator+(const vector3& other) - { - vector3 vec; - vec.x = this->x + other.x; - vec.y = this->y + other.y; - vec.z = this->z + other.z; - return vec; - } - - template - vector3 operator-(const vector3& other) - { - vector3 vec; - vec.x = this->x - other.x; - vec.y = this->y - other.y; - vec.z = this->z - other.z; - return vec; - } - - template - vector3 operator*(const vector3& other) - { - vector3 vec; - vec.x = this->x * other.x; - vec.y = this->y * other.y; - vec.z = this->z * other.z; - return vec; - } - - template - vector3 operator*(const float& other) - { - vector3 vec; - vec.x = this->x * other; - vec.y = this->y * other; - vec.z = this->z * other; - return vec; - } - - template - bool operator==(const vector3& other) + bool operator==(const vector3& other) const { return this->x == other.x && this->y == other.y && this->z == other.z; } template - bool operator!=(const vector3& other) + bool operator!=(const vector3& other) const { return this->x != other.x || this->y != other.y || this->z != other.z; } }; template - union vector4 + class vector4 { - T data[4]; - struct { T x, y, z, w; }; + public: + T x, y, z, w; constexpr vector4(T x, T y, T z, T w) : x(x), @@ -132,8 +94,59 @@ namespace rage }; typedef vector2 fvector2; - typedef vector3 fvector3; typedef vector4 fvector4; typedef matrix34 fmatrix34; typedef matrix44 fmatrix44; + + class fvector3 : public vector3 + { + using vector3::vector3; + + fvector3 operator+(const fvector3& other) const + { + fvector3 vec; + vec.x = this->x + other.x; + vec.y = this->y + other.y; + vec.z = this->z + other.z; + return vec; + } + + fvector3 operator-(const fvector3& other) const + { + fvector3 vec; + vec.x = this->x - other.x; + vec.y = this->y - other.y; + vec.z = this->z - other.z; + return vec; + } + + fvector3 operator*(const fvector3& other) const + { + fvector3 vec; + vec.x = this->x * other.x; + vec.y = this->y * other.y; + vec.z = this->z * other.z; + return vec; + } + + fvector3 operator*(const float& other) const + { + fvector3 vec; + vec.x = this->x * other; + vec.y = this->y * other; + vec.z = this->z * other; + return vec; + } + + inline float GetMagnitude() const + { + return std::sqrt(x * x + y * y + z * z); + } + + inline float GetDistance(const rage::fvector3& other) const + { + return (*this - other).GetMagnitude(); + } + }; } +#pragma pack(pop) \ No newline at end of file