4 #define vlen2(v) (_vlen2 = (v), dotproduct(_vlen2, _vlen2))
8 /** Vector distance comparison, avoids sqrt() */
9 #define vdist(v, cmp, f) (vlen2(v) cmp (_vdist_f = (f), _vdist_f * _vdist_f))
11 #define vdist(v, cmp, f) (vlen(v) cmp (f))
15 #define dotproduct(a, b) ((a) * (b))
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)
26 #define cross(a, b) ((a) >< (b))
28 vector cross(vector a, vector b)
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);
37 noref vector _vmul_a, _vmul_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))
44 const vector eX = '1 0 0';
45 const vector eY = '0 1 0';
46 const vector eZ = '0 0 1';
48 vector randompos(vector m1, vector m2)
52 v_x = m2_x * random() + m1_x;
53 v_y = m2_y * random() + m1_y;
54 v_z = m2_z * random() + m1_z;
58 float vlen_maxnorm2d(vector v)
60 return max(v.x, v.y, -v.x, -v.y);
63 float vlen_minnorm2d(vector v)
65 return min(max(v.x, -v.x), max(v.y, -v.y));
68 float dist_point_line(vector p, vector l0, vector ldir)
70 ldir = normalize(ldir);
72 // remove the component in line direction
73 p = p - (p * ldir) * ldir;
75 // vlen of the remaining vector
79 /** requires that m2>m1 in all coordinates, and that m4>m3 */
80 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; }
82 /** requires the same as boxesoverlap, but is a stronger condition */
83 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; }
85 #define PITCH(v) ((v).x)
86 #define YAW(v) ((v).y)
87 #define ROLL(v) ((v).z)
89 #define MAKEVECTORS(f, angles, forward, right, up) MACRO_BEGIN { \
91 forward = v_forward; \
97 #define vec2(...) EVAL(OVERLOAD(vec2, __VA_ARGS__))
98 #define vec2_1(v) (_vec2 = (v), _vec2.z = 0, _vec2)
99 #define vec2_2(x, y) (_vec2_x = (x), _vec2_y = (y), _vec2)
102 #define vec3(_x, _y, _z) (_vec3.x = (_x), _vec3.y = (_y), _vec3.z = (_z), _vec3)
104 vector Rotate(vector v, float a)
106 float a_sin = sin(a), a_cos = cos(a);
108 r.x = v.x * a_cos + v.y * a_sin;
109 r.y = -1 * v.x * a_sin + v.y * a_cos;
113 noref vector _yinvert;
114 #define yinvert(v) (_yinvert = (v), _yinvert.y = 1 - _yinvert.y, _yinvert)
117 * @param dir the directional vector
118 * @param norm the normalized normal
119 * @returns dir reflected by norm
121 vector reflect(vector dir, vector norm)
123 return dir - 2 * (dir * norm) * norm;
127 * clip vel along the plane defined by norm (assuming 0 distance away), bounciness determined by bounce 0..1
129 vector vec_reflect(vector vel, vector norm, float bounce)
131 return vel - (1 + bounce) * (vel * norm) * norm;
134 vector vec_epsilon(vector this, float eps)
136 if (this.x > -eps && this.x < eps) this.x = 0;
137 if (this.y > -eps && this.y < eps) this.y = 0;
138 if (this.z > -eps && this.z < eps) this.z = 0;
142 #define ClipVelocity(in, normal, out, overbounce) \
143 (out = vec_epsilon(vec_reflect(in, normal, (overbounce) - 1), 0.1))
146 vector get_corner_position(entity box, int corner)
150 case 1: return vec3(box.absmin.x, box.absmin.y, box.absmin.z);
151 case 2: return vec3(box.absmax.x, box.absmin.y, box.absmin.z);
152 case 3: return vec3(box.absmin.x, box.absmax.y, box.absmin.z);
153 case 4: return vec3(box.absmin.x, box.absmin.y, box.absmax.z);
154 case 5: return vec3(box.absmax.x, box.absmax.y, box.absmin.z);
155 case 6: return vec3(box.absmin.x, box.absmax.y, box.absmax.z);
156 case 7: return vec3(box.absmax.x, box.absmin.y, box.absmax.z);
157 case 8: return vec3(box.absmax.x, box.absmax.y, box.absmax.z);
158 default: return '0 0 0';
162 vector NearestPointOnBox(entity box, vector org)
164 vector m1 = box.mins + box.origin;
165 vector m2 = box.maxs + box.origin;
168 ret.x = bound(m1.x, org.x, m2.x);
169 ret.y = bound(m1.y, org.y, m2.y);
170 ret.z = bound(m1.z, org.z, m2.z);