]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/lib/vector.qh
String: cons
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / vector.qh
index de32d206f3b1f40235498f1914c7db66e2e6fd3c..c5c4d655585d1bf6d8a0b0b3d9dd0b2b8ffaebef 100644 (file)
@@ -1,6 +1,31 @@
 #ifndef VECTOR_H
 #define VECTOR_H
 
+noref vector _vlen2;
+#define vlen2(v) \
+       (_vlen2 = (v), \
+         _vlen2.x * _vlen2.x \
+       + _vlen2.y * _vlen2.y \
+       + _vlen2.z * _vlen2.z)
+
+noref float _vdist_f;
+/** Vector distance comparison, avoids sqrt() */
+#define vdist(v, cmp, f) (vlen2(v) cmp (_vdist_f = (f), _vdist_f * _vdist_f))
+/*
+#define vdist(v, cmp, f) (vlen(v) cmp (f))
+*/
+
+#define cross(a, b) ((a) >< (b))
+/*
+vector cross(vector a, vector b)
+{
+       return
+               '1 0 0' * (a.y * b.z - a.z * b.y)
+       +       '0 1 0' * (a.z * b.x - a.x * b.z)
+       +       '0 0 1' * (a.x * b.y - a.y * b.x);
+}
+*/
+
 const vector eX = '1 0 0';
 const vector eY = '0 1 0';
 const vector eZ = '0 0 1';
@@ -15,11 +40,6 @@ vector randompos(vector m1, vector m2)
        return v;
 }
 
-float vlen2d(vector v)
-{
-       return sqrt(v.x * v.x + v.y * v.y);
-}
-
 float vlen_maxnorm2d(vector v)
 {
        return max(v.x, v.y, -v.x, -v.y);
@@ -47,22 +67,35 @@ float boxesoverlap(vector m1, vector m2, vector m3, vector m4) { return m2_x >=
 /** requires the same as boxesoverlap, but is a stronger condition */
 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; }
 
+#define PITCH(v) ((v).x)
+#define YAW(v) ((v).y)
+#define ROLL(v) ((v).z)
 
-vector vec2(vector v)
-{
-       v.z = 0;
-       return v;
-}
+#define MAKEVECTORS(f, angles, forward, right, up) MACRO_BEGIN { \
+       f(angles); \
+       forward = v_forward; \
+       right = v_right; \
+       up = v_up; \
+} MACRO_END
+
+noref vector _vec2;
+#define vec2(v) (_vec2 = (v), _vec2.z = 0, _vec2)
+
+noref vector _vec3;
+#define vec3(x, y, z) (_vec3_x = (x), _vec3_y = (y), _vec3_z = (z), _vec3)
 
-vector vec3(float x, float y, float z)
+vector rotate(vector v, float a)
 {
-       vector v;
-       v.x = x;
-       v.y = y;
-       v.z = z;
-       return v;
+       float a_sin = sin(a), a_cos = cos(a);
+       vector r = '0 0 0';
+       r.x =      v.x * a_cos + v.y * a_sin;
+       r.y = -1 * v.x * a_sin + v.y * a_cos;
+       return r;
 }
 
+noref vector _yinvert;
+#define yinvert(v) (_yinvert = (v), _yinvert.y = 1 - _yinvert.y, _yinvert)
+
 #ifndef MENUQC
        vector get_corner_position(entity box, int corner)
        {