SDL Breakout Engine  0.1.0
An implementation of a Breakout game using sdl, implemented with efficient resource 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 
33  float &operator[](int i)
34  {
35  return ((&x)[i]);
36  }
37 
42  const float &operator[](int i) const
43  {
44  return ((&x)[i]);
45  }
46 
51  Vector3D &operator*=(float s)
52  {
53  x *= s;
54  y *= s;
55  z *= s;
56  return (*this);
57  }
58 
62  Vector3D &operator/=(float s)
63  {
64  x /= s;
65  y /= s;
66  z /= s;
67  return (*this);
68  }
69 
77  {
78  x += v.x;
79  y += v.y;
80  z += v.z;
81  return (*this);
82  }
83 
91  {
92  x -= v.x;
93  y -= v.y;
94  z -= v.z;
95  return (*this);
96  }
97 
103  bool operator==(const Vector3D &other);
104 
111  bool equals(const Vector3D &other, float tolerance);
112 };
113 
119 static Vector3D abs(const Vector3D &other)
120 {
121  return Vector3D(fabs(other.x), fabs(other.y), fabs(other.z));
122 }
123 
131 inline bool operator<(const Vector3D &a, float scalar)
132 {
133  return a.x < scalar && a.y < scalar && a.z < scalar;
134 }
135 
142 inline float Dot(const Vector3D &a, const Vector3D &b)
143 {
144  return a.x * b.x + a.y * b.y + a.z * b.z;
145 }
146 
153 inline Vector3D operator*(const Vector3D &v, float s)
154 {
155  return Vector3D(v.x * s, v.y * s, v.z * s);
156 }
157 
165 inline Vector3D operator*(const Vector3D &v, const Vector3D &s)
166 {
167  return Vector3D(v.x * s.x, v.y * s.y, v.z * s.z);
168 }
169 
176 inline Vector3D operator/(const Vector3D &v, float s)
177 {
178  return Vector3D(v.x / s, v.y / s, v.z / s);
179 }
180 
187 inline Vector3D operator-(const Vector3D &v)
188 {
189  return Vector3D(v.x * -1, v.y * -1, v.z * -1);
190 }
191 
197 inline float Magnitude(const Vector3D &v)
198 {
199  return sqrtf32(v.x * v.x + v.y * v.y + v.z * v.z);
200 }
201 
208 inline Vector3D operator+(const Vector3D &a, const Vector3D &b)
209 {
210  return Vector3D(a.x + b.x, a.y + b.y, a.z + b.z);
211 }
212 
221 inline Vector3D operator-(const Vector3D &a, const Vector3D &b)
222 {
223  return Vector3D(a.x - b.x, a.y - b.y, a.z - b.z);
224 }
225 
233 inline Vector3D Project(const Vector3D &a, const Vector3D &b)
234 {
235  float mag = Magnitude(a);
236  return (a / mag) * (Dot(a, b) / mag);
237 }
238 
245 inline Vector3D Normalize(const Vector3D &v)
246 {
247  float mag = Magnitude(v);
248  if (mag == 0) mag = 1;
249  return v / mag;
250 }
251 
260 inline Vector3D CrossProduct(const Vector3D &a, const Vector3D &b)
261 {
262  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);
263 }
264 
265 inline bool Vector3D::operator==(const Vector3D &other)
266 {
267  return x == other.x && y == other.y && z == other.z;
268 }
269 
270 // returns if two vectors are equal within a given tolerance, to account for floating point math
271 inline bool Vector3D::equals(const Vector3D &other, float tolerance)
272 {
273  return abs(*this - other) < tolerance;
274 }
275 
279 struct Matrix3D
280 {
281 private:
282  float n[3][3];
283 
284 public:
285  Matrix3D() = default;
286 
299  Matrix3D(float n00, float n01, float n02,
300  float n10, float n11, float n12,
301  float n20, float n21, float n22)
302  {
303  n[0][0] = n00;
304  n[0][1] = n01;
305  n[0][2] = n02;
306  n[1][0] = n10;
307  n[1][1] = n11;
308  n[1][2] = n12;
309  n[2][0] = n20;
310  n[2][1] = n21;
311  n[2][2] = n22;
312  }
313 
317  Matrix3D(const Vector3D &a, const Vector3D &b, const Vector3D &c)
318  {
319 
320  n[0][0] = a.x;
321  n[0][1] = a.y;
322  n[0][2] = a.z;
323  n[1][0] = b.x;
324  n[1][1] = b.y;
325  n[1][2] = b.z;
326  n[2][0] = c.x;
327  n[2][1] = c.y;
328  n[2][2] = c.z;
329  }
330 
335  float &operator()(int i, int j)
336  {
337  return (n[i][j]);
338  }
339 
344  const float &operator()(int i, int j) const
345  {
346  return (n[i][j]);
347  }
348 
353  {
354  return (*reinterpret_cast<Vector3D *>(n[i]));
355  }
356 
360  const Vector3D &operator[](int i) const
361  {
362  return (*reinterpret_cast<const Vector3D *>(n[i]));
363  }
364 
369  {
370  return Vector3D(n[0][j], n[1][j], n[2][j]);
371  }
372 
376  Vector3D column(int j) const
377  {
378  return Vector3D(n[0][j], n[1][j], n[2][j]);
379  }
380 
384  bool operator==(const Matrix3D &other)
385  {
386  return (*this)[0] == other[0] && (*this)[1] == other[1] && (*this)[2] == other[2];
387  }
388 
392  bool equals(const Matrix3D &other, float tolerance)
393  {
394  return (*this)[0].equals(other[0], tolerance) && (*this)[1].equals(other[2], tolerance) && (*this)[1].equals(other[2], tolerance);
395  }
396 };
397 
401 inline Matrix3D operator*(const Matrix3D &A, const Matrix3D &B)
402 {
403  const Vector3D col0 = B.column(0);
404  const Vector3D col1 = B.column(1);
405  const Vector3D col2 = B.column(2);
406  return Matrix3D(Dot(A[0], col0), Dot(A[0], col1), Dot(A[0], col2),
407  Dot(A[1], col0), Dot(A[1], col1), Dot(A[1], col2),
408  Dot(A[2], col0), Dot(A[2], col1), Dot(A[2], col2));
409 }
410 
414 inline Vector3D operator*(const Matrix3D &M, const Vector3D &v)
415 {
416  return Vector3D(Dot(M[0], v), Dot(M[1], v), Dot(M[2], v));
417 }
418 
425 inline std::ostream &operator<<(std::ostream &os, const Vector3D &v)
426 {
427  os << "Vector: " << v.x << ", " << v.y << ", " << v.z;
428  return os;
429 }
430 
437 inline std::ostream &operator<<(std::ostream &os, const Matrix3D &m)
438 {
439  os << "Matrix: " << std::endl
440  << m[0] << std::endl
441  << m[1] << std::endl
442  << m[2];
443  return os;
444 }
445 
446 #endif
bool operator==(const Matrix3D &other)
Definition: TinyMath.hpp:384
const Vector3D & operator[](int i) const
Definition: TinyMath.hpp:360
float & operator ()(int i, int j)
Definition: TinyMath.hpp:335
Matrix3D(const Vector3D &a, const Vector3D &b, const Vector3D &c)
Definition: TinyMath.hpp:317
Matrix3D(float n00, float n01, float n02, float n10, float n11, float n12, float n20, float n21, float n22)
Definition: TinyMath.hpp:299
Matrix3D() = default
Default constructor.
Vector3D CrossProduct(const Vector3D &a, const Vector3D &b)
Definition: TinyMath.hpp:260
Vector3D Normalize(const Vector3D &v)
Definition: TinyMath.hpp:245
Vector3D Project(const Vector3D &a, const Vector3D &b)
Definition: TinyMath.hpp:233
Vector3D operator+(const Vector3D &a, const Vector3D &b)
Definition: TinyMath.hpp:208
float Magnitude(const Vector3D &v)
Definition: TinyMath.hpp:197
Vector3D column(int j) const
Definition: TinyMath.hpp:376
Vector3D operator-(const Vector3D &a, const Vector3D &b)
Definition: TinyMath.hpp:221
Vector3D operator/(const Vector3D &v, float s)
Definition: TinyMath.hpp:176
float Dot(const Vector3D &a, const Vector3D &b)
Definition: TinyMath.hpp:142
bool operator<(const Vector3D &a, float scalar)
Definition: TinyMath.hpp:131
bool equals(const Matrix3D &other, float tolerance)
Definition: TinyMath.hpp:392
static Vector3D abs(const Vector3D &other)
Definition: TinyMath.hpp:119
bool equals(const Vector3D &other, float tolerance)
Definition: TinyMath.hpp:271
bool operator==(const Vector3D &other)
Definition: TinyMath.hpp:265
Vector3D operator*(const Matrix3D &M, const Vector3D &v)
Definition: TinyMath.hpp:414
Vector3D & operator-=(const Vector3D &v)
Definition: TinyMath.hpp:90
static ostream & operator<<(ostream &os, const Level &l)
Definition: LevelManager.cpp:9
Vector3D & operator+=(const Vector3D &v)
Definition: TinyMath.hpp:76
Vector3D column(int j)
Definition: TinyMath.hpp:368
Vector3D & operator*=(float s)
Definition: TinyMath.hpp:51
Vector3D & operator[](int i)
Definition: TinyMath.hpp:352
const float & operator[](int i) const
Definition: TinyMath.hpp:42
Vector3D & operator/=(float s)
Definition: TinyMath.hpp:62
float & operator[](int i)
Definition: TinyMath.hpp:33
Vector3D(float a, float b, float c)
Definition: TinyMath.hpp:27
Vector3D() = default
Default constructor.
float y
Definition: TinyMath.hpp:19
float x
Definition: TinyMath.hpp:19
Definition: TinyMath.hpp:279
float z
Definition: TinyMath.hpp:19
Definition: TinyMath.hpp:15