-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmath.qc
228 lines (189 loc) · 4.58 KB
/
math.qc
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
/*
* math.qc
*
* Author: Joshua Skelton joshua.skelton@gmail.com
*
* A collection of helpful math functions.
*/
// Forward declarations
float(float value, float minValue, float maxValue) clamp;
float(float a, float b) mod;
float(float x) sign;
float(float value, float minValue, float maxValue) wrap;
/*
* clamp
*
* Limits the given value to the given range.
*
* value: A number
*
* minValue: The minimum value of the range
*
* maxValue: The maximum value of the range
*
* Returns: A number within the given range.
*/
float(float value, float minValue, float maxValue) clamp = {
if (value < minValue) {
return minValue;
}
else if (value > maxValue) {
return maxValue;
}
return value;
};
/*
* mod
*
* Returns the remainder after the division of a by n
*
* a: The dividend
*
* b: The divisor
*
* Returns: The remainder of a divided by n
*/
float(float a, float n) mod = {
return a - (n * floor(a / n));
};
/*
* sign
*
* Returns an indication of the sign of the given number.
*
* x: A number
*
* Returns: -1 if x < 0, 0 if x == 0, 1 if x > 0.
*/
float(float x) sign = {
if (x > 0) {
return 1;
}
else if (x < 0) {
return -1;
}
return 0;
};
/*
* wrap
*
* Limits the given value to the given range and will wrap the value to the
* the other end of the range if exceeded.
*
* value: A number
*
* minValue: The minimum value of the range
*
* maxValue: The maximum value of the range
*
* Returns: A number within the given range.
*/
float(float value, float minValue, float maxValue) wrap = {
local float range = maxValue - minValue;
return mod(value - minValue, range + 1) + minValue;
};
float(float a, float b, float mix) lerp =
{
if (mix <= 0) return a;
if (mix >= 1) return b;
return (b * mix + a * ( 1 - mix ) );
}
vector(vector a, vector b, float mix) lerpVector =
{
if (mix <= 0) return a;
if (mix >= 1) return b;
return (b * mix + a * ( 1 - mix ) );
}
// for a relaxing lerp: hermite lerp.
float(float a, float b, float mix) lerpHermite =
{
if (mix <= 0) return a;
if (mix >= 1) return b;
local float h01;
h01 = mix * mix;
h01 *= 3 - 2 * mix;
return (b * h01 + a * ( 1 - h01 ) );
}
vector(vector a, vector b, float mix) lerpVectorHermite =
{
if (mix <= 0) return a;
if (mix >= 1) return b;
local float h01;
h01 = mix * mix;
h01 *= 3 - 2 * mix;
return (b * h01 + a * ( 1 - h01 ) );
}
float(float anga, float angb) angledif =
{
float dif;
dif = fabs(anga - angb);
if (dif > 180)
dif = 360 - dif;
return dif;
}
float(vector ang, vector base_ang, vector offset) isInAngle = {
if (angledif(ang_x, base_ang_x) > offset_x || angledif(ang_y, base_ang_y) > offset_y)
return FALSE;
else
return TRUE;
};
// normalizes an angle vector to the 0/+359 range
vector(vector ang) normalizeAngles = {
ang_x = ang_x - floor(ang_x/360) * 360;
ang_y = ang_y - floor(ang_y/360) * 360;
ang_z = ang_z - floor(ang_z/360) * 360;
/*
while (ang_x > 360)
ang_x = ang_x - 360;
while (ang_x < 0)
ang_x = ang_x + 360;
while (ang_y > 360)
ang_y = ang_y - 360;
while (ang_y < 0)
ang_y = ang_y + 360;
while (ang_z > 360)
ang_z = ang_z - 360;
while (ang_z < 0)
ang_z = ang_z + 360;
*/
return ang;
};
// normalizes an angle vector to the -180/+179 range
vector(vector ang) normalizeAngles180 = {
ang_x = ((ang_x+180) - floor((ang_x+180)/360) * 360) - 180;
ang_y = ((ang_y+180) - floor((ang_y+180)/360) * 360) - 180;
ang_z = ((ang_z+180) - floor((ang_z+180)/360) * 360) - 180;
/*
while (ang_x > 180)
ang_x = ang_x - 360;
while (ang_x < -180)
ang_x = ang_x + 360;
while (ang_y > 180)
ang_y = ang_y - 360;
while (ang_y < -180)
ang_y = ang_y + 360;
while (ang_z > 180)
ang_z = ang_z - 360;
while (ang_z < -180)
ang_z = ang_z + 360;
*/
return ang;
};
//======================================================================
// Vector randomizer, used mostly for avelocity setups
//----------------------------------------------------------------------
vector(float base, float rndmix, float plusminus) vecrand =
{
local vector vecmix;
if (plusminus) {
vecmix_x = base + crandom() * rndmix;
vecmix_y = base + crandom() * rndmix;
vecmix_z = base + crandom() * rndmix;
}
else {
vecmix_x = base + random() * rndmix;
vecmix_y = base + random() * rndmix;
vecmix_z = base + random() * rndmix;
}
return vecmix;
};