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
35#define MATH_PI 3.1415926
36noref const vector g_vec_null = [0.0f, 0.0f, 0.0f];
37
38#ifdef MENU
39
40vector vectoangles2(vector fwd, optional vector up) = #11;
41void rotatevectorsbyangle(vector angle) = #0;
42
43#define vectoangles vectoangles2
44
45vector v_forward;
46vector v_up;
47vector v_right;
48void makevectors( vector angles )
49{
50 float angle;
51 float sr, sp, sy, cr, cp, cy;
52
53 angle = angles[1] * (M_PI*2 / 360);
54 sy = sin(angle);
55 cy = cos(angle);
56 angle = angles[0] * (M_PI*2 / 360);
57 sp = sin(angle);
58 cp = cos(angle);
59 angle = angles[2] * (M_PI*2 / 360);
60 sr = sin(angle);
61 cr = cos(angle);
62
63 v_forward[0] = cp*cy;
64 v_forward[1] = cp*sy;
65 v_forward[2] = -sp;
66
67 v_right[0] = (-1*sr*sp*cy+-1*cr*-sy);
68 v_right[1] = (-1*sr*sp*sy+-1*cr*cy);
69 v_right[2] = -1*sr*cp;
70
71 v_up[0] = (cr*sp*cy+-sr*-sy);
72 v_up[1] = (cr*sp*sy+-sr*cy);
73 v_up[2] = cr*cp;
74}
75#endif
76
77
84float
85lerpAngle(float startAngle, float endAngle, float lerpAmount)
86{
87 float shortest_angle = ((((endAngle - startAngle) % 360.0f) + 540.0f) % 360.0f) - 180.0f;
88 return shortest_angle * lerpAmount;
89}
90
97float
98lerp(float startValue, float endValue, float lerpAmount)
99{
100 return (startValue * (1 - lerpAmount)) + (endValue * lerpAmount);
101}
102
107float
108fixAngleDelta(float angleValue)
109{
110 if (angleValue > 180) {
111 angleValue -= 360;
112 } else if (angleValue < -180) {
113 angleValue += 360;
114 } else {
115 return angleValue;
116 }
117
118 return fixAngleDelta(angleValue);
119}
120
121
126vector
127fixAngle(vector inputAngle)
128{
129 inputAngle[0] = fixAngleDelta(inputAngle[0]);
130 inputAngle[1] = fixAngleDelta(inputAngle[1]);
131 inputAngle[2] = fixAngleDelta(inputAngle[2]);
132 return inputAngle;
133}
134
140vector
141reflect(vector hitDirection, vector planeNormal)
142{
143 return hitDirection - 2 * dotproduct(hitDirection, planeNormal) * planeNormal;
144}
145
150vector
151randomVector(bool flyUp)
152{
153 vector tmp;
154 tmp[0] = random() - 0.5f;
155 tmp[1] = random() - 0.5f;
156
157 if ( flyUp == true ) {
158 tmp[2] = random();
159 } else {
160 tmp[2] = random() - 0.5f;
161 }
162
163 return tmp * 2.0f;
164}
165
172vector
173rotateAroundPoint(vector pos, vector pivot, float degr)
174{
175 vector new = pos;
176 new[0] = pivot[0] + (pos[0] - pivot[0]) * cos(degr) - (pos[1] - pivot[1]) * sin(degr);
177 new[1] = pivot[1] + (pos[0] - pivot[0]) * sin(degr) + (pos[1] - pivot[1]) * cos(degr);
178 return new;
179}
180
186vector
187angleDifference(vector angle1, vector angle2)
188{
189 static float Math_AngleDiff_S(float from, float to) {
190 float angleDelta = from - to;
191
192 if (angleDelta > 180) {
193 angleDelta -= 360;
194 } else if (angleDelta < -180) {
195 angleDelta += 360;
196 }
197
198 return angleDelta;
199 }
200
201 vector newAngle;
202
203 /* clean up input angles */
204 angle1 = fixAngle(angle1);
205 angle2 = fixAngle(angle2);
206
207 newAngle[0] = Math_AngleDiff_S(angle1[0], angle2[0]);
208 newAngle[1] = Math_AngleDiff_S(angle1[1], angle2[1]);
209 newAngle[2] = Math_AngleDiff_S(angle1[2], angle2[2]);
210 return newAngle;
211}
212
218vector
219hsvToRGB(float h, float s, float v)
220{
221 float i,f,p,q,t;
222 vector col = [0,0,0];
223
224 h = max(0.0, min(360.0, h));
225 s = max(0.0, min(100.0, s));
226 v = max(0.0, min(100.0, v));
227
228 s /= 100;
229 v /= 100;
230
231 if (s == 0) {
232 col[0] = col[1] = col[2] = rint(v*255);
233 return col;
234 }
235
236 h /= 60;
237 i = floor(h);
238 f = h - i;
239 p = v * (1 - s);
240 q = v * (1 - s * f);
241 t = v * (1 - s * (1 - f));
242
243 switch (i) {
244 case 0:
245 col[0] = rint(255*v);
246 col[1] = rint(255*t);
247 col[2] = rint(255*p);
248 break;
249 case 1:
250 col[0] = rint(255*q);
251 col[1] = rint(255*v);
252 col[2] = rint(255*p);
253 break;
254 case 2:
255 col[0] = rint(255*p);
256 col[1] = rint(255*v);
257 col[2] = rint(255*t);
258 break;
259 case 3:
260 col[0] = rint(255*p);
261 col[1] = rint(255*q);
262 col[2] = rint(255*v);
263 break;
264 case 4:
265 col[0] = rint(255*t);
266 col[1] = rint(255*p);
267 col[2] = rint(255*v);
268 break;
269 default:
270 col[0] = rint(255*v);
271 col[1] = rint(255*p);
272 col[2] = rint(255*q);
273 }
274 return col;
275} // end of multiprogs, server
277
278#include "math_vector.h"
vector v_up
Definition: math.h:46
float lerp(float startValue, float endValue, float lerpAmount)
Linear lerp function.
Definition: math.h:98
float fixAngleDelta(float angleValue)
Tecursive function that fixes an euler angle.
Definition: math.h:108
noref const vector g_vec_null
Definition: math.h:36
vector reflect(vector hitDirection, vector planeNormal)
Takes a direction and a plane normal, returns a new trajectory.
Definition: math.h:141
void rotatevectorsbyangle(vector angle)
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:173
vector v_right
Definition: math.h:47
vector angleDifference(vector angle1, vector angle2)
Calculates the difference between two angles.
Definition: math.h:187
float lerpAngle(float startAngle, float endAngle, float lerpAmount)
Euler-angle lerping function that accounts for negative degrees.
Definition: math.h:85
vector v_forward
Definition: math.h:45
vector hsvToRGB(float h, float s, float v)
Converts a Hue-Saturation-Value pair to an RGB vector.
Definition: math.h:219
void makevectors(vector angles)
Definition: math.h:48
vector fixAngle(vector inputAngle)
Recursive function that fixes euler angles.
Definition: math.h:127
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:151
vector vectoangles2(vector fwd, optional vector up)
noref vector angles
Definition: ui_3dview.qc:18