SDL SPOOKY PLATFORMER ENGINE  0.2.0
An implementation of a platformer using sdl, implemented using an entity component system and efficient collision management.
TinyMath.hpp
Go to the documentation of this file.
1 #ifndef TINYMATH_H
2 #define TINYMATH_H
3 
4 #include <cmath>
5 #include <iostream>
6 
7 // Forward references of each of the structs
8 struct Vector3D;
9 struct Matrix3D;
10 
15 struct Vector3D
16 {
18 
19  float x, y, z;
21 
22  Vector3D() = default;
23 
27  Vector3D(float a, float b, float c) : x(a), y(b), z(c) {}
28 
29  Vector3D(float a, float b) : x(a), y(b), z(0) {}
30 
35  float &operator[](int i)
36  {
37  return ((&x)[i]);
38  }
39 
44  const float &operator[](int i) const
45  {
46  return ((&x)[i]);
47  }
48 
53  Vector3D &operator*=(float s)
54  {
55  x *= s;
56  y *= s;
57  z *= s;
58  return (*this);
59  }
60 
64  Vector3D &operator/=(float s)
65  {
66  x /= s;
67  y /= s;
68  z /= s;
69  return (*this);
70  }
71 
79  {
80  x += v.x;
81  y += v.y;
82  z += v.z;
83  return (*this);
84  }
85 
93  {
94  x -= v.x;
95  y -= v.y;
96  z -= v.z;
97  return (*this);
98  }
99 
105  bool operator==(const Vector3D &other);
106 
113  bool equals(const Vector3D &other, float tolerance);
114 };
115 
121 static Vector3D abs(const Vector3D &other)
122 {
123  return Vector3D(fabs(other.x), fabs(other.y), fabs(other.z));
124 }
125 
133 inline bool operator<(const Vector3D &a, float scalar)
134 {
135  return a.x < scalar && a.y < scalar && a.z < scalar;
136 }
137 
144 inline float Dot(const Vector3D &a, const Vector3D &b)
145 {
146  return a.x * b.x + a.y * b.y + a.z * b.z;
147 }
148 
155 inline Vector3D operator*(const Vector3D &v, float s)
156 {
157  return Vector3D(v.x * s, v.y * s, v.z * s);
158 }
159 
167 inline Vector3D operator*(const Vector3D &v, const Vector3D &s)
168 {
169  return Vector3D(v.x * s.x, v.y * s.y, v.z * s.z);
170 }
171 
178 inline Vector3D operator/(const Vector3D &v, float s)
179 {
180  return Vector3D(v.x / s, v.y / s, v.z / s);
181 }
182 
189 inline Vector3D operator-(const Vector3D &v)
190 {
191  return Vector3D(v.x * -1, v.y * -1, v.z * -1);
192 }
193 
199 inline float Magnitude(const Vector3D &v)
200 {
201  return sqrtf32(v.x * v.x + v.y * v.y + v.z * v.z);
202 }
203 
210 inline Vector3D operator+(const Vector3D &a, const Vector3D &b)
211 {
212  return Vector3D(a.x + b.x, a.y + b.y, a.z + b.z);
213 }
214 
223 inline Vector3D operator-(const Vector3D &a, const Vector3D &b)
224 {
225  return Vector3D(a.x - b.x, a.y - b.y, a.z - b.z);
226 }
227 
235 inline Vector3D Project(const Vector3D &a, const Vector3D &b)
236 {
237  float mag = Magnitude(a);
238  return (a / mag) * (Dot(a, b) / mag);
239 }
240 
247 inline Vector3D Normalize(const Vector3D &v)
248 {
249  float mag = Magnitude(v);
250  if (mag == 0) mag = 1;
251  return v / mag;
252 }
253 
262 inline Vector3D CrossProduct(const Vector3D &a, const Vector3D &b)
263 {
264  return Vector3D(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
265 }
266 
267 inline bool Vector3D::operator==(const Vector3D &other)
268 {
269  return x == other.x && y == other.y && z == other.z;
270 }
271 
272 // returns if two vectors are equal within a given tolerance, to account for floating point math
273 inline bool Vector3D::equals(const Vector3D &other, float tolerance)
274 {
275  return abs(*this - other) < tolerance;
276 }
277 
281 struct Matrix3D
282 {
283 private:
284  float n[3][3];
285 
286 public:
287  Matrix3D() = default;
288 
301  Matrix3D(float n00, float n01, float n02,
302  float n10, float n11, float n12,
303  float n20, float n21, float n22)
304  {
305  n[0][0] = n00;
306  n[0][1] = n01;
307  n[0][2] = n02;
308  n[1][0] = n10;
309  n[1][1] = n11;
310  n[1][2] = n12;
311  n[2][0] = n20;
312  n[2][1] = n21;
313  n[2][2] = n22;
314  }
315 
319  Matrix3D(const Vector3D &a, const Vector3D &b, const Vector3D &c)
320  {
321 
322  n[0][0] = a.x;
323  n[0][1] = a.y;
324  n[0][2] = a.z;
325  n[1][0] = b.x;
326  n[1][1] = b.y;
327  n[1][2] = b.z;
328  n[2][0] = c.x;
329  n[2][1] = c.y;
330  n[2][2] = c.z;
331  }
332 
337  float &operator()(int i, int j)
338  {
339  return (n[i][j]);
340  }
341 
346  const float &operator()(int i, int j) const
347  {
348  return (n[i][j]);
349  }
350 
355  {
356  return (*reinterpret_cast<Vector3D *>(n[i]));
357  }
358 
362  const Vector3D &operator[](int i) const
363  {
364  return (*reinterpret_cast<const Vector3D *>(n[i]));
365  }
366 
371  {
372  return Vector3D(n[0][j], n[1][j], n[2][j]);
373  }
374 
378  Vector3D column(int j) const
379  {
380  return Vector3D(n[0][j], n[1][j], n[2][j]);
381  }
382 
386  bool operator==(const Matrix3D &other)
387  {
388  return (*this)[0] == other[0] && (*this)[1] == other[1] && (*this)[2] == other[2];
389  }
390 
394  bool equals(const Matrix3D &other, float tolerance)
395  {
396  return (*this)[0].equals(other[0], tolerance) && (*this)[1].equals(other[2], tolerance) && (*this)[1].equals(other[2], tolerance);
397  }
398 };
399 
403 inline Matrix3D operator*(const Matrix3D &A, const Matrix3D &B)
404 {
405  const Vector3D col0 = B.column(0);
406  const Vector3D col1 = B.column(1);
407  const Vector3D col2 = B.column(2);
408  return Matrix3D(Dot(A[0], col0), Dot(A[0], col1), Dot(A[0], col2),
409  Dot(A[1], col0), Dot(A[1], col1), Dot(A[1], col2),
410  Dot(A[2], col0), Dot(A[2], col1), Dot(A[2], col2));
411 }
412 
416 inline Vector3D operator*(const Matrix3D &M, const Vector3D &v)
417 {
418  return Vector3D(Dot(M[0], v), Dot(M[1], v), Dot(M[2], v));
419 }
420 
427 inline std::ostream &operator<<(std::ostream &os, const Vector3D &v)
428 {
429  os << "Vector: " << v.x << ", " << v.y << ", " << v.z;
430  return os;
431 }
432 
439 inline std::ostream &operator<<(std::ostream &os, const Matrix3D &m)
440 {
441  os << "Matrix: " << std::endl
442  << m[0] << std::endl
443  << m[1] << std::endl
444  << m[2];
445  return os;
446 }
447 
448 #endif
bool equals(const Matrix3D &other, float tolerance)
Definition: TinyMath.hpp:394
bool operator==(const Matrix3D &other)
Definition: TinyMath.hpp:386
Vector3D column(int j)
Definition: TinyMath.hpp:370
float & operator ()(int i, int j)
Definition: TinyMath.hpp:337
Matrix3D(float n00, float n01, float n02, float n10, float n11, float n12, float n20, float n21, float n22)
Definition: TinyMath.hpp:301
Matrix3D() = default
Default constructor.
Matrix3D(const Vector3D &a, const Vector3D &b, const Vector3D &c)
Definition: TinyMath.hpp:319
Vector3D CrossProduct(const Vector3D &a, const Vector3D &b)
Definition: TinyMath.hpp:262
const Vector3D & operator[](int i) const
Definition: TinyMath.hpp:362
Vector3D Normalize(const Vector3D &v)
Definition: TinyMath.hpp:247
Vector3D operator+(const Vector3D &a, const Vector3D &b)
Definition: TinyMath.hpp:210
float Magnitude(const Vector3D &v)
Definition: TinyMath.hpp:199
Vector3D operator-(const Vector3D &a, const Vector3D &b)
Definition: TinyMath.hpp:223
Vector3D column(int j) const
Definition: TinyMath.hpp:378
Vector3D operator/(const Vector3D &v, float s)
Definition: TinyMath.hpp:178
Vector3D operator*(const Matrix3D &M, const Vector3D &v)
Definition: TinyMath.hpp:416
float Dot(const Vector3D &a, const Vector3D &b)
Definition: TinyMath.hpp:144
Vector3D & operator[](int i)
Definition: TinyMath.hpp:354
bool operator<(const Vector3D &a, float scalar)
Definition: TinyMath.hpp:133
bool equals(const Vector3D &other, float tolerance)
Definition: TinyMath.hpp:273
bool operator==(const Vector3D &other)
Definition: TinyMath.hpp:267
static Vector3D abs(const Vector3D &other)
Definition: TinyMath.hpp:121
Vector3D & operator-=(const Vector3D &v)
Definition: TinyMath.hpp:92
Vector3D & operator+=(const Vector3D &v)
Definition: TinyMath.hpp:78
Vector3D & operator/=(float s)
Definition: TinyMath.hpp:64
Vector3D & operator*=(float s)
Definition: TinyMath.hpp:53
const float & operator[](int i) const
Definition: TinyMath.hpp:44
Vector3D Project(const Vector3D &a, const Vector3D &b)
Definition: TinyMath.hpp:235
float & operator[](int i)
Definition: TinyMath.hpp:35
Vector3D(float a, float b)
Definition: TinyMath.hpp:29
Vector3D(float a, float b, float c)
Definition: TinyMath.hpp:27
std::ostream & operator<<(std::ostream &os, const Matrix3D &m)
Definition: TinyMath.hpp:439
Vector3D() = default
Default constructor.
float z
Definition: TinyMath.hpp:19
float y
Definition: TinyMath.hpp:19
float x
Definition: TinyMath.hpp:19
Definition: TinyMath.hpp:281
Definition: TinyMath.hpp:15