dreamworkAPI
Ctrl K
GitHub
classDocs / Vector3

Vector3

classsharedmenusource
Object → Vector3

A 3D vector object in Source engine coordinates.

Arithmetic operators work component-wise:

  • vec3 + vec3, vec3 + number, number + vec3 ( same for - and * )
  • vec3 / vec3, vec3 / number
  • -vec3

Constructor

Vector3(x: number?, y: number?, z: number?): Vector3

Fields

NameTypeDescription
xnumberThe X coordinate ( forward ).
ynumberThe Y coordinate ( left ).
znumberThe Z coordinate ( up ).
originVector3The shared zero vector ( 0, 0, 0 ), must not be modified.

Operators

OperatorOperandResult
add Vector3 | number Vector3
sub Vector3 | number Vector3
mul Vector3 | number Vector3
div Vector3 | number Vector3
unmVector3

Static functions

copyCreates a new independent copy of the vector.
dotCalculates the dot product of two vectors.
fromScreenReturns the view direction from the screen position.
getLengthCalculates the length of the vector.
getLengthSqrCalculates the squared length of the vector.
getLerpedReturns a copy of the vector linearly interpolated to another vector or a number.
getLerpedToNumberReturns a copy of the vector linearly interpolated to another vector or a number.
getLerpedToVectorReturns a copy of the vector linearly interpolated to another vector or a number.
lerpLinear interpolation between the vector and another vector or a number.
lerpToNumberLinear interpolation between the vector and another vector or a number.
lerpToVectorLinear interpolation between the vector and another vector or a number.
normalizeScales the vector to the length of 1.
orderSwaps coordinates of the given vectors in place, so every coordinate of mins is less than or equal to the same coordina…
projectProjects the vector onto the direction of another vector.
reflectReflects the vector from the plane with the given normal ( v - 2 * dot( v, n ) * n ).
rotateRotates the vector by the given angle.
scaleMultiplies all coordinates of the vector by the number.
translateToLocalTranslates the world position and angle into the local coordinate system.
translateToWorldTranslates the local position and angle into the world coordinate system.

#Vector3.copy

sharedmenunodiscardsource
function Vector3.copy(self: Vector3): Vector3

Creates a new independent copy of the vector.

Parameters
NameTypeDescription
selfVector3The vector.
Returns
TypeNameDescription
Vector3copyThe new vector.

#Vector3.dot

sharedmenunodiscardsource
function Vector3.dot(self: Vector3, vector: Vector3): number

Calculates the dot product of two vectors.

Parameters
NameTypeDescription
selfVector3The vector.
vectorVector3The other vector.
Returns
TypeNameDescription
numberdot_productThe dot product of two vectors.

#Vector3.fromScreen

sharedmenunodiscardsource
function Vector3.fromScreen(view_angle: Angle3, view_fov: number, x: number, y: number, screen_width: number, screen_height: number): Vector3

Returns the view direction from the screen position.

Same as util.AimVector and gui.ScreenToVector in Garry's Mod.

Parameters
NameTypeDescription
view_angleAngle3The view angle.
view_fovnumberThe view fov in degrees ( 4:3 based, like fov_desired ).
xnumberThe x position on the screen, 0 is the left edge.
ynumberThe y position on the screen, 0 is the top edge.
screen_widthnumberThe screen width in pixels.
screen_heightnumberThe screen height in pixels.
Returns
TypeNameDescription
Vector3directionThe new normalized direction vector.

#Vector3.getLength

sharedmenunodiscardsource
function Vector3.getLength(self: Vector3): number

Calculates the length of the vector.

Parameters
NameTypeDescription
selfVector3The vector.
Returns
TypeNameDescription
numberlengthThe length of the vector.

#Vector3.getLengthSqr

sharedmenunodiscardsource
function Vector3.getLengthSqr(self: Vector3): number

Calculates the squared length of the vector.

Faster than getLength, use it to compare lengths.

Parameters
NameTypeDescription
selfVector3The vector.
Returns
TypeNameDescription
numberlengthThe squared length of the vector.

#Vector3.getLerped

sharedmenunodiscardsource
function Vector3.getLerped(self: Vector3, target: Vector3 | number, frac: number): Vector3

Returns a copy of the vector linearly interpolated to another vector or a number.

Parameters
NameTypeDescription
selfVector3The vector.
targetVector3 | numberThe target vector or a number ( used for every coordinate ).
fracnumberThe interpolation factor.
Returns
TypeNameDescription
Vector3lerped_vec3The new vector.

#Vector3.getLerpedToNumber

sharedmenunodiscardsource
function Vector3.getLerpedToNumber(self: Vector3, target: Vector3 | number, frac: number): Vector3

Returns a copy of the vector linearly interpolated to another vector or a number.

Parameters
NameTypeDescription
selfVector3The vector.
targetVector3 | numberThe target vector or a number ( used for every coordinate ).
fracnumberThe interpolation factor.
Returns
TypeNameDescription
Vector3lerped_vec3The new vector.

#Vector3.getLerpedToVector

sharedmenunodiscardsource
function Vector3.getLerpedToVector(self: Vector3, target: Vector3 | number, frac: number): Vector3

Returns a copy of the vector linearly interpolated to another vector or a number.

Parameters
NameTypeDescription
selfVector3The vector.
targetVector3 | numberThe target vector or a number ( used for every coordinate ).
fracnumberThe interpolation factor.
Returns
TypeNameDescription
Vector3lerped_vec3The new vector.

#Vector3.lerp

sharedmenusource
function Vector3.lerp(self: Vector3, target: Vector3 | number, frac: number): Vector3

Linear interpolation between the vector and another vector or a number.

frac of 0 keeps the vector, 1 sets it to the target, values outside [0, 1] extrapolate.

Parameters
NameTypeDescription
selfVector3The vector.
targetVector3 | numberThe target vector or a number ( used for every coordinate ).
fracnumberThe interpolation factor.
Returns
TypeNameDescription
Vector3vec3The same vector, interpolated.

#Vector3.lerpToNumber

sharedmenusource
function Vector3.lerpToNumber(self: Vector3, target: Vector3 | number, frac: number): Vector3

Linear interpolation between the vector and another vector or a number.

frac of 0 keeps the vector, 1 sets it to the target, values outside [0, 1] extrapolate.

Parameters
NameTypeDescription
selfVector3The vector.
targetVector3 | numberThe target vector or a number ( used for every coordinate ).
fracnumberThe interpolation factor.
Returns
TypeNameDescription
Vector3vec3The same vector, interpolated.

#Vector3.lerpToVector

sharedmenusource
function Vector3.lerpToVector(self: Vector3, target: Vector3 | number, frac: number): Vector3

Linear interpolation between the vector and another vector or a number.

frac of 0 keeps the vector, 1 sets it to the target, values outside [0, 1] extrapolate.

Parameters
NameTypeDescription
selfVector3The vector.
targetVector3 | numberThe target vector or a number ( used for every coordinate ).
fracnumberThe interpolation factor.
Returns
TypeNameDescription
Vector3vec3The same vector, interpolated.

#Vector3.normalize

sharedmenusource
function Vector3.normalize(self: Vector3): Vector3

Scales the vector to the length of 1.

Zero vector stays unchanged.

Parameters
NameTypeDescription
selfVector3The vector.
Returns
TypeNameDescription
Vector3vec3The same vector, normalized.

#Vector3.order

sharedmenusource
function Vector3.order(mins: Vector3, maxs: Vector3): Vector3, Vector3

Swaps coordinates of the given vectors in place, so every coordinate of mins is less than or equal to the same coordinate of maxs.

Same as OrderVectors in Garry's Mod, useful for building bounding boxes from two arbitrary corners.

Parameters
NameTypeDescription
minsVector3The first corner, becomes the minimum corner.
maxsVector3The second corner, becomes the maximum corner.
Returns
TypeNameDescription
Vector3minsThe same mins vector.
Vector3maxsThe same maxs vector.

#Vector3.project

sharedmenusource
function Vector3.project(self: Vector3, vector: Vector3): Vector3

Projects the vector onto the direction of another vector.

vector does not have to be normalized, projecting onto a zero vector results in a zero vector.

Parameters
NameTypeDescription
selfVector3The vector.
vectorVector3The direction to project onto.
Returns
TypeNameDescription
Vector3vec3The same vector, projected.

#Vector3.reflect

sharedmenusource
function Vector3.reflect(self: Vector3, normal: Vector3): Vector3

Reflects the vector from the plane with the given normal ( v - 2 * dot( v, n ) * n ).

Useful for bouncing velocities from surfaces, e.g. with a trace hit normal.

Parameters
NameTypeDescription
selfVector3The vector.
normalVector3The plane normal, must be normalized.
Returns
TypeNameDescription
Vector3vec3The same vector, reflected.

#Vector3.rotate

sharedmenusource
function Vector3.rotate(self: Vector3, angle: Angle3): Vector3

Rotates the vector by the given angle.

Same as VectorRotate in Source engine and Vector:Rotate in Garry's Mod.

Parameters
NameTypeDescription
selfVector3The vector to rotate.
angleAngle3The angle to rotate by.
Returns
TypeNameDescription
Vector3vec3The same vector, rotated.

#Vector3.scale

sharedmenusource
function Vector3.scale(self: Vector3, scale: number): Vector3

Multiplies all coordinates of the vector by the number.

Parameters
NameTypeDescription
selfVector3The vector.
scalenumberThe scale factor.
Returns
TypeNameDescription
Vector3vec3The same vector, scaled.

#Vector3.translateToLocal

sharedmenunodiscardsource
function Vector3.translateToLocal(position: Vector3, angle: Angle3, world_position: Vector3, world_angle: Angle3): Vector3, Angle3

Translates the world position and angle into the local coordinate system.

Same as WorldToLocal in Garry's Mod.

Parameters
NameTypeDescription
positionVector3The world position.
angleAngle3The world angle.
world_positionVector3The origin of the local coordinate system.
world_angleAngle3The angle of the local coordinate system.
Returns
TypeNameDescription
Vector3vec3The new local position vector.
Angle3ang3The new local angle.

#Vector3.translateToWorld

sharedmenunodiscardsource
function Vector3.translateToWorld(local_position: Vector3, local_angle: Angle3, world_position: Vector3, world_angle: Angle3): Vector3, Angle3

Translates the local position and angle into the world coordinate system.

Same as LocalToWorld in Garry's Mod.

Parameters
NameTypeDescription
local_positionVector3The local position.
local_angleAngle3The local angle.
world_positionVector3The origin of the local coordinate system.
world_angleAngle3The angle of the local coordinate system.
Returns
TypeNameDescription
Vector3vec3The new world position vector.
Angle3ang3The new world angle.

Methods

addAdds another vector or a number ( to every coordinate ) to the vector.
crossCalculates the cross product of two vectors ( self x vector ).
divDivides the vector by another vector ( component-wise ) or a number.
getAngleCalculates the angle between two vectors in degrees.
getDistanceCalculates the distance between two vectors.
getDistance2DCalculates the distance between two vectors in the XY plane ( ignoring Z ).
getDistance2DSqrCalculates the squared distance between two vectors in the XY plane ( ignoring Z ).
getDistanceSqrCalculates the squared distance between two vectors.
getLength2DCalculates the length of the vector in the XY plane ( ignoring Z ).
getLength2DSqrCalculates the squared length of the vector in the XY plane ( ignoring Z ).
getNormalizedReturns a copy of the vector with the length of 1.
getProjectedReturns a copy of the vector projected onto another vector.
getReflectedReturns a copy of the vector reflected from the plane with the given normal.
getRotatedReturns a copy of the vector rotated by the given angle.
getScaledReturns a scaled copy of the vector.
isNearChecks if the vector is equal to the given vector with the given tolerance.
isZeroChecks if all coordinates of the vector are exactly zero.
mulMultiplies the vector by another vector ( component-wise ) or a number.
negateNegates all coordinates of the vector.
setCopies the coordinates of another vector into this vector.
setUnpackedSets the x, y, and z coordinates of the vector.
subSubtracts another vector or a number ( from every coordinate ) from the vector.
toAngle3Returns the angle that points in the direction of the vector.
toScreenConverts the world position to the screen position.
unpackReturns the x, y, and z coordinates of the vector.
withinAABoxChecks if the vector is within an axis-aligned box, borders included.
zeroSets all coordinates of the vector to zero.

#Vector3:add

sharedmenusource
function Vector3:add(value: Vector3 | number): Vector3

Adds another vector or a number ( to every coordinate ) to the vector.

Parameters
NameTypeDescription
valueVector3 | numberThe other vector or a number.
Returns
TypeNameDescription
Vector3vec3The same vector, with the sum.

#Vector3:cross

sharedmenunodiscardsource
function Vector3:cross(vector: Vector3): Vector3

Calculates the cross product of two vectors ( self x vector ).

The result is perpendicular to both vectors, e.g. forward x left = up.

Parameters
NameTypeDescription
vectorVector3The other vector.
Returns
TypeNameDescription
Vector3cross_productThe new vector.

#Vector3:div

sharedmenusource
function Vector3:div(value: Vector3 | number): Vector3

Divides the vector by another vector ( component-wise ) or a number.

Modifies the vector in place, division by zero is not checked and results in inf / nan components.

Parameters
NameTypeDescription
valueVector3 | numberThe divisor vector or number.
Returns
TypeNameDescription
Vector3vec3The same vector, divided.

#Vector3:getAngle

sharedmenunodiscardsource
function Vector3:getAngle(vector: Vector3): number

Calculates the angle between two vectors in degrees.

Vectors do not have to be normalized, the result is in range [0, 180].

Not to be confused with toAngle3, which returns the direction of the vector as an angle.

Parameters
NameTypeDescription
vectorVector3The other vector.
Returns
TypeNameDescription
numberdegreesThe angle between two vectors.

#Vector3:getDistance

sharedmenunodiscardsource
function Vector3:getDistance(vector: Vector3): number

Calculates the distance between two vectors.

Parameters
NameTypeDescription
vectorVector3The other vector.
Returns
TypeNameDescription
numberdistanceThe distance between the two vectors.

#Vector3:getDistance2D

sharedmenunodiscardsource
function Vector3:getDistance2D(vector: Vector3): number

Calculates the distance between two vectors in the XY plane ( ignoring Z ).

Parameters
NameTypeDescription
vectorVector3The other vector.
Returns
TypeNameDescription
numberdistanceThe 2D distance between the two vectors.

#Vector3:getDistance2DSqr

sharedmenunodiscardsource
function Vector3:getDistance2DSqr(vector: Vector3): number

Calculates the squared distance between two vectors in the XY plane ( ignoring Z ).

Parameters
NameTypeDescription
vectorVector3The other vector.
Returns
TypeNameDescription
numberdistanceThe squared 2D distance between the two vectors.

#Vector3:getDistanceSqr

sharedmenunodiscardsource
function Vector3:getDistanceSqr(vector: Vector3): number

Calculates the squared distance between two vectors.

Faster than getDistance, use it to compare distances.

Parameters
NameTypeDescription
vectorVector3The other vector.
Returns
TypeNameDescription
numberdistanceThe squared distance between the two vectors.

#Vector3:getLength2D

sharedmenunodiscardsource
function Vector3:getLength2D(): number

Calculates the length of the vector in the XY plane ( ignoring Z ).

Returns
TypeNameDescription
numberlengthThe 2D length of the vector.

#Vector3:getLength2DSqr

sharedmenunodiscardsource
function Vector3:getLength2DSqr(): number

Calculates the squared length of the vector in the XY plane ( ignoring Z ).

Returns
TypeNameDescription
numberlengthThe squared 2D length of the vector.

#Vector3:getNormalized

sharedmenunodiscardsource
function Vector3:getNormalized(): Vector3

Returns a copy of the vector with the length of 1.

Zero vector returns a zero copy.

Returns
TypeNameDescription
Vector3normalized_vec3The new vector.

#Vector3:getProjected

sharedmenunodiscardsource
function Vector3:getProjected(vector: Vector3): Vector3

Returns a copy of the vector projected onto another vector.

Parameters
NameTypeDescription
vectorVector3The direction to project onto.
Returns
TypeNameDescription
Vector3projected_vec3The new vector.

#Vector3:getReflected

sharedmenunodiscardsource
function Vector3:getReflected(normal: Vector3): Vector3

Returns a copy of the vector reflected from the plane with the given normal.

Parameters
NameTypeDescription
normalVector3The plane normal, must be normalized.
Returns
TypeNameDescription
Vector3reflected_vec3The new vector.

#Vector3:getRotated

sharedmenunodiscardsource
function Vector3:getRotated(angle: Angle3): Vector3

Returns a copy of the vector rotated by the given angle.

Parameters
NameTypeDescription
angleAngle3The angle to rotate by.
Returns
TypeNameDescription
Vector3rotated_vec3The new vector.

#Vector3:getScaled

sharedmenunodiscardsource
function Vector3:getScaled(scale: number): Vector3

Returns a scaled copy of the vector.

Parameters
NameTypeDescription
scalenumberThe scale factor.
Returns
TypeNameDescription
Vector3scaled_vec3The scaled copy of the vector.

#Vector3:isNear

sharedmenunodiscardsource
function Vector3:isNear(vector: Vector3, tolerance: number): boolean

Checks if the vector is equal to the given vector with the given tolerance.

Each coordinate is compared separately: |a.x - b.x| <= tolerance and so on.

Parameters
NameTypeDescription
vectorVector3The vector to check.
tolerancenumberThe maximum allowed difference per coordinate.
Returns
TypeNameDescription
booleanis_neartrue if the vectors are equal within the tolerance, otherwise false.

#Vector3:isZero

sharedmenunodiscardsource
function Vector3:isZero(): boolean

Checks if all coordinates of the vector are exactly zero.

Returns
TypeNameDescription
booleanis_zerotrue if the vector is zero, false otherwise.

#Vector3:mul

sharedmenusource
function Vector3:mul(value: Vector3 | number): Vector3

Multiplies the vector by another vector ( component-wise ) or a number.

Parameters
NameTypeDescription
valueVector3 | numberThe other vector or a number.
Returns
TypeNameDescription
Vector3vec3The same vector, with the product.

#Vector3:negate

sharedmenusource
function Vector3:negate(): Vector3

Negates all coordinates of the vector.

Returns
TypeNameDescription
Vector3vec3The same vector, negated.

#Vector3:set

sharedmenusource
function Vector3:set(vector: Vector3): Vector3

Copies the coordinates of another vector into this vector.

Parameters
NameTypeDescription
vectorVector3The vector to copy from.
Returns
TypeNameDescription
Vector3vec3The same vector.

#Vector3:setUnpacked

sharedmenusource
function Vector3:setUnpacked(x: number, y: number, z: number): Vector3

Sets the x, y, and z coordinates of the vector.

Parameters
NameTypeDescription
xnumberThe x coordinate of the vector.
ynumberThe y coordinate of the vector.
znumberThe z coordinate of the vector.
Returns
TypeNameDescription
Vector3vec3The same vector.

#Vector3:sub

sharedmenusource
function Vector3:sub(value: Vector3 | number): Vector3

Subtracts another vector or a number ( from every coordinate ) from the vector.

Parameters
NameTypeDescription
valueVector3 | numberThe other vector or a number.
Returns
TypeNameDescription
Vector3vec3The same vector, with the difference.

#Vector3:toAngle3

sharedmenunodiscardsource
function Vector3:toAngle3(up?: Vector3): Angle3

Returns the angle that points in the direction of the vector.

Same as VectorAngles in Source engine and Vector:Angle / Vector:AngleEx in Garry's Mod:

  • without up, pitch and yaw are in range [0, 360) and roll is always 0, e.g. ( 0, 0, 1 ) returns ( 270, 0, 0 ).
  • with up, roll is calculated from the given up direction and angles are in range [-180, 180].

The vector does not have to be normalized.

Parameters
NameTypeDescription
upoptionalVector3The up direction, used to calculate roll.
Returns
TypeNameDescription
Angle3ang3The new angle.

#Vector3:toScreen

sharedmenunodiscardsource
function Vector3:toScreen(view_origin: Vector3, view_angle: Angle3, view_fov: number, screen_width: number, screen_height: number): number, number, boolean

Converts the world position to the screen position.

Like Vector:ToScreen in Garry's Mod, but the view is passed explicitly.

Positions behind the view are still projected ( mirrored ) with visible set to false, a position exactly in the view plane returns the screen center.

Parameters
NameTypeDescription
view_originVector3The view origin.
view_angleAngle3The view angle.
view_fovnumberThe view fov in degrees ( 4:3 based, like fov_desired ).
screen_widthnumberThe screen width in pixels.
screen_heightnumberThe screen height in pixels.
Returns
TypeNameDescription
numberxThe x position on the screen, 0 is the left edge.
numberyThe y position on the screen, 0 is the top edge.
booleanvisibletrue if the position is in front of the view, false otherwise.

#Vector3:unpack

sharedmenunodiscardsource
function Vector3:unpack(): number, number, number

Returns the x, y, and z coordinates of the vector.

Returns
TypeNameDescription
numberxThe x coordinate of the vector.
numberyThe y coordinate of the vector.
numberzThe z coordinate of the vector.

#Vector3:withinAABox

sharedmenunodiscardsource
function Vector3:withinAABox(box_start: Vector3, box_end: Vector3): boolean

Checks if the vector is within an axis-aligned box, borders included.

The corners of the box do not have to be ordered.

Same as Vector:WithinAABox in Garry's Mod.

Parameters
NameTypeDescription
box_startVector3The first corner of the box.
box_endVector3The second corner of the box.
Returns
TypeNameDescription
booleanin_boxtrue if the vector is within the box, false otherwise.

#Vector3:zero

sharedmenusource
function Vector3:zero(): Vector3

Sets all coordinates of the vector to zero.

Returns
TypeNameDescription
Vector3vec3The same vector.
Generated from the dreamwork meta branch.