-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGT_MathGlobal.h
148 lines (120 loc) · 3.07 KB
/
GT_MathGlobal.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
#ifndef GT_MATHGLOBAL_H
#define GT_MATHGLOBAL_H
// Global math defines/macros/functions/classes to be used by everything
#pragma once
#include <stdlib.h>
#include <assert.h>
#include <cmath>
//////////////////////
// Constant Values //
////////////////////
const float kPi = 3.14159265f;
const float k2Pi = kPi * 2.0f;
const float kHalfPi = kPi / 2.0f;
const float k1OverPi = 1.0f / kPi;
const float k1Over2Pi = 1.0f / k2Pi;
const float kPiOver180 = kPi / 180.0f;
const float k180OverPi = 180.0f / kPi;
/////////////
// Marcos //
///////////
#define DEG2RAD(x) (x * kPiOver180) // Converts degrees to radians
#define RAD2DEG(x) (x * k180OverPi) // Converts radians to degrees
#define FLT2INT(x) ( (int)(x + 0.5f) )
// Returns a random percent between 0 - 1
#define RAND_PERCENT() ((rand() & 0x7FFF) / ((float)0x7FFF))
////////////////
// Functions //
//////////////
// Checks for two variables to be equal within a certain tolerance
template <typename type>
inline bool Equivalent(type a, type b, type t)
{
return ((a > b - t) && (a < b + t));
}
// Returns a random number between and including min and max
// Assumes generic case to be integer "min" and "max" input parameters
template <typename type>
type Rand(type min, type max)
{
assert(min < max); // Make sure min is less than max
return (type)(min + (max - min) * RAND_PERCENT() + 0.5f);
}
// Exlipict specilization of Rand() for floats
template <>
inline float Rand<float>(float min, float max)
{
assert(min < max); // Make sure min is less than max
return (min + (max - min) * RAND_PERCENT());
}
// Exlipict specilization of Rand() for doubles
template <>
inline double Rand<double>(double min, double max)
{
assert(min < max); // Make sure min is less than max
return (min + (max - min) * RAND_PERCENT());
}
//////////////
// Classes //
////////////
// Class for oscillating a number between a "min" and a "max"
template <typename type>
class COscillator
{
public:
COscillator() // Default constructor
{
memset(&mMin, 0, sizeof(type));
memset(&mMax, 0, sizeof(type));
memset(&mVal, 0, sizeof(type));
memset(&mIncAmt, 0, sizeof(type));
}
COscillator(type min, type max, type incAmt)
{
setMinMax(min, max);
setIncAmt(incAmt);
mVal = mMin + (mMax - mMin) * 0.5f; // Set to center of (max,min)
}
// Update the oscillator
void update()
{
mVal += mIncAmt;
if(mVal > mMax)
{
mVal = mMax;
mIncAmt = -mIncAmt;
}
else if(mVal < mMin)
{
mVal = mMin;
mIncAmt = -mIncAmt;
}
}
// Update the oscillator with respect to time
void update(float dt)
{
mVal += mIncAmt * dt;
if(mVal > mMax)
{
mVal = mMax;
mIncAmt = -mIncAmt;
}
else if(mVal < mMin)
{
mVal = mMin;
mIncAmt = -mIncAmt;
}
}
void setMinMax(type min, type max) { mMin = min; mMax = max; }
void setIncAmt(type incAmt) { mIncAmt = incAmt; }
void setVal(type val) { mVal = val; } // Set current value
// Data Access ***
type getVal() { return mVal; } // Get current value
// *** End Data Access
private:
type mMin;
type mMax;
type mVal;
type mIncAmt;
};
#endif