]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/vector.qh
Merge branch 'terencehill/quickmenu_file_example' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / vector.qh
1 #ifndef VECTOR_H
2 #define VECTOR_H
3
4 const vector eX = '1 0 0';
5 const vector eY = '0 1 0';
6 const vector eZ = '0 0 1';
7
8 vector randompos(vector m1, vector m2)
9 {
10         vector v;
11         m2 = m2 - m1;
12         v_x = m2_x * random() + m1_x;
13         v_y = m2_y * random() + m1_y;
14         v_z = m2_z * random() + m1_z;
15         return v;
16 }
17
18 float vlen2d(vector v)
19 {
20         return sqrt(v.x * v.x + v.y * v.y);
21 }
22
23 float vlen_maxnorm2d(vector v)
24 {
25         return max(v.x, v.y, -v.x, -v.y);
26 }
27
28 float vlen_minnorm2d(vector v)
29 {
30         return min(max(v.x, -v.x), max(v.y, -v.y));
31 }
32
33 float dist_point_line(vector p, vector l0, vector ldir)
34 {
35         ldir = normalize(ldir);
36
37         // remove the component in line direction
38         p = p - (p * ldir) * ldir;
39
40         // vlen of the remaining vector
41         return vlen(p);
42 }
43
44 /** requires that m2>m1 in all coordinates, and that m4>m3 */
45 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; }
46
47 /** requires the same as boxesoverlap, but is a stronger condition */
48 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; }
49
50
51 vector vec2(vector v)
52 {
53         v.z = 0;
54         return v;
55 }
56
57 vector vec3(float x, float y, float z)
58 {
59         vector v;
60         v.x = x;
61         v.y = y;
62         v.z = z;
63         return v;
64 }
65
66 vector rotate(vector v, float a)
67 {
68         float a_sin = sin(a), a_cos = cos(a);
69         vector r = '0 0 0';
70         r.x =      v.x * a_cos + v.y * a_sin;
71         r.y = -1 * v.x * a_sin + v.y * a_cos;
72         return r;
73 }
74
75 vector yinvert(vector v)
76 {
77         v.y = 1 - v.y;
78         return v;
79 }
80
81 #ifndef MENUQC
82         vector get_corner_position(entity box, int corner)
83         {
84                 switch (corner)
85                 {
86                         case 1: return vec3(box.absmin.x, box.absmin.y, box.absmin.z);
87                         case 2: return vec3(box.absmax.x, box.absmin.y, box.absmin.z);
88                         case 3: return vec3(box.absmin.x, box.absmax.y, box.absmin.z);
89                         case 4: return vec3(box.absmin.x, box.absmin.y, box.absmax.z);
90                         case 5: return vec3(box.absmax.x, box.absmax.y, box.absmin.z);
91                         case 6: return vec3(box.absmin.x, box.absmax.y, box.absmax.z);
92                         case 7: return vec3(box.absmax.x, box.absmin.y, box.absmax.z);
93                         case 8: return vec3(box.absmax.x, box.absmax.y, box.absmax.z);
94                         default: return '0 0 0';
95                 }
96         }
97
98         vector NearestPointOnBox(entity box, vector org)
99         {
100                 vector m1 = box.mins + box.origin;
101                 vector m2 = box.maxs + box.origin;
102
103                 vector ret;
104                 ret.x = bound(m1.x, org.x, m2.x);
105                 ret.y = bound(m1.y, org.y, m2.y);
106                 ret.z = bound(m1.z, org.z, m2.z);
107                 return ret;
108         }
109 #endif
110
111 #endif