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
37var vector g_vectorCacheUp;
38
39static void
40anglesMake(vector angle)
41{
42 makevectors(angle);
43 g_vectorCacheLast = angle;
47}
48
54vector
55anglesToForward(vector angle)
56{
57 if (angle == g_vectorCacheLast) {
59 }
60
61 anglesMake(angle);
63}
64
70vector
71anglesToRight(vector angle)
72{
73 if (angle == g_vectorCacheLast) {
74 return g_vectorCacheRight;
75 }
76
77 anglesMake(angle);
78 return g_vectorCacheRight;
79}
80
86vector
87anglesToUp(vector angle)
88{
89 if (angle == g_vectorCacheLast) {
90 return g_vectorCacheUp;
91 }
92
93 anglesMake(angle);
94 return g_vectorCacheUp;
95}
96
102float
103distanceSquared(vector pointA, vector pointB)
104{
105 float diffX = pointA[0] - pointB[0];
106 float diffY = pointA[1] - pointB[1];
107 float diffZ = pointA[2] - pointB[2];
108 return (diffX * diffX) + (diffY * diffY) + (diffZ * diffZ);
109}
110
116float
117distance(vector pointA, vector pointB)
118{
119 return sqrt(distanceSquared(pointA, pointB));
120}
121
127float
128distance2D(vector pointA, vector pointB)
129{
130 float diffX = pointA[0] - pointB[0];
131 float diffY = pointA[1] - pointB[1];
132 return sqrt((diffX * diffX) + (diffY * diffY));
133}
134
141bool
142closer(vector referencePoint, vector pointA, vector pointB)
143{
144 float distanceA = distanceSquared(referencePoint, pointA);
145 float distanceB = distanceSquared(referencePoint, pointA);
146 return (distanceA < distanceB) ? true : false;
147}
148
153vector
154combineAngles(vector angleA, vector angleB)
155{
156 makevectors(angleA);
157 rotatevectorsbyangle(angleB);
158 return vectoangles(v_forward, v_up);
159}
160
165float
166length(vector toCalculate)
167{
168 static vector lastInput = g_vec_null;
169 static float lastOutput = 0.0f;
170
171 if (toCalculate == lastInput) {
172 return lastOutput;
173 }
174
175 lastInput = toCalculate;
176 lastOutput = vlen(toCalculate);
177 return lastOutput;
178}
179
184float
186{
187 return (target[0] * target[0]) + (target[1] * target[1]) + (target[2] * target[2]);
188}
189
195float
196vectorDot(vector vectorA, vector vectorB)
197{
198 return dotproduct(vectorA, vectorB);
199}
200
207vector
208vectorLerp(vector fromVector, vector toVector, float lerpFraction)
209{
210 static float vectorLerp_Lerp( float fA, float fB, float fPercent) {
211 return (fA * (1 - fPercent)) + (fB * fPercent);
212 }
213
214 vector outputVector = g_vec_null;
215 outputVector[0] = vectorLerp_Lerp(fromVector[0], toVector[0], lerpFraction);
216 outputVector[1] = vectorLerp_Lerp(fromVector[1], toVector[1], lerpFraction);
217 outputVector[2] = vectorLerp_Lerp(fromVector[2], toVector[2], lerpFraction);
218 return outputVector;
219}
220
225vector
226vectorNormalize(vector toNormalize)
227{
228 static vector lastInput = g_vec_null;
229 static vector lastOutput = g_vec_null;
230
231 if (toNormalize == lastInput) {
232 return lastOutput;
233 }
234
235 lastInput = toNormalize;
236 lastOutput = normalize(toNormalize);
237 return lastOutput;
238}
239
244vector
245vectorToAngles(vector toAngles)
246{
247 static vector lastInput = g_vec_null;
248 static vector lastOutput = g_vec_null;
249
250 if (toAngles == lastInput) {
251 return lastOutput;
252 }
253
254 lastInput = toAngles;
255 lastOutput = fixAngle(vectoangles(toAngles));
256 return lastOutput;
257}
258
264vector
265vectorToAnglesRoll(vector forwardDir, vector rollDir)
266{
267 static vector lastInput = g_vec_null;
268 static vector lastInput2 = g_vec_null;
269 static vector lastOutput = g_vec_null;
270
271 if (forwardDir == lastInput && rollDir == lastInput2) {
272 return lastOutput;
273 }
274
275 lastInput = forwardDir;
276 lastInput2 = rollDir;
277 lastOutput = fixAngle(vectoangles(forwardDir, rollDir));
278 return lastOutput;
279}
280
287vector
288lerpAngleVector(vector inputAngle, vector endAngle, float lerpAmount)
289{
290 vector currentDir = anglesToForward(inputAngle);
291 vector desiredDir = anglesToForward(endAngle);
292 return vectorToAngles(vectorLerp(currentDir, desiredDir, lerpAmount));
293}
294
300vector
301dirFromTarget(vector lookingEnt, vector targetEnt)
302{
303 return vectorNormalize(vectorToAngles(targetEnt - lookingEnt));
304}
305 // end of multiprogs, server
const vector g_vec_null
Definition: global.h:111
vector v_up
Definition: math.h:46
vector anglesToUp(vector angle)
Calculates the up vector of a set of euler-angles.
Definition: math_vector.h:87
vector vectorLerp(vector fromVector, vector toVector, float lerpFraction)
Calculates an interpolated (linear) point between two points.
Definition: math_vector.h:208
vector combineAngles(vector angleA, vector angleB)
Calculates a set of angles from a given vector.
Definition: math_vector.h:154
float length(vector toCalculate)
Calculates accurate length of a given vector.
Definition: math_vector.h:166
#define vectoangles
Definition: math.h:43
float distance2D(vector pointA, vector pointB)
Calculates the distance between two vectors, ignoring the height difference between them.
Definition: math_vector.h:128
float distance(vector pointA, vector pointB)
Calculates the distance between two points.
Definition: math_vector.h:117
vector dirFromTarget(vector lookingEnt, vector targetEnt)
Definition: math_vector.h:301
float vectorDot(vector vectorA, vector vectorB)
Calculate the dot product of two vectors.
Definition: math_vector.h:196
var vector g_vectorCacheRight
Definition: math_vector.h:36
vector vectorToAnglesRoll(vector forwardDir, vector rollDir)
Calculates a set of angles from a given vector, with roll support.
Definition: math_vector.h:265
bool closer(vector referencePoint, vector pointA, vector pointB)
Figure out which point is the closest between two options.
Definition: math_vector.h:142
vector vectorToAngles(vector toAngles)
Calculates a set of angles from a given vector.
Definition: math_vector.h:245
vector anglesToRight(vector angle)
Calculates the right vector of a set of euler-angles.
Definition: math_vector.h:71
void rotatevectorsbyangle(vector angle)
float lengthSquared(vector target)
Calculates the length of a given vector using a dot product.
Definition: math_vector.h:185
var vector g_vectorCacheForward
Definition: math_vector.h:35
vector v_right
Definition: math.h:47
var vector g_vectorCacheUp
Definition: math_vector.h:37
var vector g_vectorCacheLast
Definition: math_vector.h:34
vector lerpAngleVector(vector inputAngle, vector endAngle, float lerpAmount)
Lerps between two angles.
Definition: math_vector.h:288
float distanceSquared(vector pointA, vector pointB)
Calculates the squared distance between two points.
Definition: math_vector.h:103
vector anglesToForward(vector angle)
Calculates the forward vector of a set of euler-angles.
Definition: math_vector.h:55
vector v_forward
Definition: math.h:45
void makevectors(vector angles)
Definition: math.h:48
vector fixAngle(vector inputAngle)
Recursive function that fixes euler angles.
Definition: math.h:127
vector vectorNormalize(vector toNormalize)
Calculates a normalized version of a given vector.
Definition: math_vector.h:226
string target
Definition: defs.h:23