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
16noref 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
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);
34}
35#endif
36
38#define vmul(a, 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))
43
44const vector eX = '1 0 0';
45const vector eY = '0 1 0';
46const vector eZ = '0 0 1';
47
50{
51 vector v;
52 m2 = m2 - m1;
53 v_x = m2_x * random() + m1_x;
54 v_y = m2_y * random() + m1_y;
55 v_z = m2_z * random() + m1_z;
56 return v;
57}
58
61{
62 return max(v.x, v.y, -v.x, -v.y);
63}
64
67{
68 return min(max(v.x, -v.x), max(v.y, -v.y));
69}
70
73float 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; }
74
77float 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; }
78
79#define pointinsidebox(point, bmins, bmaxs) boxinsidebox(point, point, bmins, bmaxs)
80
81#define PITCH(v) ((v).x)
82#define YAW(v) ((v).y)
83#define ROLL(v) ((v).z)
84
85//pseudo prototypes:
86// vector vec2(vector v); // returns a vector with just the x and y components of the given vector
87// vector vec2(float x, float y); // returns a vector with the given x and y components
88
90#define vec2(...) EVAL(OVERLOAD(vec2, __VA_ARGS__))
91#define vec2_1(v) (_vec2 = (v), _vec2.z = 0, _vec2)
92#define vec2_2(x, y) (_vec2_x = (x), _vec2_y = (y), _vec2)
93
95#define vec3(_x, _y, _z) (_vec3.x = (_x), _vec3.y = (_y), _vec3.z = (_z), _vec3)
96
97#define VEC_NAN vec3(FLOAT_NAN, FLOAT_NAN, FLOAT_NAN);
98
101 return isnan(v.x) && isnan(v.y) && isnan(v.z);
102}
103
106{
107 if (a == 0) return v;
108 if (a == M_PI) return -v;
109
110 float a_sin = sin(a), a_cos = cos(a);
111 return vec2(v.x * a_cos + v.y * a_sin, -v.x * a_sin + v.y * a_cos);
112}
113
115#define yinvert(v) (_yinvert = (v), _yinvert.y = 1 - _yinvert.y, _yinvert)
116
123{
124 ldir = normalize(ldir);
125 p = l0 - p;
126 // remove the component in line direction from p
127 return p - ((p * ldir) * ldir);
128}
129
137{
138 return dir - 2 * (dir * norm) * norm;
139}
140
145vector vec_reflect(vector vel, vector norm, float bounce)
146{
147 return vel - (1 + bounce) * (vel * norm) * norm;
148}
149
151vector vec_epsilon(vector this, float eps)
152{
153 if (this.x > -eps && this.x < eps) this.x = 0;
154 if (this.y > -eps && this.y < eps) this.y = 0;
155 if (this.z > -eps && this.z < eps) this.z = 0;
156 return this;
157}
158
159#define ClipVelocity(in, normal, out, overbounce) \
160 (out = vec_epsilon(vec_reflect(in, normal, (overbounce) - 1), 0.1))
161
162#ifdef GAMEQC
165 {
166 switch (corner)
167 {
168 case 1: return vec3(box.absmin.x, box.absmin.y, box.absmin.z);
169 case 2: return vec3(box.absmax.x, box.absmin.y, box.absmin.z);
170 case 3: return vec3(box.absmin.x, box.absmax.y, box.absmin.z);
171 case 4: return vec3(box.absmin.x, box.absmin.y, box.absmax.z);
172 case 5: return vec3(box.absmax.x, box.absmax.y, box.absmin.z);
173 case 6: return vec3(box.absmin.x, box.absmax.y, box.absmax.z);
174 case 7: return vec3(box.absmax.x, box.absmin.y, box.absmax.z);
175 case 8: return vec3(box.absmax.x, box.absmax.y, box.absmax.z);
176 default: return '0 0 0';
177 }
178 }
179
182 {
183 vector mi = box.mins + box.origin;
184 vector ma = box.maxs + box.origin;
185
186 return vec3(
187 bound(mi.x, org.x, ma.x),
188 bound(mi.y, org.y, ma.y),
189 bound(mi.z, org.z, ma.z)
190 );
191 }
192
195{
196 return vec3(
197 bound(mi.x, org.x, ma.x),
198 bound(mi.y, org.y, ma.y),
199 bound(mi.z, org.z, ma.z)
200 );
201}
202
203// bones_was_here: rounding bbox to nearest perfect floats prevents obscure collision bugs like #2742
204// FIXME: QC shouldn't need to work around tracebox potentially returning a tiny trace_fraction when the move should have been blocked.
205// Tiny values are valid in some situations and can't simply be ignored.
206#define PFLOAT (1/1024) // 1/32 1/64 etc also work
207#define RPFLOAT(a) (a=rint(a/PFLOAT)*PFLOAT)
210{
211 RPFLOAT(v.x); RPFLOAT(v.y); RPFLOAT(v.z);
212 return v;
213}
214
217{
218 v.x = rint(v.x); v.y = rint(v.y); v.z = rint(v.z);
219 return v;
220}
221
222#endif
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
v y
Definition ent_cs.qc:121
#define ERASEABLE
Definition _all.inc:37
bool isnan(float e)
Definition mathlib.qc:25
#define M_PI
Definition mathlib.qh:108
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:92
vector org
Definition self.qh:92
int dir
Definition impulse.qc:89
const vector eY
Definition vector.qh:45
ERASEABLE vector randompos(vector m1, vector m2)
Definition vector.qh:49
ERASEABLE vector vrint(vector v)
Definition vector.qh:216
noref vector _vmul_a
Definition vector.qh:37
const vector eZ
Definition vector.qh:46
noref vector _vmul_b
Definition vector.qh:37
#define RPFLOAT(a)
Definition vector.qh:207
ERASEABLE vector NearestPointOnBoundingBox(vector mi, vector ma, vector org)
Definition vector.qh:194
ERASEABLE vector reflect(vector dir, vector norm)
Definition vector.qh:136
#define cross(a, b)
Definition vector.qh:25
ERASEABLE vector point_line_vec(vector p, vector l0, vector ldir)
Definition vector.qh:122
ERASEABLE vector RoundPerfectVector(vector v)
Definition vector.qh:209
ERASEABLE vector NearestPointOnBox(entity box, vector org)
Definition vector.qh:181
noref vector _vec2
Definition vector.qh:89
ERASEABLE vector get_corner_position(entity box, int corner)
Definition vector.qh:164
ERASEABLE bool is_all_nans(vector v)
Definition vector.qh:100
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:73
ERASEABLE float vlen_maxnorm2d(vector v)
Definition vector.qh:60
ERASEABLE vector vec_epsilon(vector this, float eps)
Definition vector.qh:151
ERASEABLE float boxinsidebox(vector smins, vector smaxs, vector bmins, vector bmaxs)
requires the same as boxesoverlap, but is a stronger condition
Definition vector.qh:77
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:145
noref vector _yinvert
Definition vector.qh:114
noref vector _vec3
Definition vector.qh:94
noref vector _vlen2
Definition vector.qh:3
ERASEABLE vector Rotate(vector v, float a)
Definition vector.qh:105
ERASEABLE float vlen_minnorm2d(vector v)
Definition vector.qh:66
const vector eX
Definition vector.qh:44
#define vec2(...)
Definition vector.qh:90
#define vec3(_x, _y, _z)
Definition vector.qh:95