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