Nuclide
Software Development Kit for id Tech
math_vector.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Vera Visions LLC.
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
13 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
14 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*/
16
32
33static void
34anglesMake(vector angle)
35{
36 makevectors(angle);
37 g_vectorCacheLast = angle;
41}
42
50{
51 if (angle == g_vectorCacheLast) {
53 }
54
55 anglesMake(angle);
57}
58
66{
67 if (angle == g_vectorCacheLast) {
68 return g_vectorCacheRight;
69 }
70
71 anglesMake(angle);
72 return g_vectorCacheRight;
73}
74
82{
83 if (angle == g_vectorCacheLast) {
84 return g_vectorCacheUp;
85 }
86
87 anglesMake(angle);
88 return g_vectorCacheUp;
89}
90
96float
98{
99 float diffX = pointA[0] - pointB[0];
100 float diffY = pointA[1] - pointB[1];
101 float diffZ = pointA[2] - pointB[2];
102 return (diffX * diffX) + (diffY * diffY) + (diffZ * diffZ);
103}
104
110float
111distance(vector pointA, vector pointB)
112{
113 return sqrt(distanceSquared(pointA, pointB));
114}
115
121float
122distance2D(vector pointA, vector pointB)
123{
124 float diffX = pointA[0] - pointB[0];
125 float diffY = pointA[1] - pointB[1];
126 return sqrt((diffX * diffX) + (diffY * diffY));
127}
128
135bool
136closer(vector referencePoint, vector pointA, vector pointB)
137{
138 float distanceA = distanceSquared(referencePoint, pointA);
139 float distanceB = distanceSquared(referencePoint, pointA);
140 return (distanceA < distanceB) ? true : false;
141}
142
147vector
149{
150 makevectors(angleA);
151 rotatevectorsbyangle(angleB);
152 return vectoangles(v_forward, v_up);
153}
154
159float
160length(vector toCalculate)
161{
162 static vector lastInput = g_vec_null;
163 static float lastOutput = 0.0f;
164
165 if (toCalculate == lastInput) {
166 return lastOutput;
167 }
168
169 lastInput = toCalculate;
170 lastOutput = vlen(toCalculate);
171 return lastOutput;
172}
173
178float
180{
181 return (target[0] * target[0]) + (target[1] * target[1]) + (target[2] * target[2]);
182}
183
189float
190vectorDot(vector vectorA, vector vectorB)
191{
192 return dotproduct(vectorA, vectorB);
193}
194
201vector
202vectorLerp(vector fromVector, vector toVector, float lerpFraction)
203{
204 static float vectorLerp_Lerp( float fA, float fB, float fPercent) {
205 return (fA * (1 - fPercent)) + (fB * fPercent);
206 }
207
208 vector outputVector = g_vec_null;
209 outputVector[0] = vectorLerp_Lerp(fromVector[0], toVector[0], lerpFraction);
210 outputVector[1] = vectorLerp_Lerp(fromVector[1], toVector[1], lerpFraction);
211 outputVector[2] = vectorLerp_Lerp(fromVector[2], toVector[2], lerpFraction);
212 return outputVector;
213}
214
219vector
221{
222 static vector lastInput = g_vec_null;
223 static vector lastOutput = g_vec_null;
224
225 if (toNormalize == lastInput) {
226 return lastOutput;
227 }
228
229 lastInput = toNormalize;
230 lastOutput = normalize(toNormalize);
231 return lastOutput;
232}
233
238vector
240{
241 static vector lastInput = g_vec_null;
242 static vector lastOutput = g_vec_null;
243
244 if (toAngles == lastInput) {
245 return lastOutput;
246 }
247
248 lastInput = toAngles;
249 lastOutput = fixAngle(vectoangles(toAngles));
250 return lastOutput;
251}
252
256vector
257vectorToAnglesRoll(vector forwardDir, vector rollDir)
258{
259 static vector lastInput = g_vec_null;
260 static vector lastInput2 = g_vec_null;
261 static vector lastOutput = g_vec_null;
262
263 if (forwardDir == lastInput && rollDir == lastInput2) {
264 return lastOutput;
265 }
266
267 lastInput = forwardDir;
268 lastInput2 = rollDir;
269 lastOutput = fixAngle(vectoangles(forwardDir, rollDir));
270 return lastOutput;
271}
272
273vector
274lerpAngleVector(vector inputAngle, vector endAngle, float lerpAmount)
275{
276 vector currentDir = anglesToForward(inputAngle);
277 vector desiredDir = anglesToForward(endAngle);
278 return vectorToAngles(vectorLerp(currentDir, desiredDir, lerpAmount));
279}
#define dotproduct(v1, v2)
Definition: fteextensions.qc:2464
vector(vector) normalize
@ true
Definition: fteextensions.qc:3840
const vector g_vec_null
Definition: global.h:101
string target
Definition: defs.h:23
vector v_up
Definition: math.h:40
#define vectoangles
Definition: math.h:37
vector v_right
Definition: math.h:41
vector v_forward
Definition: math.h:39
void makevectors(vector angles)
Definition: math.h:42
vector fixAngle(vector inputAngle)
Recursive function that fixes euler angles.
Definition: math.h:121
vector anglesToUp(vector angle)
Calculates the up vector of a set of euler-angles.
Definition: math_vector.h:81
vector vectorLerp(vector fromVector, vector toVector, float lerpFraction)
Calculates an interpolated (linear) point between two points.
Definition: math_vector.h:202
vector combineAngles(vector angleA, vector angleB)
Calculates a set of angles from a given vector.
Definition: math_vector.h:148
float length(vector toCalculate)
Calculates accurate length of a given vector.
Definition: math_vector.h:160
float distance2D(vector pointA, vector pointB)
Calculates the distance between two vectors, ignoring the height difference between them.
Definition: math_vector.h:122
float distance(vector pointA, vector pointB)
Calculates the distance between two points.
Definition: math_vector.h:111
float vectorDot(vector vectorA, vector vectorB)
Calculate the dot product of two vectors.
Definition: math_vector.h:190
var vector g_vectorCacheRight
Definition: math_vector.h:30
vector vectorToAnglesRoll(vector forwardDir, vector rollDir)
Calculates a set of angles from a given vector, with roll support.
Definition: math_vector.h:257
bool closer(vector referencePoint, vector pointA, vector pointB)
Figure out which point is the closest between two options.
Definition: math_vector.h:136
vector vectorToAngles(vector toAngles)
Calculates a set of angles from a given vector.
Definition: math_vector.h:239
vector anglesToRight(vector angle)
Calculates the right vector of a set of euler-angles.
Definition: math_vector.h:65
float lengthSquared(vector target)
Calculates the length of a given vector using a dot product.
Definition: math_vector.h:179
var vector g_vectorCacheForward
Definition: math_vector.h:29
var vector g_vectorCacheUp
Definition: math_vector.h:31
var vector g_vectorCacheLast
Definition: math_vector.h:28
vector lerpAngleVector(vector inputAngle, vector endAngle, float lerpAmount)
Definition: math_vector.h:274
float distanceSquared(vector pointA, vector pointB)
Calculates the squared distance between two points.
Definition: math_vector.h:97
vector anglesToForward(vector angle)
Calculates the forward vector of a set of euler-angles.
Definition: math_vector.h:49
vector vectorNormalize(vector toNormalize)
Calculates a normalized version of a given vector.
Definition: math_vector.h:220