Vector3
Sandbox.System
Derives from ValueType
Implements IEquatable<Vector3>

Summary

A point in 3D space.

Constructors

Vector3( float x, float y, float z, )

Initializes a vector with given components.

Vector3( float x, float y, )

Initializes a vector with given components and Z set to 0.

Vector3( Vector3 other, )

Initializes a vector3 from a given vector3, i.e. a copy.

Vector3( Vector2 other, float z, )

Initializes a vector3 from given vector2 and the given Z component.

Vector3( float all = 0, )

Initializes the vector with all components set to given value.

Fields

static readonly Vector3 Backward

A vector with X set to -1. This represents the backwards direction.

static readonly Vector3 Down

A vector with Z set to -1. This represents the downwards direction.

static readonly Vector3 Forward

A vector with X set to 1. This represents the forwards direction.

static readonly Vector3 Left

A vector with Y set to 1. This represents the left hand direction.

static readonly Vector3 One

A vector with all components set to 1.

static readonly Vector3 Up

A vector with Z set to 1. This represents the upwards direction.

static readonly Vector3 Zero

A vector with all components set to 0.

Properties

static Vector3 Random { get; }

Returns a new vector with each component randomized between -1 and 1.

Angles EulerAngles { get; set; }

The Euler angles of this direction vector.

bool IsNaN { get; }

Returns true if x, y or z are NaN

bool IsNearZeroLength { get; }

Whether length of this vector is nearly zero.

float Length { get; }

Length (or magnitude) of the vector (Distance from 0,0,0).

float LengthSquared { get; }

Squared length of the vector. This is faster than Length, and can be used for things like comparing distances, as long as only squared values are used.

Vector3 Normal { get; }

Returns a unit version of this vector. A unit vector has length of 1.

float x { get; set; }

The X component of this Vector.

float y { get; set; }

The Y component of this Vector.

float z { get; set; }

The Z component of this Vector.

Methods

static Vector3 Cross( Vector3 a, Vector3 b, )

Returns the cross product of the 2 given vectors. If the given vectors are linearly independent, the resulting vector is perpendicular to them both, also known as a normal of a plane.

static Vector3 CubicBeizer( Vector3 source, Vector3 target, Vector3 sourceTangent, Vector3 targetTangent, float t, )

Calculates position of a point on a cubic beizer curve at given fraction.

static float DistanceBetween( Vector3 a, Vector3 b, )

Returns distance between the 2 given vectors.

static float DistanceBetweenSquared( Vector3 a, Vector3 b, )

Returns squared distance between the 2 given vectors. This is faster than DistanceBetween, and can be used for things like comparing distances, as long as only squared values are used.

static float Dot( Vector3 a, Vector3 b, )

Returns the scalar/dot product of the 2 given vectors.

static float GetAngle( Vector3 v1, Vector3 v2, )

Return the distance between the two direction vectors in degrees.

static Vector3 Lerp( Vector3 a, Vector3 b, float frac, bool clamp = true, )

Performs linear interpolation between 2 given vectors.

static Vector3 Lerp( Vector3 a, Vector3 b, Vector3 frac, bool clamp = true, )

Performs linear interpolation between 2 given vectors, with separate fraction for each vector component.

static Vector3 Max( Vector3 a, Vector3 b, )

Returns a vector that has the maximum values on each axis between the 2 given vectors.

static Vector3 Min( Vector3 a, Vector3 b, )

Returns a vector that has the minimum values on each axis between the 2 given vectors.

static Vector3 Parse( string str, )

Given a string, try to convert this into a vector. Example input formats that work would be "1,1,1", "1;1;1", "[1 1 1]".

This handles a bunch of different separators ( ' ', ',', ';', '\n', '\r' ).

It also trims surrounding characters ('[', ']', ' ', '\n', '\r', '\t', '"').

static Vector3 Read( BinaryReader reader, )

Read a vector3 from given binary reader.

static Vector3 Reflect( Vector3 direction, Vector3 normal, )

Returns a reflected vector based on incoming direction and plane normal. Like a ray reflecting off of a mirror.

static Vector3 SmoothDamp( Vector3 current, Vector3 target, ref Vector3 velocity, float smoothTime, float deltaTime, )

Smoothly move towards the target vector

static void Sort( ref Vector3 min, ref Vector3 max, )

Sort these two vectors into min and max. This doesn't just swap the vectors, it sorts each component. So that min will come out containing the minimum x, y and z values.

static Angles VectorAngle( Vector3 vec, )

Converts a direction vector to an angle.

static Vector3 VectorPlaneProject( Vector3 v, Vector3 planeNormal, )

Projects given vector on a plane defined by ``.

Returns a new vector with all values positive. -5 becomes 5, etc.

Vector3 AddClamped( Vector3 toAdd, float maxLength, )

Try to add to this vector. If we're already over max then don't add. If we're over max when we add, clamp in that direction so we're not.

bool AlmostEqual( Vector3 v, float delta = 0.0001, )

Returns true if we're nearly equal to the passed vector.

float Angle( Vector3 v2, )

Return the distance between the two direction vectors in degrees.

Vector3 Approach( float length, float amount, )

Returns a new vector whose length is closer to given target length by given amount.

Vector3 Clamp( Vector3 otherMin, Vector3 otherMax, )

Returns a vector each axis of which is clamped to between the 2 given vectors. Basically clamps a point to an Axis Aligned Bounding Box (AABB).

Vector3 Clamp( float min, float max, )

Returns a vector each axis of which is clamped to given min and max values.

Vector3 ClampLength( float maxLength, )

Returns a vector whose length is limited to given maximum.

Vector3 ClampLength( float minLength, float maxLength, )

Returns a vector whose length is limited between given minimum and maximum.

Returns a vector that has the maximum values on each axis between this vector and given vector.

Returns a vector that has the minimum values on each axis between this vector and given vector.

Returns the cross product of this and the given vector. If this and the given vectors are linearly independent, the resulting vector is perpendicular to them both, also known as a normal of a plane.

float Distance( Vector3 target, )

Returns distance between this vector to given vector.

float DistanceSquared( Vector3 target, )

Returns squared distance between this vector to given vector. This is faster than Distance, and can be used for things like comparing distances, as long as only squared values are used.

float Dot( Vector3 b, )

Returns the scalar/dot product of this and the given vectors.

bool IsNearlyZero( float tolerance = 1E-45, )

Returns true if value on every axis is less than tolerance away from zero

Vector3 LerpTo( Vector3 target, float frac, bool clamp = true, )

Performs linear interpolation between this and given vectors.

Vector3 LerpTo( Vector3 target, Vector3 frac, bool clamp = true, )

Performs linear interpolation between this and given vectors, with separate fraction for each vector component.

Vector3 LerpTo( Vector3 target, float frac, )
Vector3 LerpTo( Vector3 target, Vector3 frac, )

Projects this vector onto another vector.

Basically extends the given normal/unit vector to be as long as necessary to make a right triangle (a triangle which has a 90 degree corner) between (0,0,0), this vector and the projected vector.

Vector3 SnapToGrid( float gridSize, bool sx = true, bool sy = true, bool sz = true, )

Snap to grid along any of the 3 axes.

Vector3 SubtractDirection( Vector3 direction, float strength = 1, )

Given a vector like 1,1,1 and direction 1,0,0, will return 0,1,1. This is useful for velocity collision type events, where you want to cancel out velocity based on a normal. For this to work properly, direction should be a normal, but you can scale how much you want to subtract by scaling the direction. Ie, passing in a direction with a length of 0.5 will remove half the direction.

Vector3 WithX( float x, )

Returns this vector with given X component.

Vector3 WithY( float y, )

Returns this vector with given Y component.

Vector3 WithZ( float z, )

Returns this vector with given Z component.

void Write( BinaryWriter writer, )

Write this vector3 to a System.IO.BinaryWriter.

override bool Equals( object obj, )
Overrides ValueType.Equals
override bool Equals( Vector3 o, )
Overrides ValueType.Equals
override int GetHashCode( )
Overrides ValueType.GetHashCode
override string ToString( )
Overrides ValueType.ToString

Formats the Vector into a string "x,y,z"

Extensions

Clientside, returns position of this 3D vector on local clients' screen in 2D coordinates based on the Sandbox.CurrentView.

Vector2? ToScreen( Vector2 screenSize, )

Same as Vector3Extensions.ToScreen, but return value is in absolute coordinates.

Operators

float this[ int index,  ]
Vector3 +( Vector3 c1, Vector3 c2,  )
Vector3 /( Vector3 c1, float f,  )
bool ==( Vector3 left, Vector3 right,  )
bool !=( Vector3 left, Vector3 right,  )
Vector3 *( Vector3 c1, float f,  )
Vector3 *( Vector3 c1, Rotation f,  )
Vector3 *( Vector3 c1, Vector3 c2,  )
Vector3 *( float f, Vector3 c1,  )
Vector3 -( Vector3 c1, Vector3 c2,  )
Vector3 -( Vector3 value,  )
implicit Vector3 =( Color value,  )
implicit Vector3 =( float value,  )
implicit Vector3 =( Vector2 value,  )
implicit Vector3 =( Vector4 vec,  )
implicit Vector3 =( Vector3 value,  )
implicit Vector3 =( Vector3 value,  )