Garry's Mod Wiki

Vector

List of all possible functions to manipulate vectors.

Created by Vector.

Type Name Description
number x or 1 The X component of the vector.
number y or 2 The Y component of the vector.
number z or 3 The Z component of the vector.
Metamethod Second Operand Description
__add Vector Returns new Vector with the result of addition.
__div number Returns new Vector with the result of division.
__eq any Compares 2 operands, if they both are Vector, compares each individual component.
__index number or string Gets the component of the Vector. Returns a number.
__mul number or Vector Returns new Vector with the result of multiplication.
__newindex number or string Sets the component of the Vector. Accepts number and string.
__sub Vector Returns new Vector with the result of subtraction.
__tostring Returns x y z.
__unm Returns new Vector with the result of negation.

Methods

Vector:Add( Vector vector )
Adds the values of the argument vector to the orignal vector. This functions the same as vector1 + vector2 without creating a new vector object, skipping object construction and garbage collection.
Angle Vector:Angle()
Returns an angle representing the normal of the vector.
Angle Vector:AngleEx( Vector up )
Returns the angle of the vector, but instead of assuming that up is Vector( 0, 0, 1 ) (Like Vector:Angle does) you can specify which direction is 'up' for the angle.
Vector Vector:Cross( Vector otherVector )
Calculates the cross product of this vector and the passed one. The cross product of two vectors is a 3-dimensional vector with a direction perpendicular (at right angles) to both of them (according to the right-hand rule), and magnitude equal to the area of parallelogram they span. This is defined as the product of the magnitudes, the sine of the angle between them, and unit (normal) vector n defined by the right-hand rule: :a × b = |a| |b| sin(θ) n̂ where a and b are vectors, and n̂ is a unit vector (ma..
number Vector:Distance( Vector otherVector )
Returns the euclidean distance between the vector and the other vector. This is a relatively expensive process since it uses the square root. It is recommended that you use Vector:DistToSqr whenever possible. (Benchmarking shows Vector:DistToSqr was at most 153 nanoseconds faster.)
number Vector:DistToSqr( Vector otherVec )
Returns the squared distance of 2 vectors, this is faster than Vector:Distance as calculating the square root is an expensive process.
Vector:Div( number divisor )
Divide the vector by the given number, that means x, y and z are divided by that value. This will change the value of the original vector, see example 2 for division without changing the value.
number Vector:Dot( Vector otherVector )
Returns the dot product of this vector and the passed one. The dot product of two vectors is the product of their magnitudes (lengths), and the cosine of the angle between them: a · b = |a| |b| cos(θ) where a and b are vectors. See Vector:Length for obtaining magnitudes. A dot product returns just the cosine of the angle if both vectors are normalized, and zero if the vectors are at right angles to each other.
number Vector:DotProduct( Vector Vector )
We advise against using this. It may be changed or removed in a future update. This is an alias of Vector:Dot. Use that instead. Returns the dot product of the two vectors.
Vector Vector:GetNormal()
We advise against using this. It may be changed or removed in a future update. Use Vector:GetNormalized instead. Returns a normalized version of the vector. This is a alias of Vector:GetNormalized.
Vector Vector:GetNormalized()
Returns a normalized version of the vector. Normalized means vector with same direction but with length of 1. This does not affect the vector you call it on; to do this, use Vector:Normalize.
boolean Vector:IsEqualTol( Vector compare, number tolerance )
Returns if the vector is equal to another vector with the given tolerance.
boolean Vector:IsZero()
Checks whenever all fields of the vector are 0.
number Vector:Length()
Returns the Euclidean length of the vector: √x² + y² + z² 
number Vector:Length2D()
Returns the length of the vector in two dimensions, without the Z axis.
number Vector:Length2DSqr()
Returns the squared length of the vectors x and y value, x² + y². This is faster than Vector:Length2D as calculating the square root is an expensive process.
number Vector:LengthSqr()
Returns the squared length of the vector, x² + y² + z². This is faster than Vector:Length as calculating the square root is an expensive process.
Vector:Mul( number multiplier )
Scales the vector by the given number, that means x, y and z are multiplied by that value.
Vector:Normalize()
Normalizes the given vector. This changes the vector you call it on, if you want to return a normalized copy without affecting the original, use Vector:GetNormalized.
Vector:Rotate( Angle rotation )
Rotates a vector by the given angle. Doesn't return anything, but rather changes the original vector.
Vector:Set( Vector vector )
Copies the values from the second vector to the first vector.
Vector:SetUnpacked( number x, number y, number z )
Sets the x, y, and z of the vector.
Vector:Sub( Vector vector )
Substracts the values of the second vector from the orignal vector, this function can be used to avoid garbage collection.
table Vector:ToColor()
Translates the Vector (values ranging from 0 to 1) into a Color. This will also range the values from 0 - 1 to 0 - 255. x * 255 -> r y * 255 -> g z * 255 -> b This is the opposite of Color:ToVector
table Vector:ToScreen()
Returns where on the screen the specified position vector would appear. A related function is gui.ScreenToVector, which converts a 2D coordinate to a 3D direction. Should be called from a 3D rendering environment or after cam.Start3D or it may not work correctly.Errors in a render hook can make this value incorrect until the player restarts their game.Issue Tracker: 462cam.Start3D or 3D context cam.Start with non-default parameters incorrectly sets the reference FOV for this function, causing incorrect re..
table Vector:ToTable()
Returns the vector as a table with three elements.
number, number, number Vector:Unpack()
Returns the x, y, and z of the vector.
boolean Vector:WithinAABox( Vector boxStart, Vector boxEnd )
Returns whenever the given vector is in a box created by the 2 other vectors.
Vector:Zero()
Sets x, y and z to 0.

Example

Indexing by named component.

local v = Vector( 1, 2, 3 ) print( v.x, v.y, v.z )
Output: 1 2 3

Example

Indexing by number (most efficient!).

local v = Vector( 1, 2, 3 ) print( v[1], v[2], v[3] )
Output: 1 2 3

Page Links


Special Pages


Wikis

?

Render Time: 87ms

DB GetPage 6
Generate Html 38
SaveChanges (1) 22
Render Body 0
Render Sidebar 18