PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
AVector3.hpp
1#pragma once
2
3#include <atomic>
4#include <cmath>
5#include <iostream>
6
7namespace PixelBullet
8{
9 template <typename T>
11 {
12 public:
14 AVector3() noexcept
15 : m_X(0)
16 , m_Y(0)
17 , m_Z(0)
18 {
19 }
20
22 AVector3(T x, T y, T z) noexcept
23 : m_X(x)
24 , m_Y(y)
25 , m_Z(z)
26 {
27 }
28
30 AVector3(const AVector3& other) noexcept
31 : m_X(other.m_X.load(std::memory_order_relaxed))
32 , m_Y(other.m_Y.load(std::memory_order_relaxed))
33 , m_Z(other.m_Z.load(std::memory_order_relaxed))
34 {
35 }
36
38 AVector3(AVector3&& other) noexcept
39 : m_X(other.m_X.load(std::memory_order_relaxed))
40 , m_Y(other.m_Y.load(std::memory_order_relaxed))
41 , m_Z(other.m_Z.load(std::memory_order_relaxed))
42 {
43 other.m_X.store(0, std::memory_order_relaxed);
44 other.m_Y.store(0, std::memory_order_relaxed);
45 other.m_Z.store(0, std::memory_order_relaxed);
46 }
47
49 AVector3& operator=(const AVector3& other) noexcept
50 {
51 if (this != &other)
52 {
53 m_X.store(other.m_X.load(std::memory_order_relaxed), std::memory_order_relaxed);
54 m_Y.store(other.m_Y.load(std::memory_order_relaxed), std::memory_order_relaxed);
55 m_Z.store(other.m_Z.load(std::memory_order_relaxed), std::memory_order_relaxed);
56 }
57 return *this;
58 }
59
61 AVector3& operator=(AVector3&& other) noexcept
62 {
63 if (this != &other)
64 {
65 m_X.store(other.m_X.load(std::memory_order_relaxed), std::memory_order_relaxed);
66 m_Y.store(other.m_Y.load(std::memory_order_relaxed), std::memory_order_relaxed);
67 m_Z.store(other.m_Z.load(std::memory_order_relaxed), std::memory_order_relaxed);
68 other.m_X.store(0, std::memory_order_relaxed);
69 other.m_Y.store(0, std::memory_order_relaxed);
70 other.m_Z.store(0, std::memory_order_relaxed);
71 }
72 return *this;
73 }
74
76 AVector3 operator+(const AVector3& other) const noexcept
77 {
78 return AVector3(GetX() + other.GetX(), GetY() + other.GetY(), GetZ() + other.GetZ());
79 }
80
82 AVector3& operator+=(const AVector3& other) noexcept
83 {
84 SetX(GetX() + other.GetX());
85 SetY(GetY() + other.GetY());
86 SetZ(GetZ() + other.GetZ());
87 return *this;
88 }
89
91 AVector3 operator-(const AVector3& other) const noexcept
92 {
93 return AVector3(GetX() - other.GetX(), GetY() - other.GetY(), GetZ() - other.GetZ());
94 }
95
97 AVector3& operator-=(const AVector3& other) noexcept
98 {
99 SetX(GetX() - other.GetX());
100 SetY(GetY() - other.GetY());
101 SetZ(GetZ() - other.GetZ());
102 return *this;
103 }
104
106 AVector3 operator-() const noexcept
107 {
108 return AVector3(-GetX(), -GetY(), -GetZ());
109 }
110
112 AVector3 operator*(T scalar) const noexcept
113 {
114 return AVector3(GetX() * scalar, GetY() * scalar, GetZ() * scalar);
115 }
116
118 AVector3& operator*=(T scalar) noexcept
119 {
120 SetX(GetX() * scalar);
121 SetY(GetY() * scalar);
122 SetZ(GetZ() * scalar);
123 return *this;
124 }
125
127 AVector3 operator/(T scalar) const noexcept
128 {
129 return AVector3(GetX() / scalar, GetY() / scalar, GetZ() / scalar);
130 }
131
133 AVector3& operator/=(T scalar) noexcept
134 {
135 SetX(GetX() / scalar);
136 SetY(GetY() / scalar);
137 SetZ(GetZ() / scalar);
138 return *this;
139 }
140
142 bool operator==(const AVector3& other) const noexcept
143 {
144 return GetX() == other.GetX() && GetY() == other.GetY() && GetZ() == other.GetZ();
145 }
146 bool operator!=(const AVector3& other) const noexcept
147 {
148 return !(*this == other);
149 }
150
152 T GetX() const noexcept
153 {
154 return m_X.load(std::memory_order_relaxed);
155 }
156 T GetY() const noexcept
157 {
158 return m_Y.load(std::memory_order_relaxed);
159 }
160 T GetZ() const noexcept
161 {
162 return m_Z.load(std::memory_order_relaxed);
163 }
164
166 void SetX(T x) noexcept
167 {
168 m_X.store(x, std::memory_order_relaxed);
169 }
170 void SetY(T y) noexcept
171 {
172 m_Y.store(y, std::memory_order_relaxed);
173 }
174 void SetZ(T z) noexcept
175 {
176 m_Z.store(z, std::memory_order_relaxed);
177 }
178
180
182 T Dot(const AVector3& other) const noexcept
183 {
184 return GetX() * other.GetX() + GetY() * other.GetY() + GetZ() * other.GetZ();
185 }
186
188 AVector3 Cross(const AVector3& other) const noexcept
189 {
190 return AVector3(GetY() * other.GetZ() - GetZ() * other.GetY(),
191 GetZ() * other.GetX() - GetX() * other.GetZ(),
192 GetX() * other.GetY() - GetY() * other.GetX());
193 }
194
196 T Length() const noexcept
197 {
198 return static_cast<T>(std::sqrt(GetX() * GetX() + GetY() * GetY() + GetZ() * GetZ()));
199 }
200
202 AVector3 Normalized() const noexcept
203 {
204 T len = Length();
205 if (len != 0)
206 {
207 return *this / len;
208 }
209 return AVector3();
210 }
211
212 private:
213 std::atomic<T> m_X;
214 std::atomic<T> m_Y;
215 std::atomic<T> m_Z;
216 };
217
219 template <typename T>
220 AVector3<T> operator*(T scalar, const AVector3<T>& vec) noexcept
221 {
222 return vec * scalar;
223 }
224
226 template <typename T>
227 std::ostream& operator<<(std::ostream& os, const AVector3<T>& vec)
228 {
229 os << "(" << vec.GetX() << ", " << vec.GetY() << ", " << vec.GetZ() << ")";
230 return os;
231 }
232} // namespace PixelBullet
Definition AVector3.hpp:11
AVector3 Normalized() const noexcept
Returns a normalized (unit-length) copy of the vector.
Definition AVector3.hpp:202
AVector3(const AVector3 &other) noexcept
Copy constructor.
Definition AVector3.hpp:30
AVector3 operator-(const AVector3 &other) const noexcept
Subtraction operator.
Definition AVector3.hpp:91
AVector3 & operator-=(const AVector3 &other) noexcept
Compound subtraction.
Definition AVector3.hpp:97
AVector3 operator-() const noexcept
Unary negation.
Definition AVector3.hpp:106
AVector3 Cross(const AVector3 &other) const noexcept
Cross product.
Definition AVector3.hpp:188
T GetX() const noexcept
Getters.
Definition AVector3.hpp:152
AVector3 & operator=(AVector3 &&other) noexcept
Move assignment operator.
Definition AVector3.hpp:61
AVector3(T x, T y, T z) noexcept
Parameterized constructor.
Definition AVector3.hpp:22
AVector3(AVector3 &&other) noexcept
Move constructor.
Definition AVector3.hpp:38
AVector3 & operator+=(const AVector3 &other) noexcept
Compound addition.
Definition AVector3.hpp:82
AVector3 & operator/=(T scalar) noexcept
Compound scalar division.
Definition AVector3.hpp:133
T Dot(const AVector3 &other) const noexcept
Additional operations:
Definition AVector3.hpp:182
AVector3 & operator*=(T scalar) noexcept
Compound scalar multiplication.
Definition AVector3.hpp:118
T Length() const noexcept
Returns the vector's magnitude.
Definition AVector3.hpp:196
AVector3 operator*(T scalar) const noexcept
Scalar multiplication.
Definition AVector3.hpp:112
AVector3() noexcept
Default constructor: initializes coordinates to zero.
Definition AVector3.hpp:14
AVector3 & operator=(const AVector3 &other) noexcept
Copy assignment operator.
Definition AVector3.hpp:49
AVector3 operator/(T scalar) const noexcept
Scalar division.
Definition AVector3.hpp:127
void SetX(T x) noexcept
Setters.
Definition AVector3.hpp:166
bool operator==(const AVector3 &other) const noexcept
Equality operators.
Definition AVector3.hpp:142
AVector3 operator+(const AVector3 &other) const noexcept
Addition operator.
Definition AVector3.hpp:76