Difference between revisions of "Vector"
		
		
		
		
		
		Jump to navigation
		Jump to search
		
				
		
		
	
Outerbeast (talk | contribs)  (Created page with "Vectors are essentially float arrays of size 3 which are used to represent various things like spatial co-ordinates, angles, velocities and colours, and so will come up very o...")  | 
				Outerbeast (talk | contribs)   | 
				||
| (12 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
| − | Vectors are essentially float arrays of size 3 which are used to represent various   | + | Vectors are essentially float arrays of size 3 which are used to represent various quanties like spatial co-ordinates, angles, velocities and colours, and so will come up very often.  | 
| + | A vector can be instanced with the <code>Vector</code> data type, like so:  | ||
| + | <pre>Vector vec(1, 2, 3);</pre>  | ||
| + | Each vector component can be accessed via the fields x, y and z, which are of type <code>float</code>  | ||
| + | <pre>  | ||
| + | vec.x // 1.0  | ||
| + | vec.y // 2.0  | ||
| + | vec.z // 3.0  | ||
| + | </pre>  | ||
| + | |||
| + | These components are also accessible via the index operator:  | ||
| + | <pre>  | ||
| + | vec[0] // vec.x  | ||
| + | vec[1] // vec.y  | ||
| + | vec[2] // vec.z  | ||
| + | </pre>  | ||
| + | |||
| + | There is also a <code>Vector2D</code> which simply has the <code>z</code> component absent but its usage is identical.  | ||
| + | |||
| + | Operations such as dot product and cross product can be performed on vectors using these global functions:  | ||
| + | |||
| + | {| class="wikitable"   | ||
| + | |- style="font-weight:bold; text-align:center; vertical-align:middle;"  | ||
| + | ! Function  | ||
| + | ! Description  | ||
| + | |- style="vertical-align:middle; background-color:#F8F9FA; color:#202122;"  | ||
| + | | <code>float DotProduct(const Vector2D& in lhs, const Vector2D& in rhs)  | ||
| + | | Returns a dot product of the given 2D vectors  | ||
| + | |- style="background-color:#F8F9FA; color:#202122;"  | ||
| + | | style="vertical-align:middle;" | <code>float DotProduct(const Vector& in lhs, const Vector& in rhs)  | ||
| + | | Returns the dot product of the given vectors  | ||
| + | |- style="background-color:#F8F9FA; color:#202122;"  | ||
| + | | <code>Vector CrossProduct(const Vector& in, const Vector& in)  | ||
| + | | style="vertical-align:middle;" | Returns the cross product of the given vectors  | ||
| + | |}  | ||
| + | == Methods ==  | ||
{| class="wikitable"    | {| class="wikitable"    | ||
|-  | |-  | ||
| Line 6: | Line 41: | ||
! style="vertical-align:middle;" | Description  | ! style="vertical-align:middle;" | Description  | ||
|-  | |-  | ||
| − | | void Vector()  | + | | <code>void Vector()  | 
| style="vertical-align:middle;" | Default constructs a 3D vector (0, 0, 0)  | | style="vertical-align:middle;" | Default constructs a 3D vector (0, 0, 0)  | ||
|-  | |-  | ||
| − | | void Vector(const Vector& in vec)  | + | | <code>void Vector(const Vector& in vec)  | 
| style="vertical-align:middle;" | Copy constructs a 3D vector  | | style="vertical-align:middle;" | Copy constructs a 3D vector  | ||
|-  | |-  | ||
| − | | void Vector(float x, float y, float z)  | + | | <code>void Vector(float x, float y, float z)  | 
| style="vertical-align:middle;" | Constructs a 3D vector from 3 floats  | | style="vertical-align:middle;" | Constructs a 3D vector from 3 floats  | ||
|-  | |-  | ||
| − | | Vector& opAssign(const Vector& in other)  | + | | <code>Vector& opAssign(const Vector& in other)  | 
| style="vertical-align:middle;" | Assign vector  | | style="vertical-align:middle;" | Assign vector  | ||
|-  | |-  | ||
| − | | Vector opNeg() const  | + | | <code>Vector opNeg() const  | 
| style="vertical-align:middle;" | Negate vector  | | style="vertical-align:middle;" | Negate vector  | ||
|-  | |-  | ||
| − | | Vector opAdd(const Vector& in other) const  | + | | <code>Vector opAdd(const Vector& in other) const  | 
| style="vertical-align:middle;" | Add vectors  | | style="vertical-align:middle;" | Add vectors  | ||
|-  | |-  | ||
| − | | Vector opSub(const Vector& in other) const  | + | | <code>Vector opSub(const Vector& in other) const  | 
| style="vertical-align:middle;" | Subtract vectors  | | style="vertical-align:middle;" | Subtract vectors  | ||
|-  | |-  | ||
| − | | Vector opMul(float fl) const  | + | | <code>Vector opMul(float fl) const  | 
| style="vertical-align:middle;" | Multiply vector by value  | | style="vertical-align:middle;" | Multiply vector by value  | ||
|-  | |-  | ||
| − | | Vector opMul_r(float fl) const  | + | | <code>Vector opMul_r(float fl) const  | 
| style="vertical-align:middle;" | Multiply vector by value  | | style="vertical-align:middle;" | Multiply vector by value  | ||
|-  | |-  | ||
| − | | Vector opDiv(float fl) const  | + | | <code>Vector opDiv(float fl) const  | 
| style="vertical-align:middle;" | Divide vector by value  | | style="vertical-align:middle;" | Divide vector by value  | ||
|-  | |-  | ||
| − | | Vector opDiv_r(float fl) const  | + | | <code>Vector opDiv_r(float fl) const  | 
| style="vertical-align:middle;" | Divide vector by value  | | style="vertical-align:middle;" | Divide vector by value  | ||
|-  | |-  | ||
| − | | float& opIndex(size_t uiIndex)  | + | | <code>float& opIndex(size_t uiIndex)  | 
| style="vertical-align:middle;" | Index operator  | | style="vertical-align:middle;" | Index operator  | ||
|-  | |-  | ||
| − | | float opIndex(size_t uiIndex) const  | + | | <code>float opIndex(size_t uiIndex) const  | 
| style="vertical-align:middle;" | Index operator  | | style="vertical-align:middle;" | Index operator  | ||
|-  | |-  | ||
| − | | bool opEquals(const Vector& in other) const  | + | | <code>bool opEquals(const Vector& in other) const  | 
| style="vertical-align:middle;" | Compare vectors  | | style="vertical-align:middle;" | Compare vectors  | ||
|-  | |-  | ||
| − | | Vector opMul(const Vector& in other) const  | + | | <code>Vector opMul(const Vector& in other) const  | 
| style="vertical-align:middle;" | Multiply vectors  | | style="vertical-align:middle;" | Multiply vectors  | ||
|-  | |-  | ||
| − | | Vector opDiv(const Vector& in other) const  | + | | <code>Vector opDiv(const Vector& in other) const  | 
| style="vertical-align:middle;" | Divide vectors  | | style="vertical-align:middle;" | Divide vectors  | ||
|-  | |-  | ||
| − | | float Length() const  | + | | <code>float Length() const  | 
| style="vertical-align:middle;" | Gets the length of this vector  | | style="vertical-align:middle;" | Gets the length of this vector  | ||
|-  | |-  | ||
| − | | float Length2D() const  | + | | <code>float Length2D() const  | 
| style="vertical-align:middle;" | Gets the length of this vector in 2D  | | style="vertical-align:middle;" | Gets the length of this vector in 2D  | ||
|-  | |-  | ||
| − | | Vector Normalize() const  | + | | <code>Vector Normalize() const  | 
| style="vertical-align:middle;" | Returns the normalized form of this vector  | | style="vertical-align:middle;" | Returns the normalized form of this vector  | ||
|-  | |-  | ||
| − | | Vector2D Make2D() const  | + | | <code>Vector2D Make2D() const  | 
| style="vertical-align:middle;" | Returns the 2D form of this vector  | | style="vertical-align:middle;" | Returns the 2D form of this vector  | ||
|-  | |-  | ||
| − | | string ToString() const  | + | | <code>string ToString() const  | 
| − | | style="vertical-align:middle;" | Returns a string representation of this vector in the format "x, y, z"  | + | | style="vertical-align:middle;" | Returns a string representation of this vector in the format <code>"x, y, z"</code>  | 
| + | |}  | ||
| + | |||
| + | == Constants ==  | ||
| + | These constants exist in global scope.  | ||
| + | {| class="wikitable"  | ||
| + | |-  | ||
| + | ! Constant  | ||
| + | ! Value  | ||
| + | |-  | ||
| + | | <code>const Vector g_vecZero  | ||
| + | | style="vertical-align:middle;" | Zero vector (0, 0, 0)  | ||
| + | |-  | ||
| + | | <code>const Vector VEC_HULL_MIN  | ||
| + | | style="vertical-align:middle;" | Default hull minimum. Used with [[CEntityFuncs#SetSize|CEntityFuncs::SetSize]]<br />Value: (-16.0, -16.0, -36.0)  | ||
| + | |-  | ||
| + | | <code>const Vector VEC_HULL_MAX  | ||
| + | | Default hull maximum. Used with [[CEntityFuncs#SetSize|CEntityFuncs::SetSize]]<br />Value: (16.0, 16.0, 36.0)  | ||
| + | |-  | ||
| + | | <code>const Vector VEC_HUMAN_HULL_MIN  | ||
| + | | Default human hull maximum. Used with [[CEntityFuncs#SetSize|CEntityFuncs::SetSize]]<br />Value: (-16.0, -16.0, 0.0)  | ||
| + | |-  | ||
| + | | <code>const Vector VEC_HUMAN_HULL_MAX  | ||
| + | | Default human hull maximum while standing. Used with [[CEntityFuncs#SetSize|CEntityFuncs::SetSize]]<br/>Value: (16.0, 16.0, 72.0)  | ||
| + | |-  | ||
| + | | <code>const Vector VEC_HUMAN_HULL_DUCK  | ||
| + | | Default human hull maximum while ducking. Used with [[CEntityFuncs#SetSize|CEntityFuncs::SetSize]]<br />Value: (16.0, 16.0, 36.0)  | ||
| + | |-  | ||
| + | | <code>const Vector VEC_VIEW  | ||
| + | | View offset.<br />Value: (0.0, 0.0, 28.0)  | ||
| + | |-  | ||
| + | | <code>const Vector VEC_DUCK_HULL_MIN  | ||
| + | | Hull minimum while ducking. Used with [[CEntityFuncs#SetSize|CEntityFuncs::SetSize]]<br />Value: (-16.0, -16.0, -18.0)  | ||
| + | |-  | ||
| + | | <code>const Vector VEC_DUCK_HULL_MAX  | ||
| + | | Hull maximum while ducking. Used with [[CEntityFuncs#SetSize|CEntityFuncs::SetSize]]<br />Value: (16.0, 16.0, 18.0)  | ||
| + | |-  | ||
| + | | <code>const Vector VEC_DUCK_VIEW  | ||
| + | | Hull view offset while ducking.<br />Value: (0.0, 0.0, 12.0)  | ||
| + | |-  | ||
| + | | <code>const Vector VECTOR_CONE_9DEGREES  | ||
| + | | style="vertical-align:middle;" | (0.078460, 0.078460, 0.078460)  | ||
| + | |-  | ||
| + | | <code>const Vector VECTOR_CONE_10DEGREES  | ||
| + | | style="vertical-align:middle;" | (0.087160, 0.087160, 0.087160)  | ||
| + | |-  | ||
| + | | <code>const Vector VECTOR_CONE_15DEGREES  | ||
| + | | style="vertical-align:middle;" | (0.130530, 0.130530, 0.130530)  | ||
| + | |-  | ||
| + | | <code>const Vector VECTOR_CONE_20DEGREES  | ||
| + | | style="vertical-align:middle;" | (0.173650, 0.173650, 0.173650)  | ||
|}  | |}  | ||
| + | [[Category:Scripting]]  | ||
Latest revision as of 18:09, 27 March 2025
Vectors are essentially float arrays of size 3 which are used to represent various quanties like spatial co-ordinates, angles, velocities and colours, and so will come up very often.
A vector can be instanced with the Vector data type, like so:
Vector vec(1, 2, 3);
Each vector component can be accessed via the fields x, y and z, which are of type float
vec.x // 1.0 vec.y // 2.0 vec.z // 3.0
These components are also accessible via the index operator:
vec[0] // vec.x vec[1] // vec.y vec[2] // vec.z
There is also a Vector2D which simply has the z component absent but its usage is identical.
Operations such as dot product and cross product can be performed on vectors using these global functions:
| Function | Description | 
|---|---|
float DotProduct(const Vector2D& in lhs, const Vector2D& in rhs)
 | 
Returns a dot product of the given 2D vectors | 
float DotProduct(const Vector& in lhs, const Vector& in rhs)
 | 
Returns the dot product of the given vectors | 
Vector CrossProduct(const Vector& in, const Vector& in)
 | 
Returns the cross product of the given vectors | 
1 Methods
| Method | Description | 
|---|---|
void Vector()
 | 
Default constructs a 3D vector (0, 0, 0) | 
void Vector(const Vector& in vec)
 | 
Copy constructs a 3D vector | 
void Vector(float x, float y, float z)
 | 
Constructs a 3D vector from 3 floats | 
Vector& opAssign(const Vector& in other)
 | 
Assign vector | 
Vector opNeg() const
 | 
Negate vector | 
Vector opAdd(const Vector& in other) const
 | 
Add vectors | 
Vector opSub(const Vector& in other) const
 | 
Subtract vectors | 
Vector opMul(float fl) const
 | 
Multiply vector by value | 
Vector opMul_r(float fl) const
 | 
Multiply vector by value | 
Vector opDiv(float fl) const
 | 
Divide vector by value | 
Vector opDiv_r(float fl) const
 | 
Divide vector by value | 
float& opIndex(size_t uiIndex)
 | 
Index operator | 
float opIndex(size_t uiIndex) const
 | 
Index operator | 
bool opEquals(const Vector& in other) const
 | 
Compare vectors | 
Vector opMul(const Vector& in other) const
 | 
Multiply vectors | 
Vector opDiv(const Vector& in other) const
 | 
Divide vectors | 
float Length() const
 | 
Gets the length of this vector | 
float Length2D() const
 | 
Gets the length of this vector in 2D | 
Vector Normalize() const
 | 
Returns the normalized form of this vector | 
Vector2D Make2D() const
 | 
Returns the 2D form of this vector | 
string ToString() const
 | 
Returns a string representation of this vector in the format "x, y, z"
 | 
2 Constants
These constants exist in global scope.
| Constant | Value | 
|---|---|
const Vector g_vecZero
 | 
Zero vector (0, 0, 0) | 
const Vector VEC_HULL_MIN
 | 
Default hull minimum. Used with CEntityFuncs::SetSize Value: (-16.0, -16.0, -36.0)  | 
const Vector VEC_HULL_MAX
 | 
Default hull maximum. Used with CEntityFuncs::SetSize Value: (16.0, 16.0, 36.0)  | 
const Vector VEC_HUMAN_HULL_MIN
 | 
Default human hull maximum. Used with CEntityFuncs::SetSize Value: (-16.0, -16.0, 0.0)  | 
const Vector VEC_HUMAN_HULL_MAX
 | 
Default human hull maximum while standing. Used with CEntityFuncs::SetSize Value: (16.0, 16.0, 72.0)  | 
const Vector VEC_HUMAN_HULL_DUCK
 | 
Default human hull maximum while ducking. Used with CEntityFuncs::SetSize Value: (16.0, 16.0, 36.0)  | 
const Vector VEC_VIEW
 | 
View offset. Value: (0.0, 0.0, 28.0)  | 
const Vector VEC_DUCK_HULL_MIN
 | 
Hull minimum while ducking. Used with CEntityFuncs::SetSize Value: (-16.0, -16.0, -18.0)  | 
const Vector VEC_DUCK_HULL_MAX
 | 
Hull maximum while ducking. Used with CEntityFuncs::SetSize Value: (16.0, 16.0, 18.0)  | 
const Vector VEC_DUCK_VIEW
 | 
Hull view offset while ducking. Value: (0.0, 0.0, 12.0)  | 
const Vector VECTOR_CONE_9DEGREES
 | 
(0.078460, 0.078460, 0.078460) | 
const Vector VECTOR_CONE_10DEGREES
 | 
(0.087160, 0.087160, 0.087160) | 
const Vector VECTOR_CONE_15DEGREES
 | 
(0.130530, 0.130530, 0.130530) | 
const Vector VECTOR_CONE_20DEGREES
 | 
(0.173650, 0.173650, 0.173650) |