Level H Engine
Vec3.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "math.h"
4 
8 struct Vec3
9 {
11  float x, y, z;
12 
16  Vec3() : x(0.0f), y(0.0f), z(0.0f) {}
17 
24  Vec3(float x, float y, float z) : x(x), y(y), z(z) {}
25 
32  Vec3(int x, int y, int z) : x((float)x), y((float)y), z((float)z) {}
33 
40  {
41  x += vecIn.x;
42  y += vecIn.y;
43  z += vecIn.y;
44  return this;
45  }
46 
53  {
54  x -= vecIn.x;
55  y -= vecIn.y;
56  z -= vecIn.z;
57  return this;
58  }
59 
64  float getLength()
65  {
66  return float(sqrt((x*x) + (y*y) + (z*z)));
67  }
68 
74  {
75  float magnitude = getLength();
76  Vec3 normalised = Vec3(x / magnitude, y / magnitude, z / magnitude);
77  return normalised;
78  }
79 };
80 
86 inline Vec3 operator - (Vec3 vecIn)
87 {
88  Vec3 vecOut;
89  vecOut.x = -vecIn.x;
90  vecOut.y = -vecIn.y;
91  vecOut.z = -vecIn.z;
92  return vecOut;
93 }
94 
101 inline Vec3 operator - (Vec3 vecInA, Vec3 vecInB)
102 {
103  Vec3 vecOut;
104  vecOut.x = vecInA.x - vecInB.x;
105  vecOut.y = vecInA.y - vecInB.y;
106  vecOut.z = vecInA.z - vecInB.z;
107  return vecOut;
108 }
109 
116 inline Vec3 operator + (Vec3 vecInA, Vec3 vecInB)
117 {
118  Vec3 vecOut;
119  vecOut.x = vecInA.x + vecInB.x;
120  vecOut.y = vecInA.y + vecInB.y;
121  vecOut.z = vecInA.z + vecInB.z;
122  return vecOut;
123 }
124 
131 inline Vec3 operator / (Vec3 vecInA, float scalar)
132 {
133  Vec3 vecOut;
134  vecOut.x = vecInA.x / scalar;
135  vecOut.y = vecInA.y / scalar;
136  vecOut.z = vecInA.z / scalar;
137  return vecOut;
138 }
139 
146 inline Vec3 operator * (Vec3 vecInA, float scalar)
147 {
148  Vec3 vecOut;
149  vecOut.x = vecInA.x * scalar;
150  vecOut.y = vecInA.y * scalar;
151  vecOut.z = vecInA.z * scalar;
152  return vecOut;
153 }
154 
161 inline Vec3 operator * (Vec3 vecInA, Vec3 vecInB)
162 {
163  Vec3 vecOut;
164  vecOut.x = vecInA.x * vecInB.x;
165  vecOut.y = vecInA.y * vecInB.y;
166  vecOut.z = vecInA.z * vecInB.z;
167  return vecOut;
168 }
float y
Definition: Vec3.h:11
Vec3 operator-(Vec3 vecIn)
Overloads the - operator allowing a Vec3 to be inverted.
Definition: Vec3.h:86
float x
Position variables.
Definition: Vec3.h:11
Vec3(float x, float y, float z)
Constructs the Vec3 setting the values to the input coordinates.
Definition: Vec3.h:24
Vec3 getNormalised()
Returns a normalised version of the Vec3.
Definition: Vec3.h:73
Vec3 * operator+=(Vec3 vecIn)
Overloads the += operator.
Definition: Vec3.h:39
Vec3 operator+(Vec3 vecInA, Vec3 vecInB)
Overloads the + operator.
Definition: Vec3.h:116
Vec3(int x, int y, int z)
Constructs the Vec3 setting the values to the input coordinates.
Definition: Vec3.h:32
Contains the Vec3 structure with functions and overloaded operators.
Definition: Vec3.h:8
float getLength()
Returns the length of the Vec3.
Definition: Vec3.h:64
Vec3()
Constructs the Vec3 setting the values to 0,0,0.
Definition: Vec3.h:16
float z
Definition: Vec3.h:11
Vec3 * operator-=(Vec3 vecIn)
Overloads the -= operator.
Definition: Vec3.h:52
Vec3 operator/(Vec3 vecInA, float scalar)
Overloads the / operator allowing a Vec3 to be divided by a scalar.
Definition: Vec3.h:131
Vec3 operator*(Vec3 vecInA, float scalar)
Overloads the * operator allowing a Vec3 to be multiplied by a scalar.
Definition: Vec3.h:146