Src/idlib/math/vectors.h (source file)
From modwiki
The Doom 3 engine uses vectors for many things, including forces of movement, direction of view, and even plain variable storage in some cases. In the engine, a vector can be represented as
idVec2 - x,y idVec3 - x,y,z idVec4 - x,y,z,w idVec5 - x,y,z,s,t idVec6 - p[0],p[1],p[2],p[3],p[4],p[5] idVecX - p[0]-p[x-1]
Where idVec 2-6 represents 2d-6d vectors, respectively, and idVecX is an arbitrarily sized vector.
The components of the vector are public floats, so accessing them is easy enough:
idVec3 myvector;
myvector.x = 1.4;
myvector.y = 2;
myvector.z = 25.99;
The vectors all have overloaded operators, although some (namely idVec5) seem to use only a few. Here is a table describing the arguments, function, and datatype of operators used with idVec2 and idVec3, the more frequently used vector types:
| operator | Arguments | Description | Returns |
|---|---|---|---|
| [ ] | int index | Returns component index | float& |
| - | none | Multiply all components by -1 | idVecN |
| * | const idVecN &a | Multiply vector components together | float |
| / | const float a | Divide all components of the vector by a | idVecN |
| + | const idVecN &a | Add components of the vectors together | idVecN |
| - | const idVecN &a | Subtract components from a from current vector (x - a.x, etc.) | idVecN |
| + = | const idVecN &a | Add components of the vectors together | idVecN & |
| - = | const idVecN &a | Subtract components from a from current vector (x - a.x, etc.) | idVecN & |
| / = | const idVecN &a | Divide components from current vector by components in a | idVecN & |
| / = | const float a | Divide all components in current vector by a | idVecN & |
| * = | const float a | Multiply all components in current vector by a | idVecN & |
| * | const float a, const idVecN b | Multiply all components of b by a | idVecN |
| = = | const idVecN &a | Non-epsilon comparison of current vector to a | bool |
| ! = | const idVecN &a | Non-epsilon inverse comparison of current vector to a | bool |
And, for your enjoyment, here is a table with all vector types, applicable functions, and descriptions of the functions.
| idVec2 | idVec3 | idVec4 | idVec5 | idVec6 | idVecX | Description | |
|---|---|---|---|---|---|---|---|
| Set | X | X | X | X | Sets the components of the vector. (float args) | ||
| Zero | X | X | X | X | X | X | Sets all components to 0. |
| Compare | X | X | X | X | X | X | Compares two vectors. (optional epsilon comparison) |
| Length | X | X | X | X | X | Returns the modulus of a vector. | |
| LengthFast | X | X | X | X | X | Returns the modulus. (less accurate) | |
| LengthSqr | X | X | X | X | X | Returns the sum of the squares of the components. (modulus squared) | |
| Normalize | X | X | X | X | X | Brings the components into 0-1 range, modulus = 1. | |
| NormalizeFast | X | X | X | X | X | Normalizes, but less accurate. | |
| Truncate | X | X | Shortens modulus to new value if applicable. | ||||
| Clamp | X | X | X | Bumps the vectors components into the supplied min-max. | |||
| Snap | X | X | Rounds the components of the vector to integers. | ||||
| SnapInt | X | X | Converts the components to ints, then back to floats. | ||||
| GetDimension | X | X | X | X | X | X | Returns the dimension of the vector. (2,3,4,5,6,x) |
| ToFloatPtr | X | X | X | X | X | X | Returns &x. (&p for idVecX) |
| ToString | X | X | X | X | X | X | Returns the vector in string format. |
| Lerp | X | X | X | X | Linearly inperpolates one vector to another. | ||
| ToYaw | X | Returns yaw value. (x and y) | |||||
| ToPitch | X | Returns pitch value. (x, y, z) | |||||
| ToAngles | X | Returns angles value. (-pitch, yaw, 0) | |||||
| ToPolar | X | Returns polar value. ((vx²+y²+z²), yaw, -pitch) | |||||
| ToMat3 | X | Returns a 3x1 matrix. (xyz) | |||||
| ToVec2 | X | X | Reinterpret_cast to idVec2. (only x/y?) | ||||
| NormalVectors | X | ||||||
| OrthagonalBasis | X | ||||||
| ProjectOntoPlane | X | ||||||
| ProjectAlongPlane | X | ||||||
| ProjectSelfOntoSphere | X | ||||||
| SLerp | X | ||||||
| FixDegenerateNormal | X | ||||||
| FixDenormals | X | ||||||
| Cross | X | ||||||
| SetSize | X | Set size(dimension) of idVecX. | |||||
| ChangeSize | X | Change size of idVecX. (expand makes new components 0) | |||||
| GetSize | X | Returns the size(dimension) of idVecX. | |||||
| SetData | X | Set 16 byte-aligned data. (?) | |||||
| Random | X | Randomize all current elements. (or a list to the specified length) | |||||
| Negate | X | Multiply all elements by -1. | |||||
| SwapElements | X | Switches index of two elements. | |||||
| SubVec3 | X | Convert to idVec3. | |||||
| SubVec6 | X | Convert to idVec6. |

