Copperfield Engine 0.1
C++ Game Engine
Loading...
Searching...
No Matches
vector_3.h
1#ifndef __VEC3_H__
2#define __VEC3_H__ 1
3
4#include <math.h>
5
6#include "math/math_utils.h"
7
9namespace coma {
10
12class Vec3 {
13 public:
14 Vec3();
15 Vec3(float value);
16 Vec3(float x, float y, float z);
17 Vec3(float* values_array);
18 Vec3(const Vec3& other);
19 ~Vec3();
21 Vec3 operator+(const Vec3& other) const;
23 Vec3 operator+(float value) const;
25 Vec3& operator+=(const Vec3& other);
27 Vec3& operator+=(float value);
29 Vec3 operator-(const Vec3& other) const;
31 Vec3 operator-(float value) const;
33 Vec3& operator-=(const Vec3& other);
35 Vec3& operator-=(float value);
37 bool operator==(const Vec3& other) const;
39 bool operator!=(const Vec3& other) const;
41 void operator=(const Vec3& other);
43 void operator=(float value);
45 Vec3 operator*(float value) const;
47 Vec3& operator*=(float value);
49 Vec3 operator/(float value) const;
51 Vec3& operator/=(float value);
52
55 float Magnitude() const;
58 Vec3 Normalized() const;
60 void Normalize();
63 float SqrMagnitude() const;
66 void Scale(const Vec3& other);
67
73 static Vec3 Lerp(const Vec3& a, const Vec3& b, float t);
79 static Vec3 LerpUnclamped(const Vec3& a, const Vec3& b, float t);
84 static float DotProduct(const Vec3& a, const Vec3& b);
89 static float Angle(const Vec3& a, const Vec3& b);
94 static Vec3 CrossProduct(const Vec3& a, const Vec3& b);
99 static float Distance(const Vec3& a, const Vec3& b);
104 static Vec3 Reflect(const Vec3& direction, const Vec3& normal);
108 static Vec3 eulerToForward(const Vec3& a);
109
114 static Vec3 forwardToEuler(const Vec3& forward, const Vec3& up);
115
117 static const Vec3 up;
119 static const Vec3 down;
121 static const Vec3 right;
123 static const Vec3 left;
125 static const Vec3 forward;
127 static const Vec3 back;
129 static const Vec3 zero;
131 static const Vec3 unit;
132
134 float x;
136 float y;
138 float z;
139};
140
141inline float Vec3::Magnitude() const { return sqrtf(x * x + y * y + z * z); }
142
143inline void Vec3::Normalize() { *this /= Magnitude(); }
144
145inline Vec3 Vec3::Normalized() const { return Vec3(*this / Magnitude()); }
146
147inline float Vec3::DotProduct(const Vec3& a, const Vec3& b) {
148 return a.x * b.x + a.y * b.y + a.z * b.z;
149}
150
151inline float Vec3::Angle(const Vec3& a, const Vec3& b) {
152 return acosf(DotProduct(a, b) / (a.Magnitude() * b.Magnitude()));
153}
154
155inline Vec3 Vec3::CrossProduct(const Vec3& a, const Vec3& b) {
156 return Vec3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z,
157 a.x * b.y - a.y * b.x);
158}
159
160inline float Vec3::SqrMagnitude() const { return x * x + y * y + z * z; }
161
162inline void Vec3::Scale(const Vec3& other) {
163 x *= other.x;
164 y *= other.y;
165 z *= other.z;
166}
167
168inline Vec3 Vec3::Lerp(const Vec3& a, const Vec3& b, float t) {
169 t = t > 1 ? 1 : t;
170 t = t < 0 ? 0 : t;
171 return (b - a) * t + a;
172}
173
174inline Vec3 Vec3::LerpUnclamped(const Vec3& a, const Vec3& b, float t) {
175 return (b - a) * t + a;
176}
177
178inline float Vec3::Distance(const Vec3& a, const Vec3& b) {
179 return (b - a).Magnitude();
180}
181
182inline Vec3 Vec3::Reflect(const Vec3& direction, const Vec3& normal) {
183 Vec3 nn = normal.Normalized();
184 return direction - nn * 2 * DotProduct(direction, nn);
185}
186
188 float yaw = Radians(a.y + 90.0f);
189 float pitch = Radians(a.x);
190 Vec3 forward_v;
191 forward_v.x = cosf(yaw) * cosf(pitch);
192 forward_v.y = sinf(pitch);
193 forward_v.z = sinf(yaw) * cosf(pitch);
194 forward_v.Normalize();
195 return forward_v;
196}
197
198inline Vec3 Vec3::forwardToEuler(const Vec3& fron, const Vec3& upp = Vec3::up) {
199 float yaw = atan2f(fron.y, fron.x);
200 float pitch = asinf(fron.z);
201
202 Vec3 W0 = Vec3(-fron.y, fron.x, 0);
203 Vec3 U0 = Vec3::CrossProduct(W0, fron);
204 float roll = atan2f( Vec3::DotProduct(W0,upp) / W0.Magnitude(), Vec3::DotProduct(U0,upp) / U0.Magnitude());
205 return Vec3(yaw, pitch, roll);
206}
207
208inline Vec3 Vec3::operator+(const Vec3& other) const {
209 return Vec3(x + other.x, y + other.y, z + other.z);
210}
211
212inline Vec3 Vec3::operator+(float value) const {
213 return Vec3(x + value, y + value, z + value);
214}
215
216inline Vec3& Vec3::operator+=(const Vec3& other) {
217 x += other.x;
218 y += other.y;
219 z += other.z;
220 return *this;
221}
222
223inline Vec3& Vec3::operator+=(float value) {
224 x += value;
225 y += value;
226 z += value;
227 return *this;
228}
229
230inline Vec3 Vec3::operator-(const Vec3& other) const {
231 return Vec3(x - other.x, y - other.y, z - other.z);
232}
233
234inline Vec3 Vec3::operator-(float value) const {
235 return Vec3(x - value, y - value, z - value);
236}
237
238inline Vec3& Vec3::operator-=(const Vec3& other) {
239 x -= other.x;
240 y -= other.y;
241 z -= other.z;
242 return *this;
243}
244
245inline Vec3& Vec3::operator-=(float value) {
246 x -= value;
247 y -= value;
248 z -= value;
249 return *this;
250}
251
252inline bool Vec3::operator==(const Vec3& other) const {
253 if (x == other.x && y == other.y && z == other.z) return true;
254 return false;
255}
256
257inline bool Vec3::operator!=(const Vec3& other) const {
258 if (x == other.x && y == other.y && z == other.z) return false;
259 return true;
260}
261
262inline void Vec3::operator=(const Vec3& other) {
263 x = other.x;
264 y = other.y;
265 z = other.z;
266}
267
268inline void Vec3::operator=(float value) {
269 x = value;
270 y = value;
271 z = value;
272}
273
274inline Vec3 Vec3::operator*(float value) const {
275 return Vec3(x * value, y * value, z * value);
276}
277
278inline Vec3& Vec3::operator*=(float value) {
279 x *= value;
280 y *= value;
281 z *= value;
282 return *this;
283}
284
285inline Vec3 Vec3::operator/(float value) const {
286 return Vec3(x / value, y / value, z / value);
287}
288
289inline Vec3& Vec3::operator/=(float value) {
290 x /= value;
291 y /= value;
292 z /= value;
293 return *this;
294}
295} // namespace coma
296#endif
represents mathematical vector with 3 components
Definition: vector_3.h:12
static const Vec3 up
Vec3(0, 1, 0)
Definition: vector_3.h:117
static const Vec3 right
Vec3(1, 0, 0)
Definition: vector_3.h:121
static const Vec3 left
Vec3(-1, 0, 0)
Definition: vector_3.h:123
static Vec3 eulerToForward(const Vec3 &a)
Definition: vector_3.h:187
static const Vec3 down
Vec3(0, -1, 0)
Definition: vector_3.h:119
static float Distance(const Vec3 &a, const Vec3 &b)
Definition: vector_3.h:178
static Vec3 forwardToEuler(const Vec3 &forward, const Vec3 &up)
Definition: vector_3.h:198
static Vec3 Reflect(const Vec3 &direction, const Vec3 &normal)
Definition: vector_3.h:182
float SqrMagnitude() const
Definition: vector_3.h:160
static const Vec3 zero
Vec3(0, 0, 0)
Definition: vector_3.h:129
static float Angle(const Vec3 &a, const Vec3 &b)
Definition: vector_3.h:151
static Vec3 Lerp(const Vec3 &a, const Vec3 &b, float t)
Definition: vector_3.h:168
void Scale(const Vec3 &other)
Definition: vector_3.h:162
static const Vec3 back
Vec3(0, 0, -1)
Definition: vector_3.h:127
static Vec3 LerpUnclamped(const Vec3 &a, const Vec3 &b, float t)
Definition: vector_3.h:174
static Vec3 CrossProduct(const Vec3 &a, const Vec3 &b)
Definition: vector_3.h:155
float Magnitude() const
Definition: vector_3.h:141
static float DotProduct(const Vec3 &a, const Vec3 &b)
Definition: vector_3.h:147
static const Vec3 unit
Vec3(1, 1, 1)
Definition: vector_3.h:131
static const Vec3 forward
Vec3(0, 0, 1)
Definition: vector_3.h:125
Vec3 Normalized() const
Definition: vector_3.h:145
copperdielf Math Library
Definition: buffer.hpp:5