-
Notifications
You must be signed in to change notification settings - Fork 2
/
vector.qc
163 lines (131 loc) · 2.85 KB
/
vector.qc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const vector vec3_origin = '0 0 0';
enum : int { PITCH, YAW, ROLL };
inline float(vector v) VectorLengthSquared =
{
return v * v;
}
inline float(vector v) VectorLength =
{
return sqrt(VectorLengthSquared(v));
}
INLINE float(inout vector v) VectorNormalize =
{
float length = VectorLength(v);
if (length)
v *= 1.f / length;
return length;
}
inline float(vector a, vector b) VectorDistance =
{
return VectorLength(a - b);
}
inline float(vector a, vector b) VectorDistanceSquared =
{
return VectorLengthSquared(a - b);
}
/*
==================
ClipVelocity
Slide off of the impacting object
==================
*/
// This constant is used for how steep a ground plane is for
// bouncing things.
const float STOP_EPSILON = 0.1f;
vector(vector in, vector normal, float overbounce) ClipVelocity =
{
float backoff = (in * normal) * overbounce;
vector out = vec3_origin;
for (int i = 0; i < 3; i++)
{
float change = normal[i]*backoff;
out[i] = in[i] - change;
if (out[i] > -STOP_EPSILON && out[i] < STOP_EPSILON)
out[i] = 0;
}
return out;
}
void(vector angles, vector *forward, vector *right, vector *up) AngleVectors =
{
float angle = DEG2RAD(angles[YAW]);
float sy = sin(angle);
float cy = cos(angle);
float angle = DEG2RAD(angles[PITCH]);
float sp = sin(angle);
float cp = cos(angle);
if (forward)
*forward = [ cp * cy, cp * sy, -sp ];
if (right || up)
{
float angle = DEG2RAD(angles[ROLL]);
float sr = sin(angle);
float cr = cos(angle);
if (right)
*right = [
(-1 * sr * sp * cy + -1 * cr * -sy),
(-1 * sr * sp * sy + -1 * cr * cy),
-1 * sr * cp
];
if (up)
*up = [
(cr * sp * cy + -sr * -sy),
(cr * sp * sy + -sr * cy),
cr * cp
];
}
}
vector(vector value1) vectoangles =
{
if (value1[1] == 0 && value1[0] == 0)
{
if (value1[2] > 0)
return [ -90f, 0, 0 ];
return [ -270f, 0, 0 ];
}
float yaw;
if (value1[0])
{
yaw = RAD2DEG(atan2(value1[1], value1[0]));
if (yaw < 0)
yaw += 360;
}
else if (value1[1] > 0)
yaw = 90f;
else
yaw = 270f;
const float forward = sqrt(value1[0]*value1[0] + value1[1]*value1[1]);
float pitch = RAD2DEG(atan2(value1[2], forward));
if (pitch < 0)
pitch += 360;
return [ -pitch, yaw, 0 ];
}
float(vector vec) vectoyaw =
{
if (vec.x == 0)
{
if (vec.y > 0)
return 90f;
else if (vec.y < 0)
return -90f;
return 0;
}
float yaw = RAD2DEG(atan2(vec.y, vec.x));
if (yaw < 0)
yaw += 360f;
return yaw;
}
inline vector(vector v1, vector v2) CrossProduct =
{
return [v1[1] * v2[2] - v1[2] * v2[1],
v1[2] * v2[0] - v1[0] * v2[2],
v1[0] * v2[1] - v1[1] * v2[0]];
}
inline void(vector v, inout vector mins, inout vector maxs) AddPointToBounds =
{
mins[0] = minf(mins[0], v[0]);
maxs[0] = maxf(maxs[0], v[0]);
mins[1] = minf(mins[1], v[1]);
maxs[1] = maxf(maxs[1], v[1]);
mins[2] = minf(mins[2], v[2]);
maxs[2] = maxf(maxs[2], v[2]);
}