-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.h
264 lines (217 loc) · 6.92 KB
/
vector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// -----------------------------------------------------------------
// Learning Team B
// Members:
// Adam LeMmon
// Faith Satterthwaite
// Tom Fletcher
// Justin Ball
// CS 4280 – 11:30 am
// Final Project
// Dr. Rague
// Due: 12/06/12
// Version: 2.4
// -----------------------------------------------------------------
// We made five major improvements to this game
// 1) New controls
// 2) Enemy attack
// 3) HUD (heads up display)
// 4) Enemy health bars
// 5) New Weapon
// -----------------------------------------------------------------
#ifndef __VECTOR_H
#define __VECTOR_H
#include <math.h>
/*
VECTOR.H
CVector class
*/
#define PI (3.14159265359f) //Useful constant and conversions
#define DEG2RAD(a) (PI/180*(a))
#define RAD2DEG(a) (180/PI*(a))
typedef float scalar_t;
class CVector
{
public:
union //Storage for x(=v[0]), y(=v[1]), z(=v[2])
{
struct
{
scalar_t x;
scalar_t y;
scalar_t z; // x,y,z coordinates
};
scalar_t v[3];
};
public:
CVector(scalar_t a = 0, scalar_t b = 0, scalar_t c = 0) : x(a), y(b), z(c) {}
CVector(const CVector &vec) : x(vec.x), y(vec.y), z(vec.z) {}
// vector index
scalar_t &operator[](const long idx) //operator for vec[0], vec[1], vec[2]
{
return *((&x)+idx);
}
// vector assignment
const CVector &operator=(const CVector &vec) // operator for vec1 = vec2
{
x = vec.x;
y = vec.y;
z = vec.z;
return *this;
}
// vecector equality
const bool operator==(const CVector &vec) const // operator for vec1 == vec2
{
return ((x == vec.x) && (y == vec.y) && (z == vec.z));
}
// vecector inequality
const bool operator!=(const CVector &vec) const // operator for vec1 != vec2
{
return !(*this == vec);
}
// vector add
const CVector operator+(const CVector &vec) const // operator for vec1 + vec2
{
return CVector(x + vec.x, y + vec.y, z + vec.z);
}
// vector add (opposite of negation) // unary operator +vec1
const CVector operator+() const
{
return CVector(*this);
}
// vector increment // operator for vec1 += vec2
const CVector& operator+=(const CVector& vec)
{ x += vec.x;
y += vec.y;
z += vec.z;
return *this;
}
// vector subtraction
const CVector operator-(const CVector& vec) const //operator for vec1 - vec2
{
return CVector(x - vec.x, y - vec.y, z - vec.z);
}
// vector negation
const CVector operator-() const // unary operator -vec1
{
return CVector(-x, -y, -z);
}
// vector decrement
const CVector &operator-=(const CVector& vec) // operator for vec1 -= vec2
{
x -= vec.x;
y -= vec.y;
z -= vec.z;
return *this;
}
// scalar self-multiply
const CVector &operator*=(const scalar_t &s) // scaling operator vec1 *= scalar
{
x *= s;
y *= s;
z *= s;
return *this;
}
// scalar self-divecide
const CVector &operator/=(const scalar_t &s) // scaling operator vec1 /= scalar
{
const float recip = 1/s; // for speed, one divecision
x *= recip;
y *= recip;
z *= recip;
return *this;
}
// post multiply by scalar
const CVector operator*(const scalar_t &s) const // scaling operator vec1 * scalar
{
return CVector(x*s, y*s, z*s);
}
// pre multiply by scalar
friend inline const CVector operator*(const scalar_t &s, const CVector &vec)
{
return vec*s; // handles scalar * vec
}
const CVector operator*(const CVector& vec) const
{
return CVector(x*vec.x, y*vec.y, z*vec.z); // operator vec1 * vec2
}
// post multiply by scalar
/*friend inline const CVector operator*(const CVector &vec, const scalar_t &s)
{
return CVector(vec.x*s, vec.y*s, vec.z*s);
}*/
// divide by scalar
const CVector operator/(scalar_t s) const // scaling vec1 / scalar
{
s = 1/s;
return CVector(s*x, s*y, s*z);
}
// cross product
const CVector CrossProduct(const CVector &vec) const // vec1 X vec2
{
return CVector(y*vec.z - z*vec.y, z*vec.x - x*vec.z, x*vec.y - y*vec.x);
}
// cross product
const CVector operator^(const CVector &vec) const // vec1 ^ vec2 is Cross product
{
return CVector(y*vec.z - z*vec.y, z*vec.x - x*vec.z, x*vec.y - y*vec.x);
}
// dot product
const scalar_t DotProduct(const CVector &vec) const // vec1 <dot> vec2
{
return x*vec.x + y*vec.y + z*vec.z;
}
// dot product
const scalar_t operator%(const CVector &vec) const // vec1 % vec2 is Dot product
{
return x*vec.x + y*vec.y + z*vec.z;
}
// length of vector
const scalar_t Length() const // Length of vector
{
return (scalar_t)sqrt((double)(x*x + y*y + z*z));
}
// return the unit vector
const CVector UnitVector() const
{
return (*this) / Length();
}
// normalize this vector
void Normalize()
{
(*this) /= Length();
}
const scalar_t operator!() const // !vec is float magnitude
{
return sqrtf(x*x + y*y + z*z);
}
// return vector with specified length
const CVector operator | (const scalar_t length) const
{
return *this * (length / !(*this));
}
// set length of vector equal to length
const CVector& operator |= (const float length)
{
return *this = *this | length;
}
// return angle between two vectors
const float inline Angle(const CVector& normal) const
{
return acosf(*this % normal);
}
// reflect this vector off surface with normal vector
const CVector inline Reflection(const CVector& normal) const
{
const CVector vec(*this | 1); // normalize this vector
return (vec - normal * 2.0 * (vec % normal)) * !*this;
}
// rotate angle degrees about a normal
const CVector inline Rotate(const float angle, const CVector& normal) const
{
const float cosine = cosf(angle);
const float sine = sinf(angle);
return CVector(*this * cosine + ((normal * *this) * (1.0f - cosine)) *
normal + (*this ^ normal) * sine);
}
};
#endif