]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/vector.qh
Merge branch 'master' into terencehill/race_cleanups
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / vector.qh
1 #pragma once
2
3 noref vector _vlen2;
4 #define vlen2(v) (_vlen2 = (v), dotproduct(_vlen2, _vlen2))
5
6 #if 1
7 /** Vector distance comparison, avoids sqrt() */
8 #define vdist(v, cmp, f) (vlen2(v) cmp ((f) ** 2))
9 #else
10 #define vdist(v, cmp, f) (vlen(v) cmp (f))
11 #endif
12
13 #if 1
14 #define dotproduct(a, b) ((a) * (b))
15 #else
16 noref vector _dotproduct_a, _dotproduct_b;
17 #define dotproduct(a, b) \
18         (_dotproduct_a = (a), _dotproduct_b = (b), \
19           _dotproduct_a.x * _dotproduct_b.x \
20         + _dotproduct_a.y * _dotproduct_b.y \
21         + _dotproduct_a.z * _dotproduct_b.z)
22 #endif
23
24 #if 1
25 #define cross(a, b) ((a) >< (b))
26 #else
27 ERASEABLE
28 vector cross(vector a, vector b)
29 {
30         return
31                 '1 0 0' * (a.y * b.z - a.z * b.y)
32         +       '0 1 0' * (a.z * b.x - a.x * b.z)
33         +       '0 0 1' * (a.x * b.y - a.y * b.x);
34 }
35 #endif
36
37 noref vector _vmul_a, _vmul_b;
38 #define vmul(a, b) \
39     (_vmul_a = (a), _vmul_b = (b), \
40           '1 0 0' * (_vmul_a.x * _vmul_b.x) \
41         + '0 1 0' * (_vmul_a.y * _vmul_b.y) \
42         + '0 0 1' * (_vmul_a.z * _vmul_b.z))
43
44 const vector eX = '1 0 0';
45 const vector eY = '0 1 0';
46 const vector eZ = '0 0 1';
47
48 ERASEABLE
49 vector randompos(vector m1, vector m2)
50 {
51         vector v;
52         m2 = m2 - m1;
53         v_x = m2_x * random() + m1_x;
54         v_y = m2_y * random() + m1_y;
55         v_z = m2_z * random() + m1_z;
56         return v;
57 }
58
59 ERASEABLE
60 float vlen_maxnorm2d(vector v)
61 {
62         return max(v.x, v.y, -v.x, -v.y);
63 }
64
65 ERASEABLE
66 float vlen_minnorm2d(vector v)
67 {
68         return min(max(v.x, -v.x), max(v.y, -v.y));
69 }
70
71 /** requires that m2>m1 in all coordinates, and that m4>m3 */
72 ERASEABLE
73 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; }
74
75 /** requires the same as boxesoverlap, but is a stronger condition */
76 ERASEABLE
77 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; }
78
79 #define PITCH(v) ((v).x)
80 #define YAW(v) ((v).y)
81 #define ROLL(v) ((v).z)
82
83 //pseudo prototypes:
84 // vector vec2(vector v); // returns a vector with just the x and y components of the given vector
85 // vector vec2(float x, float y); // returns a vector with the given x and y components
86
87 noref vector _vec2;
88 #define vec2(...) EVAL(OVERLOAD(vec2, __VA_ARGS__))
89 #define vec2_1(v) (_vec2 = (v), _vec2.z = 0, _vec2)
90 #define vec2_2(x, y) (_vec2_x = (x), _vec2_y = (y), _vec2)
91
92 noref vector _vec3;
93 #define vec3(_x, _y, _z) (_vec3.x = (_x), _vec3.y = (_y), _vec3.z = (_z), _vec3)
94
95 #define VEC_NAN vec3(FLOAT_NAN, FLOAT_NAN, FLOAT_NAN);
96
97 ERASEABLE
98 bool is_all_nans(vector v) {
99         return isnan(v.x) && isnan(v.y) && isnan(v.z);
100 }
101
102 ERASEABLE
103 vector Rotate(vector v, float a)
104 {
105         float a_sin = sin(a), a_cos = cos(a);
106         return vec2(v.x * a_cos + v.y * a_sin, -v.x * a_sin + v.y * a_cos);
107 }
108
109 noref vector _yinvert;
110 #define yinvert(v) (_yinvert = (v), _yinvert.y = 1 - _yinvert.y, _yinvert)
111
112 /// \param[in] p point
113 /// \param[in] l0 starting point of ldir
114 /// \param[in] ldir line
115 /// \return Vector starting from p perpendicular to ldir
116 ERASEABLE
117 vector point_line_vec(vector p, vector l0, vector ldir)
118 {
119         ldir = normalize(ldir);
120         p = l0 - p;
121         // remove the component in line direction from p
122         return p - ((p * ldir) * ldir);
123 }
124
125 /**
126  * @param dir the directional vector
127  * @param norm the normalized normal
128  * @returns dir reflected by norm
129  */
130 ERASEABLE
131 vector reflect(vector dir, vector norm)
132 {
133         return dir - 2 * (dir * norm) * norm;
134 }
135
136 /**
137  * clip vel along the plane defined by norm (assuming 0 distance away), bounciness determined by bounce 0..1
138  */
139 ERASEABLE
140 vector vec_reflect(vector vel, vector norm, float bounce)
141 {
142         return vel - (1 + bounce) * (vel * norm) * norm;
143 }
144
145 ERASEABLE
146 vector vec_epsilon(vector this, float eps)
147 {
148         if (this.x > -eps && this.x < eps) this.x = 0;
149         if (this.y > -eps && this.y < eps) this.y = 0;
150         if (this.z > -eps && this.z < eps) this.z = 0;
151         return this;
152 }
153
154 #define ClipVelocity(in, normal, out, overbounce) \
155         (out = vec_epsilon(vec_reflect(in, normal, (overbounce) - 1), 0.1))
156
157 #ifdef GAMEQC
158         ERASEABLE
159         vector get_corner_position(entity box, int corner)
160         {
161                 switch (corner)
162                 {
163                         case 1: return vec3(box.absmin.x, box.absmin.y, box.absmin.z);
164                         case 2: return vec3(box.absmax.x, box.absmin.y, box.absmin.z);
165                         case 3: return vec3(box.absmin.x, box.absmax.y, box.absmin.z);
166                         case 4: return vec3(box.absmin.x, box.absmin.y, box.absmax.z);
167                         case 5: return vec3(box.absmax.x, box.absmax.y, box.absmin.z);
168                         case 6: return vec3(box.absmin.x, box.absmax.y, box.absmax.z);
169                         case 7: return vec3(box.absmax.x, box.absmin.y, box.absmax.z);
170                         case 8: return vec3(box.absmax.x, box.absmax.y, box.absmax.z);
171                         default: return '0 0 0';
172                 }
173         }
174
175         ERASEABLE
176         vector NearestPointOnBox(entity box, vector org)
177         {
178                 vector m1 = box.mins + box.origin;
179                 vector m2 = box.maxs + box.origin;
180
181                 return vec3(
182                         bound(m1.x, org.x, m2.x),
183                         bound(m1.y, org.y, m2.y),
184                         bound(m1.z, org.z, m2.z)
185                 );
186         }
187 #endif