]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/vector.qh
ergonomics initiative for MAKEVECTORS
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / vector.qh
1 #pragma once
2
3 #include "lib/float.qh"
4 #include "lib/misc.qh"
5 #include "lib/static.qh"
6
7 //pseudo prototypes:
8 // vector vec2(vector v); // returns a vector with just the x and y components of the given vector
9 // vector vec2(float x, float y); // returns a vector with the given x and y components
10
11 noref vector _vec2;
12 #define vec2(...) EVAL(OVERLOAD(vec2, __VA_ARGS__))
13 #define vec2_1(v) (_vec2 = (v), _vec2.z = 0, _vec2)
14 #define vec2_2(x, y) (_vec2_x = (x), _vec2_y = (y), _vec2)
15
16 noref vector _vec3;
17 #define vec3(_x, _y, _z) (_vec3.x = (_x), _vec3.y = (_y), _vec3.z = (_z), _vec3)
18
19 #define VEC_NAN vec3(FLOAT_NAN, FLOAT_NAN, FLOAT_NAN);
20
21 noref vector _vlen2;
22 #define vlen2(v) (_vlen2 = (v), dotproduct(_vlen2, _vlen2))
23
24 #if 1
25 noref float _vdist_f;
26 /** Vector distance comparison, avoids sqrt() */
27 #define vdist(v, cmp, f) (vlen2(v) cmp (_vdist_f = (f), _vdist_f * _vdist_f))
28 #else
29 #define vdist(v, cmp, f) (vlen(v) cmp (f))
30 #endif
31
32 #if 1
33 #define dotproduct(a, b) ((a) * (b))
34 #else
35 noref vector _dotproduct_a, _dotproduct_b;
36 #define dotproduct(a, b) \
37         (_dotproduct_a = (a), _dotproduct_b = (b), \
38           _dotproduct_a.x * _dotproduct_b.x \
39         + _dotproduct_a.y * _dotproduct_b.y \
40         + _dotproduct_a.z * _dotproduct_b.z)
41 #endif
42
43 #if 1
44 #define cross(a, b) ((a) >< (b))
45 #else
46 ERASEABLE
47 vector cross(vector a, vector b)
48 {
49         return
50                 '1 0 0' * (a.y * b.z - a.z * b.y)
51         +       '0 1 0' * (a.z * b.x - a.x * b.z)
52         +       '0 0 1' * (a.x * b.y - a.y * b.x);
53 }
54 #endif
55
56 noref vector _vmul_a, _vmul_b;
57 #define vmul(a, b) \
58     (_vmul_a = (a), _vmul_b = (b), \
59           '1 0 0' * (_vmul_a.x * _vmul_b.x) \
60         + '0 1 0' * (_vmul_a.y * _vmul_b.y) \
61         + '0 0 1' * (_vmul_a.z * _vmul_b.z))
62
63 const vector eX = '1 0 0';
64 const vector eY = '0 1 0';
65 const vector eZ = '0 0 1';
66
67 ERASEABLE
68 vector randompos(vector m1, vector m2)
69 {
70         vector v;
71         m2 = m2 - m1;
72         v_x = m2_x * random() + m1_x;
73         v_y = m2_y * random() + m1_y;
74         v_z = m2_z * random() + m1_z;
75         return v;
76 }
77
78 ERASEABLE
79 float vlen_maxnorm2d(vector v)
80 {
81         return max(v.x, v.y, -v.x, -v.y);
82 }
83
84 ERASEABLE
85 float vlen_minnorm2d(vector v)
86 {
87         return min(max(v.x, -v.x), max(v.y, -v.y));
88 }
89
90 ERASEABLE
91 float dist_point_line(vector p, vector l0, vector ldir)
92 {
93         ldir = normalize(ldir);
94
95         // remove the component in line direction
96         p = p - (p * ldir) * ldir;
97
98         // vlen of the remaining vector
99         return vlen(p);
100 }
101
102 /** requires that m2>m1 in all coordinates, and that m4>m3 */
103 ERASEABLE
104 float boxesoverlap(vector m1, vector m2, vector m3, vector m4) { return m2_x >= m3_x && m1_x <= m4_x && m2_y >= m3_y && m1_y <= m4_y && m2_z >= m3_z && m1_z <= m4_z; }
105
106 /** requires the same as boxesoverlap, but is a stronger condition */
107 ERASEABLE
108 float boxinsidebox(vector smins, vector smaxs, vector bmins, vector bmaxs) { return smins.x >= bmins.x && smaxs.x <= bmaxs.x && smins.y >= bmins.y && smaxs.y <= bmaxs.y && smins.z >= bmins.z && smaxs.z <= bmaxs.z; }
109
110 #define PITCH(v) ((v).x)
111 #define YAW(v) ((v).y)
112 #define ROLL(v) ((v).z)
113
114 #ifdef GAMEQC
115 STATIC_INIT(globals) {
116         // set to NaN to more easily detect uninitialized use
117         v_forward = VEC_NAN;
118         v_right = VEC_NAN;
119         v_up = VEC_NAN;
120 }
121 #endif
122
123 /// Same as the `makevectors` builtin but uses the provided locals instead of the `v_*` globals.
124 /// Always use this instead of raw `makevectors` to make the data flow clear.
125 /// It's 2018, they even teach that globals are bad at my uni... though for some reason they never explained why. Sigh.
126 #define MAKEVECTORS(angles, forward, right, up) MACRO_BEGIN { \
127         makevectors(angles); \
128         forward = v_forward; \
129         right = v_right; \
130         up = v_up; \
131         v_forward = VEC_NAN; \
132         v_right = VEC_NAN; \
133         v_up = VEC_NAN; \
134 } MACRO_END
135
136 // Same as `MAKEVECTORS` but also creates the locals for convenience.
137 #define MAKEVECTORS_NEW(angles, forward, right, up) \
138         vector forward; \
139         vector right; \
140         vector up; \
141         MAKEVECTORS(angles, forward, right, up);
142
143 // TODO when raw makevectors and similar functions are not used anywhere else anymore,
144 // assert that the global vectors are NaN before calling makevectors in MAKEVECTORS
145 // to make sure nobody (even builtins) is accidentally using them - NaN is the most liekly value to expose values clearly
146 // also uncomment these:
147 //#define makevectors DO_NOT_USE_GLOBALS
148 //#define v_forward DO_NOT_USE_GLOBALS
149 //#define v_right DO_NOT_USE_GLOBALS
150 //#define v_up DO_NOT_USE_GLOBALS
151
152 ERASEABLE
153 vector Rotate(vector v, float a)
154 {
155         float a_sin = sin(a), a_cos = cos(a);
156         return vec2(v.x * a_cos + v.y * a_sin, -v.x * a_sin + v.y * a_cos);
157 }
158
159 noref vector _yinvert;
160 #define yinvert(v) (_yinvert = (v), _yinvert.y = 1 - _yinvert.y, _yinvert)
161
162 /**
163  * @param dir the directional vector
164  * @param norm the normalized normal
165  * @returns dir reflected by norm
166  */
167 ERASEABLE
168 vector reflect(vector dir, vector norm)
169 {
170         return dir - 2 * (dir * norm) * norm;
171 }
172
173 /**
174  * clip vel along the plane defined by norm (assuming 0 distance away), bounciness determined by bounce 0..1
175  */
176 ERASEABLE
177 vector vec_reflect(vector vel, vector norm, float bounce)
178 {
179         return vel - (1 + bounce) * (vel * norm) * norm;
180 }
181
182 ERASEABLE
183 vector vec_epsilon(vector this, float eps)
184 {
185         if (this.x > -eps && this.x < eps) this.x = 0;
186         if (this.y > -eps && this.y < eps) this.y = 0;
187         if (this.z > -eps && this.z < eps) this.z = 0;
188         return this;
189 }
190
191 #define ClipVelocity(in, normal, out, overbounce) \
192         (out = vec_epsilon(vec_reflect(in, normal, (overbounce) - 1), 0.1))
193
194 #ifdef GAMEQC
195         ERASEABLE
196         vector get_corner_position(entity box, int corner)
197         {
198                 switch (corner)
199                 {
200                         case 1: return vec3(box.absmin.x, box.absmin.y, box.absmin.z);
201                         case 2: return vec3(box.absmax.x, box.absmin.y, box.absmin.z);
202                         case 3: return vec3(box.absmin.x, box.absmax.y, box.absmin.z);
203                         case 4: return vec3(box.absmin.x, box.absmin.y, box.absmax.z);
204                         case 5: return vec3(box.absmax.x, box.absmax.y, box.absmin.z);
205                         case 6: return vec3(box.absmin.x, box.absmax.y, box.absmax.z);
206                         case 7: return vec3(box.absmax.x, box.absmin.y, box.absmax.z);
207                         case 8: return vec3(box.absmax.x, box.absmax.y, box.absmax.z);
208                         default: return '0 0 0';
209                 }
210         }
211
212         ERASEABLE
213         vector NearestPointOnBox(entity box, vector org)
214         {
215                 vector m1 = box.mins + box.origin;
216                 vector m2 = box.maxs + box.origin;
217
218                 return vec3(
219                         bound(m1.x, org.x, m2.x),
220                         bound(m1.y, org.y, m2.y),
221                         bound(m1.z, org.z, m2.z)
222                 );
223         }
224 #endif