33#define MATH_PI 3.1415926
34noref
const vector g_vec_null = [0.0f, 0.0f, 0.0f];
38vector vectoangles2(vector fwd, optional vector up) = #11;
39void rotatevectorsbyangle(vector angle) = #0;
41#define vectoangles vectoangles2
46void makevectors( vector angles )
49 float sr, sp, sy, cr, cp, cy;
51 angle = angles[1] * (M_PI*2 / 360);
54 angle = angles[0] * (M_PI*2 / 360);
57 angle = angles[2] * (M_PI*2 / 360);
65 v_right[0] = (-1*sr*sp*cy+-1*cr*-sy);
66 v_right[1] = (-1*sr*sp*sy+-1*cr*cy);
67 v_right[2] = -1*sr*cp;
69 v_up[0] = (cr*sp*cy+-sr*-sy);
70 v_up[1] = (cr*sp*sy+-sr*cy);
83lerpAngle(
float startAngle,
float endAngle,
float lerpAmount)
85 float shortest_angle = ((((endAngle - startAngle) % 360.0f) + 540.0f) % 360.0f) - 180.0f;
86 return shortest_angle * lerpAmount;
96lerp(
float startValue,
float endValue,
float lerpAmount)
98 return (startValue * (1 - lerpAmount)) + (endValue * lerpAmount);
106fixAngleDelta(
float angleValue)
108 if (angleValue > 180) {
110 }
else if (angleValue < -180) {
116 return fixAngleDelta(angleValue);
125fixAngle(vector inputAngle)
127 inputAngle[0] = fixAngleDelta(inputAngle[0]);
128 inputAngle[1] = fixAngleDelta(inputAngle[1]);
129 inputAngle[2] = fixAngleDelta(inputAngle[2]);
139reflect(vector hitDirection, vector planeNormal)
141 return hitDirection - 2 * dotproduct(hitDirection, planeNormal) * planeNormal;
149randomVector(
bool flyUp)
152 tmp[0] = random() - 0.5f;
153 tmp[1] = random() - 0.5f;
155 if ( flyUp ==
true ) {
158 tmp[2] = random() - 0.5f;
171rotateAroundPoint(vector pos, vector pivot,
float degr)
174 new[0] = pivot[0] + (pos[0] - pivot[0]) * cos(degr) - (pos[1] - pivot[1]) * sin(degr);
175 new[1] = pivot[1] + (pos[0] - pivot[0]) * sin(degr) + (pos[1] - pivot[1]) * cos(degr);
185angleDifference(vector angle1, vector angle2)
187 static float Math_AngleDiff_S(
float from,
float to) {
188 float angleDelta = from - to;
190 if (angleDelta > 180) {
192 }
else if (angleDelta < -180) {
202 angle1 = fixAngle(angle1);
203 angle2 = fixAngle(angle2);
205 newAngle[0] = Math_AngleDiff_S(angle1[0], angle2[0]);
206 newAngle[1] = Math_AngleDiff_S(angle1[1], angle2[1]);
207 newAngle[2] = Math_AngleDiff_S(angle1[2], angle2[2]);
218hsvToRGB(
float h,
float s,
float v)
221 vector col = [0,0,0];
223 h = max(0.0, min(360.0, h));
224 s = max(0.0, min(100.0, s));
225 v = max(0.0, min(100.0, v));
231 col[0] = col[1] = col[2] = rint(v*255);
240 t = v * (1 - s * (1 - f));
244 col[0] = rint(255*v);
245 col[1] = rint(255*t);
246 col[2] = rint(255*p);
249 col[0] = rint(255*q);
250 col[1] = rint(255*v);
251 col[2] = rint(255*p);
254 col[0] = rint(255*p);
255 col[1] = rint(255*v);
256 col[2] = rint(255*t);
259 col[0] = rint(255*p);
260 col[1] = rint(255*q);
261 col[2] = rint(255*v);
264 col[0] = rint(255*t);
265 col[1] = rint(255*p);
266 col[2] = rint(255*v);
269 col[0] = rint(255*v);
270 col[1] = rint(255*p);
271 col[2] = rint(255*q);
277#include "math_vector.h"