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