Vector3class

A 3-dimentional vector. Typically represents a position, size, or direction in 3D space.

objectVector3
Namespace
global
Assembly
Sandbox.System
Declaration
public sealed struct Vector3

Constructors6

Showing 6 constructors

Methods56

Showing 56 methods

public Vector3 AddClamped(Vector3 toAdd, float maxLength)PUBLIC

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.

ParameterTypeDescription
toAddVector3
maxLengthfloat
Returns:Vector3

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

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

ParameterTypeDescription
vVector3The value to compare with
delta = 0.0001floatThe max difference between component values
Returns:boolTrue if nearly equal

public float Angle(Vector3 other)PUBLIC

Return the distance between the two direction vectors in degrees.

ParameterTypeDescription
otherVector3
Returns:float

public Vector3 Approach(float length, float amount)PUBLIC

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

ParameterTypeDescription
lengthfloatTarget length.
amountfloatHow much to subtract or add.
Returns:Vector3

public Vector3 ComponentMax(Vector3 other)PUBLIC

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

ParameterTypeDescription
otherVector3
Returns:Vector3

public Vector3 ComponentMin(Vector3 other)PUBLIC

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

ParameterTypeDescription
otherVector3
Returns:Vector3

public static Vector3 CubicBezier(Vector3 source, Vector3 target, Vector3 sourceTangent, Vector3 targetTangent, float t)PUBLICSTATIC

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

ParameterTypeDescription
sourceVector3Point A of the curve in world space.
targetVector3Point B of the curve in world space.
sourceTangentVector3Tangent for the Point A in world space.
targetTangentVector3Tangent for the Point B in world space.
tfloatHow far along the path to get a point on. Range is 0 to 1, inclusive.
Returns:Vector3The point on the curve

public static Vector3 Direction(Vector3 from, Vector3 to)PUBLICSTATIC

Calculates the normalized direction vector from one point to another in 3D space.

ParameterTypeDescription
fromVector3
toVector3
Returns:Vector3

public float Distance(Vector3 target)PUBLIC

Returns distance between this vector to given vector.

ParameterTypeDescription
targetVector3
Returns:float

public static float DistanceBetween(Vector3 a, Vector3 b)PUBLICSTATIC

Returns distance between the 2 given vectors.

ParameterTypeDescription
aVector3
bVector3
Returns:float

public static float DistanceBetweenSquared(Vector3 a, Vector3 b)PUBLICSTATIC

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.

ParameterTypeDescription
aVector3
bVector3
Returns:float

public float DistanceSquared(Vector3 target)PUBLIC

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.

ParameterTypeDescription
targetVector3
Returns:float

public static float GetAngle(Vector3 v1, Vector3 v2)PUBLICSTATIC

Return the distance between the two direction vectors in degrees.

ParameterTypeDescription
v1Vector3
v2Vector3
Returns:float

public static float InverseLerp(Vector3 pos, Vector3 a, Vector3 b, bool clamp = True)PUBLICSTATIC

Given a position, and two other positions, calculate the inverse lerp position between those

ParameterTypeDescription
posVector3
aVector3
bVector3
clamp = Truebool
Returns:float

public bool IsNearlyZero(float tolerance = 0.0001)PUBLIC

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

ParameterTypeDescription
tolerance = 0.0001float
Returns:bool

public Vector3 ProjectOnNormal(Vector3 normal)PUBLIC

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.

ParameterTypeDescription
normalVector3
Returns:Vector3The projected vector.

public static Vector3 Reflect(Vector3 direction, Vector3 normal)PUBLICSTATIC

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

ParameterTypeDescription
directionVector3
normalVector3
Returns:Vector3

public Vector3 RotateAround(Vector3 center, Rotation rot)PUBLIC

Rotate this vector around given point by given rotation and return the result as a new vector. See `Transform.RotateAround(Vector3@,Rotation@)` for similar method that also transforms rotation.

ParameterTypeDescription
centerVector3Point to rotate around.
rotRotationHow much to rotate by. `Rotation.FromAxis(Vector3,System.Single)` can be useful.
Returns:Vector3The rotated vector.

public static Vector3 Slerp(Vector3 a, Vector3 b, float frac, bool clamp = True)PUBLICSTATIC

Performs spherical linear interpolation (Slerp) between two vectors.

ParameterTypeDescription
aVector3Starting vector (A).
bVector3Target vector (B).
fracfloatInterpolation fraction: 0 returns A, 1 returns B, and values in between provide intermediate results along the spherical path.
clamp = TrueboolIf true, clamps the fraction between 0 and 1.
Returns:Vector3Interpolated vector along the spherical path.

public Vector3 SlerpTo(Vector3 target, float frac, bool clamp = True)PUBLIC

Performs spherical linear interpolation (Slerp) between this vector and a target vector.

ParameterTypeDescription
targetVector3The target vector to interpolate towards.
fracfloatInterpolation fraction: 0 returns this vector, 1 returns the target vector, and values in between provide intermediate results along the spherical path.
clamp = TrueboolIf true, clamps the fraction between 0 and 1.
Returns:Vector3Interpolated vector along the spherical path.

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

Smoothly move towards the target vector

ParameterTypeDescription
currentVector3
targetVector3
velocityVector3
smoothTimefloat
deltaTimefloat
Returns:Vector3

public Vector3 SnapToGrid(float gridSize, bool sx = True, bool sy = True, bool sz = True)PUBLIC

Snap to grid along any of the 3 axes.

ParameterTypeDescription
gridSizefloat
sx = Truebool
sy = Truebool
sz = Truebool
Returns:Vector3

public static void Sort(Vector3 min, Vector3 max)PUBLICSTATIC

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.

ParameterTypeDescription
minVector3
maxVector3
Returns:void

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

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.

ParameterTypeDescription
directionVector3
strength = 1float
Returns:Vector3

public static Vector3 TcbSpline(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float tension, float continuity, float bias, float u)PUBLICSTATIC

Calculates an interpolated point using the Kochanek-Bartels spline (TCB spline).

ParameterTypeDescription
p0Vector3
p1Vector3
p2Vector3
p3Vector3
tensionfloatTension parameter which affects the sharpness at the control point. Positive values make the curve tighter, negative values make it rounder.
continuityfloatContinuity parameter which affects the continuity between segments. Positive values create smoother transitions, negative values can create corners.
biasfloatBias parameter which affects the direction of the curve as it passes through the control point. Positive values bias the curve towards the next point, negative values towards the previous.
ufloatThe interpolation parameter between 0 and 1, where 0 is the start of the segment and 1 is the end.
Returns:Vector3The interpolated point on the curve.

public static Angles VectorAngle(Vector3 vec)PUBLICSTATIC

Converts a direction vector to an angle.

ParameterTypeDescription
vecVector3
Returns:Angles

public static Vector3 VectorPlaneProject(Vector3 v, Vector3 planeNormal)PUBLICSTATIC

Projects given vector on a plane defined by `planeNormal`.

ParameterTypeDescription
vVector3The vector to project.
planeNormalVector3Normal of a plane to project onto.
Returns:Vector3The projected vector.

public Vector3 WithAcceleration(Vector3 target, float acceleration)PUBLIC

Move to the target vector, by amount acceleration

ParameterTypeDescription
targetVector3
accelerationfloat
Returns:Vector3

public Vector3 WithFriction(float frictionAmount, float stopSpeed = 140)PUBLIC

Apply an amount of friction to the current velocity.

ParameterTypeDescription
frictionAmountfloat
stopSpeed = 140float
Returns:Vector3

public Vector3 WithX(float x)PUBLIC

Returns this vector with given X component.

ParameterTypeDescription
xfloatThe override for X component.
Returns:Vector3The new vector.

public Vector3 WithY(float y)PUBLIC

Returns this vector with given Y component.

ParameterTypeDescription
yfloatThe override for Y component.
Returns:Vector3The new vector.

public Vector3 WithZ(float z)PUBLIC

Returns this vector with given Z component.

ParameterTypeDescription
zfloatThe override for Z component.
Returns:Vector3The new vector.

Properties13

Showing 13 properties

public Angles Vector3.EulerAngles { get; set; }PUBLICGETSET

The Euler angles of this direction vector.

Returns:Angles

public Vector3 Vector3.Inverse { get; set; }PUBLICGETSET

Returns the inverse of this vector, which is useful for scaling vectors.

Returns:Vector3

public bool Vector3.IsInfinity { get; set; }PUBLICGETSET

Returns true if x, y or z are infinity

Returns:bool

public bool Vector3.IsNaN { get; set; }PUBLICGETSET

Returns true if x, y or z are NaN

Returns:bool

public bool Vector3.IsNearZeroLength { get; set; }PUBLICGETSET

Returns true if the squared length is less than 1e-8 (which is really near zero)

Returns:bool

public float Vector3.Item { get; set; }PUBLICGETSET

Returns:float

public float Vector3.Length { get; set; }PUBLICGETSET

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

Returns:float

public float Vector3.LengthSquared { get; set; }PUBLICGETSET

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.

Returns:float

public Vector3 Vector3.Normal { get; set; }PUBLICGETSET

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

Returns:Vector3

public static Vector3 Vector3.Random { get; set; }PUBLICSTATICGETSET

Uniformly samples a 3D position from all points with distance at most 1 from the origin.

Returns:Vector3

public float Vector3.x { get; set; }PUBLICGETSET

The X component of this vector.

Returns:float

public float Vector3.y { get; set; }PUBLICGETSET

The Y component of this vector.

Returns:float

public float Vector3.z { get; set; }PUBLICGETSET

The Z component of this vector.

Returns:float

On this page

Constructorspublic Vector3(System.Numerics.Vector3 v)public Vector3(System.Single x, System.Single y, System.Single z)public Vector3(System.Single x, System.Single y)public Vector3(System.Single all = 0)public Vector3(Vector2 other, System.Single z)public Vector3(Vector3 other)Methodspublic Vector3 Abs()public static Vector3 Abs(Vector3 value)public Vector3 AddClamped(Vector3 toAdd, System.Single maxLength)public System.Boolean AlmostEqual(Vector3 v, System.Single delta = 0.0001)public System.Single Angle(Vector3 other)public Vector3 Approach(System.Single length, System.Single amount)public static Vector3 CatmullRomSpline(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, System.Single t)public Vector3 Clamp(System.Single min, System.Single max)public Vector3 Clamp(Vector3 otherMin, Vector3 otherMax)public static Vector3 Clamp(Vector3 value, Vector3 min, Vector3 max)public Vector3 ClampLength(System.Single minLength, System.Single maxLength)public Vector3 ClampLength(System.Single maxLength)public Vector3 ComponentMax(Vector3 other)public Vector3 ComponentMin(Vector3 other)public static Vector3 Cross(Vector3 a, Vector3 b)public Vector3 Cross(Vector3 b)public static Vector3 CubicBezier(Vector3 source, Vector3 target, Vector3 sourceTangent, Vector3 targetTangent, System.Single t)public static Vector3 Direction(Vector3 from, Vector3 to)public System.Single Distance(Vector3 target)public static System.Single DistanceBetween(Vector3 a, Vector3 b)public static System.Single DistanceBetweenSquared(Vector3 a, Vector3 b)public System.Single DistanceSquared(Vector3 target)public static System.Single Dot(Vector3 a, Vector3 b)public System.Single Dot(Vector3 b)public static System.Single GetAngle(Vector3 v1, Vector3 v2)public static System.Single InverseLerp(Vector3 pos, Vector3 a, Vector3 b, System.Boolean clamp = True)public System.Boolean IsNearlyZero(System.Single tolerance = 0.0001)public static Vector3 Lerp(Vector3 a, Vector3 b, System.Single frac, System.Boolean clamp = True)public static Vector3 Lerp(Vector3 a, Vector3 b, Vector3 frac, System.Boolean clamp = True)public Vector3 LerpTo(Vector3 target, System.Single frac, System.Boolean clamp = True)public Vector3 LerpTo(Vector3 target, Vector3 frac, System.Boolean clamp = True)public static Vector3 Max(Vector3 a, Vector3 b)public static Vector3 Min(Vector3 a, Vector3 b)public static override Vector3 Parse(System.String str, System.IFormatProvider provider)public static Vector3 Parse(System.String str)public Vector3 ProjectOnNormal(Vector3 normal)public static Vector3 Reflect(Vector3 direction, Vector3 normal)public Vector3 RotateAround(Vector3 center, Rotation rot)public static Vector3 Slerp(Vector3 a, Vector3 b, System.Single frac, System.Boolean clamp = True)public Vector3 SlerpTo(Vector3 target, System.Single frac, System.Boolean clamp = True)public static Vector3 SmoothDamp(Vector3 current, Vector3 target, Vector3 velocity, System.Single smoothTime, System.Single deltaTime)public Vector3 SnapToGrid(System.Single gridSize, System.Boolean sx = True, System.Boolean sy = True, System.Boolean sz = True)public static System.Void Sort(Vector3 min, Vector3 max)public static Vector3 SpringDamp(Vector3 current, Vector3 target, Vector3 velocity, System.Single smoothTime, System.Single deltaTime, System.Single frequency = 2, System.Single damping = 0.5)public static Vector3 SpringDamp(Vector3 current, Vector3 target, Vector3 velocity, System.Single deltaTime, System.Single frequency = 2, System.Single damping = 0.5)public Vector3 SubtractDirection(Vector3 direction, System.Single strength = 1)public static Vector3 TcbSpline(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, System.Single tension, System.Single continuity, System.Single bias, System.Single u)public static override System.Boolean TryParse(System.String str, System.IFormatProvider provider, Vector3 result)public static System.Boolean TryParse(System.String str, Vector3 result)public static Angles VectorAngle(Vector3 vec)public static Vector3 VectorPlaneProject(Vector3 v, Vector3 planeNormal)public Vector3 WithAcceleration(Vector3 target, System.Single acceleration)public Vector3 WithFriction(System.Single frictionAmount, System.Single stopSpeed = 140)public Vector3 WithX(System.Single x)public Vector3 WithY(System.Single y)public Vector3 WithZ(System.Single z)Propertiespublic Angles Vector3.EulerAngles { get; set; }public Vector3 Vector3.Inverse { get; set; }public System.Boolean Vector3.IsInfinity { get; set; }public System.Boolean Vector3.IsNaN { get; set; }public System.Boolean Vector3.IsNearZeroLength { get; set; }public System.Single Vector3.Item { get; set; }public System.Single Vector3.Length { get; set; }public System.Single Vector3.LengthSquared { get; set; }public Vector3 Vector3.Normal { get; set; }public static Vector3 Vector3.Random { get; set; }public System.Single Vector3.x { get; set; }public System.Single Vector3.y { get; set; }public System.Single Vector3.z { get; set; }Metadata