Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
vector.qh
Go to the documentation of this file.
1#pragma once
2
4#define vlen2(v) (_vlen2 = (v), dotproduct(_vlen2, _vlen2))
5
6#if 1
8 #define vdist(v, cmp, f) (vlen2(v) cmp ((f) ** 2))
9#else
10 #define vdist(v, cmp, f) (vlen(v) cmp (f))
11#endif
12
13#if 1
14 #define dotproduct(a, b) ((a) * (b))
15#else
16 noref vector _dotproduct_a, _dotproduct_b;
17 #define dotproduct(a, b) \
18 (_dotproduct_a = (a), _dotproduct_b = (b), \
19 _dotproduct_a.x * _dotproduct_b.x \
20 + _dotproduct_a.y * _dotproduct_b.y \
21 + _dotproduct_a.z * _dotproduct_b.z)
22#endif
23
24#if 1
25 #define cross(a, b) ((a) >< (b))
26#else
29 {
30 return '1 0 0' * (a.y * b.z - a.z * b.y)
31 + '0 1 0' * (a.z * b.x - a.x * b.z)
32 + '0 0 1' * (a.x * b.y - a.y * b.x);
33 }
34#endif
35
37#define vmul(a, b) \
38 (_vmul_a = (a), _vmul_b = (b), \
39 '1 0 0' * (_vmul_a.x * _vmul_b.x) \
40 + '0 1 0' * (_vmul_a.y * _vmul_b.y) \
41 + '0 0 1' * (_vmul_a.z * _vmul_b.z))
42
43const vector eX = '1 0 0';
44const vector eY = '0 1 0';
45const vector eZ = '0 0 1';
46
49{
50 vector v;
51 m2 -= m1;
52 v.x = m2.x * random() + m1.x;
53 v.y = m2.y * random() + m1.y;
54 v.z = m2.z * random() + m1.z;
55 return v;
56}
57
60{
61 return max(v.x, v.y, -v.x, -v.y);
62}
63
66{
67 return min(max(v.x, -v.x), max(v.y, -v.y));
68}
69
72float boxesoverlap(vector m1, vector m2, vector m3, vector m4)
73{
74 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
79float boxinsidebox(vector smins, vector smaxs, vector bmins, vector bmaxs)
80{
81 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;
82}
83
84#define pointinsidebox(point, bmins, bmaxs) boxinsidebox(point, point, bmins, bmaxs)
85
86#define PITCH(v) ((v).x)
87#define YAW(v) ((v).y)
88#define ROLL(v) ((v).z)
89
90//pseudo prototypes:
91// vector vec2(vector v); // returns a vector with just the x and y components of the given vector
92// vector vec2(float x, float y); // returns a vector with the given x and y components
93
95#define vec2(...) EVAL(OVERLOAD(vec2, __VA_ARGS__))
96#define vec2_1(v) (_vec2 = (v), _vec2.z = 0, _vec2)
97#define vec2_2(x, y) (_vec2_x = (x), _vec2_y = (y), _vec2)
98
100#define vec3(_x, _y, _z) (_vec3.x = (_x), _vec3.y = (_y), _vec3.z = (_z), _vec3)
101
102#define VEC_NAN vec3(FLOAT_NAN, FLOAT_NAN, FLOAT_NAN);
103
106{
107 return isnan(v.x) && isnan(v.y) && isnan(v.z);
108}
109
112{
113 if (a == 0)
114 return v;
115 if (a == M_PI)
116 return -v;
117
118 float a_sin = sin(a), a_cos = cos(a);
119 return vec2(v.x * a_cos + v.y * a_sin, -v.x * a_sin + v.y * a_cos);
120}
121
123#define yinvert(v) (_yinvert = (v), _yinvert.y = 1 - _yinvert.y, _yinvert)
124
131{
132 ldir = normalize(ldir);
133 p = l0 - p;
134 // remove the component in line direction from p
135 return p - ((p * ldir) * ldir);
136}
137
145{
146 return dir - 2 * (dir * norm) * norm;
147}
148
151vector vec_reflect(vector vel, vector norm, float bounce)
152{
153 return vel - (1 + bounce) * (vel * norm) * norm;
154}
155
157vector vec_epsilon(vector this, float eps)
158{
159 if (this.x > -eps && this.x < eps) this.x = 0;
160 if (this.y > -eps && this.y < eps) this.y = 0;
161 if (this.z > -eps && this.z < eps) this.z = 0;
162 return this;
163}
164
165#define ClipVelocity(in, normal, out, overbounce) \
166 (out = vec_epsilon(vec_reflect(in, normal, (overbounce) - 1), 0.1))
167
168#ifdef GAMEQC
171 {
172 switch (corner)
173 {
174 case 1: return box.absmin;
175 case 2: return vec3(box.absmax.x, box.absmin.y, box.absmin.z);
176 case 3: return vec3(box.absmin.x, box.absmax.y, box.absmin.z);
177 case 4: return vec3(box.absmin.x, box.absmin.y, box.absmax.z);
178 case 5: return vec3(box.absmax.x, box.absmax.y, box.absmin.z);
179 case 6: return vec3(box.absmin.x, box.absmax.y, box.absmax.z);
180 case 7: return vec3(box.absmax.x, box.absmin.y, box.absmax.z);
181 case 8: return box.absmax;
182 default: return '0 0 0';
183 }
184 }
185
188 {
189 vector mi = box.mins + box.origin;
190 vector ma = box.maxs + box.origin;
191
192 return vec3(
193 bound(mi.x, org.x, ma.x),
194 bound(mi.y, org.y, ma.y),
195 bound(mi.z, org.z, ma.z)
196 );
197 }
198
201{
202 return vec3(
203 bound(mi.x, org.x, ma.x),
204 bound(mi.y, org.y, ma.y),
205 bound(mi.z, org.z, ma.z)
206 );
207}
208
209// bones_was_here: rounding bbox to nearest perfect floats prevents obscure collision bugs like #2742
210// FIXME: QC shouldn't need to work around tracebox potentially returning a tiny trace_fraction when the move should have been blocked.
211// Tiny values are valid in some situations and can't simply be ignored.
212#define PFLOAT (1/1024) // 1/32 1/64 etc also work
213#define RPFLOAT(a) (a = rint(a / PFLOAT) * PFLOAT)
216{
217 RPFLOAT(v.x);
218 RPFLOAT(v.y);
219 RPFLOAT(v.z);
220 return v;
221}
222
225{
226 v.x = rint(v.x);
227 v.y = rint(v.y);
228 v.z = rint(v.z);
229 return v;
230}
231
232#endif // GAMEQC
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
v y
Definition ent_cs.qc:146
v z
Definition ent_cs.qc:146
v x
Definition ent_cs.qc:146
#define ERASEABLE
Definition _all.inc:37
bool isnan(float e)
Definition mathlib.qc:21
#define M_PI
pi
Definition mathlib.qh:112
float bound(float min, float value, float max)
float cos(float f)
float random(void)
float sin(float f)
float min(float f,...)
float rint(float f)
vector normalize(vector v)
float max(float f,...)
vector
Definition self.qh:96
vector org
Definition self.qh:96
int dir
Definition impulse.qc:89
const vector eY
Definition vector.qh:44
ERASEABLE vector randompos(vector m1, vector m2)
Definition vector.qh:48
ERASEABLE vector vrint(vector v)
Definition vector.qh:224
noref vector _vmul_a
Definition vector.qh:36
const vector eZ
Definition vector.qh:45
noref vector _vmul_b
Definition vector.qh:36
#define RPFLOAT(a)
Definition vector.qh:213
ERASEABLE vector NearestPointOnBoundingBox(vector mi, vector ma, vector org)
Definition vector.qh:200
ERASEABLE vector reflect(vector dir, vector norm)
Definition vector.qh:144
#define cross(a, b)
Definition vector.qh:25
ERASEABLE vector point_line_vec(vector p, vector l0, vector ldir)
Definition vector.qh:130
ERASEABLE vector RoundPerfectVector(vector v)
Definition vector.qh:215
ERASEABLE vector NearestPointOnBox(entity box, vector org)
Definition vector.qh:187
noref vector _vec2
Definition vector.qh:94
ERASEABLE vector get_corner_position(entity box, int corner)
Definition vector.qh:170
ERASEABLE bool is_all_nans(vector v)
Definition vector.qh:105
ERASEABLE float boxesoverlap(vector m1, vector m2, vector m3, vector m4)
Requires that m2>m1 in all coordinates, and that m4>m3.
Definition vector.qh:72
ERASEABLE float vlen_maxnorm2d(vector v)
Definition vector.qh:59
ERASEABLE vector vec_epsilon(vector this, float eps)
Definition vector.qh:157
ERASEABLE float boxinsidebox(vector smins, vector smaxs, vector bmins, vector bmaxs)
Requires the same as boxesoverlap, but is a stronger condition.
Definition vector.qh:79
ERASEABLE vector vec_reflect(vector vel, vector norm, float bounce)
Clip vel along the plane defined by norm (assuming 0 distance away), bounciness determined by bounce ...
Definition vector.qh:151
noref vector _yinvert
Definition vector.qh:122
noref vector _vec3
Definition vector.qh:99
noref vector _vlen2
Definition vector.qh:3
ERASEABLE vector Rotate(vector v, float a)
Definition vector.qh:111
ERASEABLE float vlen_minnorm2d(vector v)
Definition vector.qh:65
const vector eX
Definition vector.qh:43
#define vec2(...)
Definition vector.qh:95
#define vec3(_x, _y, _z)
Definition vector.qh:100