Nuclide
Software Development Kit for id Tech
math.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016-2024 Vera Visions LLC.
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
13 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
14 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*/
16
29#define MATH_PI 3.1415926
30noref const vector g_vec_null = [0.0f, 0.0f, 0.0f];
31
32#ifdef MENU
33
34vector(vector fwd, optional vector up) vectoangles2 = #11;
35void(vector angle) rotatevectorsbyangle = #0;
36
37#define vectoangles vectoangles2
38
43{
44 float angle;
45 float sr, sp, sy, cr, cp, cy;
46
47 angle = angles[1] * (M_PI*2 / 360);
48 sy = sin(angle);
49 cy = cos(angle);
50 angle = angles[0] * (M_PI*2 / 360);
51 sp = sin(angle);
52 cp = cos(angle);
53 angle = angles[2] * (M_PI*2 / 360);
54 sr = sin(angle);
55 cr = cos(angle);
56
57 v_forward[0] = cp*cy;
58 v_forward[1] = cp*sy;
59 v_forward[2] = -sp;
60
61 v_right[0] = (-1*sr*sp*cy+-1*cr*-sy);
62 v_right[1] = (-1*sr*sp*sy+-1*cr*cy);
63 v_right[2] = -1*sr*cp;
64
65 v_up[0] = (cr*sp*cy+-sr*-sy);
66 v_up[1] = (cr*sp*sy+-sr*cy);
67 v_up[2] = cr*cp;
68}
69#endif
70
71
78float
79lerpAngle(float startAngle, float endAngle, float lerpAmount)
80{
81 float shortest_angle = ((((endAngle - startAngle) % 360.0f) + 540.0f) % 360.0f) - 180.0f;
82 return shortest_angle * lerpAmount;
83}
84
91float
92lerp(float startValue, float endValue, float lerpAmount)
93{
94 return (startValue * (1 - lerpAmount)) + (endValue * lerpAmount);
95}
96
101float
102fixAngleDelta(float angleValue)
103{
104 if (angleValue > 180) {
105 angleValue -= 360;
106 } else if (angleValue < -180) {
107 angleValue += 360;
108 } else {
109 return angleValue;
110 }
111
112 return fixAngleDelta(angleValue);
113}
114
115
120vector
121fixAngle(vector inputAngle)
122{
123 inputAngle[0] = fixAngleDelta(inputAngle[0]);
124 inputAngle[1] = fixAngleDelta(inputAngle[1]);
125 inputAngle[2] = fixAngleDelta(inputAngle[2]);
126 return inputAngle;
127}
128
134vector
135reflect(vector hitDirection, vector planeNormal)
136{
137 return hitDirection - 2 * dotproduct(hitDirection, planeNormal) * planeNormal;
138}
139
144vector
145randomVector(bool flyUp)
146{
147 vector tmp;
148 tmp[0] = random() - 0.5f;
149 tmp[1] = random() - 0.5f;
150
151 if ( flyUp == true ) {
152 tmp[2] = random();
153 } else {
154 tmp[2] = random() - 0.5f;
155 }
156
157 return tmp * 2.0f;
158}
159
166vector
167rotateAroundPoint(vector pos, vector pivot, float degr)
168{
169 vector new = pos;
170 new[0] = pivot[0] + (pos[0] - pivot[0]) * cos(degr) - (pos[1] - pivot[1]) * sin(degr);
171 new[1] = pivot[1] + (pos[0] - pivot[0]) * sin(degr) + (pos[1] - pivot[1]) * cos(degr);
172 return new;
173}
174
180vector
182{
183 static float Math_AngleDiff_S(float from, float to) {
184 float angleDelta = from - to;
185
186 if (angleDelta > 180) {
187 angleDelta -= 360;
188 } else if (angleDelta < -180) {
189 angleDelta += 360;
190 }
191
192 return angleDelta;
193 }
194
195 vector newAngle;
196
197 /* clean up input angles */
198 angle1 = fixAngle(angle1);
199 angle2 = fixAngle(angle2);
200
201 newAngle[0] = Math_AngleDiff_S(angle1[0], angle2[0]);
202 newAngle[1] = Math_AngleDiff_S(angle1[1], angle2[1]);
203 newAngle[2] = Math_AngleDiff_S(angle1[2], angle2[2]);
204 return newAngle;
205}
206
212vector
213hsvToRGB(float h, float s, float v)
214{
215 float i,f,p,q,t;
216 vector col = [0,0,0];
217
218 h = max(0.0, min(360.0, h));
219 s = max(0.0, min(100.0, s));
220 v = max(0.0, min(100.0, v));
221
222 s /= 100;
223 v /= 100;
224
225 if (s == 0) {
226 col[0] = col[1] = col[2] = rint(v*255);
227 return col;
228 }
229
230 h /= 60;
231 i = floor(h);
232 f = h - i;
233 p = v * (1 - s);
234 q = v * (1 - s * f);
235 t = v * (1 - s * (1 - f));
236
237 switch (i) {
238 case 0:
239 col[0] = rint(255*v);
240 col[1] = rint(255*t);
241 col[2] = rint(255*p);
242 break;
243 case 1:
244 col[0] = rint(255*q);
245 col[1] = rint(255*v);
246 col[2] = rint(255*p);
247 break;
248 case 2:
249 col[0] = rint(255*p);
250 col[1] = rint(255*v);
251 col[2] = rint(255*t);
252 break;
253 case 3:
254 col[0] = rint(255*p);
255 col[1] = rint(255*q);
256 col[2] = rint(255*v);
257 break;
258 case 4:
259 col[0] = rint(255*t);
260 col[1] = rint(255*p);
261 col[2] = rint(255*v);
262 break;
263 default:
264 col[0] = rint(255*v);
265 col[1] = rint(255*p);
266 col[2] = rint(255*q);
267 }
268 return col;
269}
270
271#include "math_vector.h"
#define dotproduct(v1, v2)
Definition: fteextensions.qc:2464
get float f
Definition: fteextensions.qc:3825
const float M_PI
Definition: fteextensions.qc:864
get __int i
Definition: fteextensions.qc:3826
string s
Definition: fteextensions.qc:3376
vector v_up
Definition: math.h:40
float lerp(float startValue, float endValue, float lerpAmount)
Linear lerp function.
Definition: math.h:92
float fixAngleDelta(float angleValue)
Tecursive function that fixes an euler angle.
Definition: math.h:102
void(vector angle) rotatevectorsbyangle
noref const vector g_vec_null
Definition: math.h:30
vector reflect(vector hitDirection, vector planeNormal)
Takes a direction and a plane normal, returns a new trajectory.
Definition: math.h:135
vector rotateAroundPoint(vector pos, vector pivot, float degr)
Takes a position and a pivot point and rotates point by N degrees around the pivot (YAW)
Definition: math.h:167
vector v_right
Definition: math.h:41
vector angleDifference(vector angle1, vector angle2)
Calculates the difference between two angles.
Definition: math.h:181
float lerpAngle(float startAngle, float endAngle, float lerpAmount)
Euler-angle lerping function that accounts for negative degrees.
Definition: math.h:79
vector v_forward
Definition: math.h:39
vector hsvToRGB(float h, float s, float v)
Converts a Hue-Saturation-Value pair to an RGB vector.
Definition: math.h:213
void makevectors(vector angles)
Definition: math.h:42
vector fixAngle(vector inputAngle)
Recursive function that fixes euler angles.
Definition: math.h:121
vector randomVector(bool flyUp)
Calculates a random Vector, with every axis being a value between -1.0 and 1.0, unless flyUp is true.
Definition: math.h:145
vector(vector fwd, optional vector up) vectoangles2
noref vector angles
Definition: ui_3dview.qc:18