Vector2class

A 2-dimensional vector. Typically represents a position, size, or direction in 2D space.

objectVector2
Namespace
global
Assembly
Sandbox.System
Declaration
public sealed struct Vector2

Constructors6

Showing 6 constructors

Methods49

Showing 49 methods

public Vector2 AddClamped(Vector2 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
toAddVector2
maxLengthfloat
Returns:Vector2

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

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

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

public float Angle(Vector2 other)PUBLIC

Returns the distance between this vector and another in degrees.

ParameterTypeDescription
otherVector2
Returns:float

public Vector2 Approach(float length, float amount)PUBLIC

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

ParameterTypeDescription
lengthfloat
amountfloat
Returns:Vector2

public Vector2 ComponentMax(Vector2 other)PUBLIC

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

ParameterTypeDescription
otherVector2
Returns:Vector2

public Vector2 ComponentMin(Vector2 other)PUBLIC

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

ParameterTypeDescription
otherVector2
Returns:Vector2

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

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

ParameterTypeDescription
sourceVector2Point A of the curve.
targetVector2Point B of the curve.
sourceTangentVector2Tangent for the Point A.
targetTangentVector2Tangent for the Point B.
tfloatHow far along the path to get a point on. Range is 0 to 1, inclusive.
Returns:Vector2The point on the curve

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

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

ParameterTypeDescription
fromVector2
toVector2
Returns:Vector2

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

Returns distance between the 2 given vectors.

ParameterTypeDescription
aVector2
bVector2
Returns:float

public static float DistanceBetweenSquared(Vector2 a, Vector2 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
aVector2
bVector2
Returns:float

public static Vector2 FromDegrees(float degrees)PUBLICSTATIC

Returns a point on a circle at given rotation from X axis, counter clockwise.

ParameterTypeDescription
degreesfloat
Returns:Vector2

public static Vector2 FromRadians(float radians)PUBLICSTATIC

Returns a point on a circle at given rotation from X axis, counter clockwise.

ParameterTypeDescription
radiansfloat
Returns:Vector2

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

Returns the distance between two direction vectors in degrees.

ParameterTypeDescription
v1Vector2
v2Vector2
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 static Vector2 Reflect(Vector2 direction, Vector2 normal)PUBLICSTATIC

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

ParameterTypeDescription
directionVector2
normalVector2
Returns:Vector2

public Vector2 RotateAround(Vector2 center, float angleDegrees)PUBLIC

Rotate this vector around given point by given angle in degrees and return the result as a new vector.

ParameterTypeDescription
centerVector2
angleDegreesfloat
Returns:Vector2

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

Smoothly move towards the target vector

ParameterTypeDescription
currentVector2
targetVector2
velocityVector2
smoothTimefloat
deltaTimefloat
Returns:Vector2

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

Snap to grid along all 3 axes.

ParameterTypeDescription
gridSizefloat
sx = Truebool
sy = Truebool
Returns:Vector2

public static void Sort(Vector2 min, Vector2 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 and y values.

ParameterTypeDescription
minVector2
maxVector2
Returns:void

public Vector2 SubtractDirection(Vector2 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
directionVector2
strength = 1float
Returns:Vector2

public Vector2 WithAcceleration(Vector2 target, float accelerate)PUBLIC

Move to the target vector, by amount acceleration

ParameterTypeDescription
targetVector2
acceleratefloat
Returns:Vector2

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

ParameterTypeDescription
frictionAmountfloat
stopSpeed = 140float
Returns:Vector2

public Vector2 WithX(float x)PUBLIC

Return this vector with given X.

ParameterTypeDescription
xfloat
Returns:Vector2

public Vector2 WithY(float y)PUBLIC

Return this vector with given Y.

ParameterTypeDescription
yfloat
Returns:Vector2

Properties19

Showing 19 properties

public float Vector2.Degrees { get; set; }PUBLICGETSET

Return the angle of this vector in degrees, always between 0 and 360

Returns:float

public static Vector2 Vector2.Down { get; set; }PUBLICSTATICGETSET

Returns a 2D vector with Y set to 1. This typically represents down in 2D space.

Returns:Vector2

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

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

Returns:Vector2

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

Returns true if x, y, or z are infinity

Returns:bool

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

Returns true if x, y, or z are NaN

Returns:bool

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

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

Returns:bool

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

Returns:float

public static Vector2 Vector2.Left { get; set; }PUBLICSTATICGETSET

Returns a 2D vector with X set to -1. This typically represents the left hand direction in 2D space.

Returns:Vector2

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

Returns the magnitude of the vector

Returns:float

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

This is faster than Length, so is better to use in certain circumstances

Returns:float

public static Vector2 Vector2.One { get; set; }PUBLICSTATICGETSET

Returns a 2D vector with every component set to 1

Returns:Vector2

public Vector2 Vector2.Perpendicular { get; set; }PUBLICGETSET

Returns a vector that runs perpendicular to this one

Returns:Vector2

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

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

Returns:Vector2

public static Vector2 Vector2.Right { get; set; }PUBLICSTATICGETSET

Returns a 2D vector with X set to 1. This typically represents the right hand direction in 2D space.

Returns:Vector2

public static Vector2 Vector2.Up { get; set; }PUBLICSTATICGETSET

Returns a 2D vector with Y set to -1. This typically represents up in 2D space.

Returns:Vector2

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

X component of this vector.

Returns:float

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

Y component of this vector.

Returns:float

public static Vector2 Vector2.Zero { get; set; }PUBLICSTATICGETSET

Returns a 2D vector with every component set to 0

Returns:Vector2

On this page

Constructorspublic Vector2(System.Numerics.Vector2 v)public Vector2(System.Single x, System.Single y)public Vector2(System.Single all)public Vector2(Vector2 other)public Vector2(Vector3 v)public Vector2(Vector4 v)Methodspublic Vector2 Abs()public static Vector2 Abs(Vector2 value)public Vector2 AddClamped(Vector2 toAdd, System.Single maxLength)public System.Boolean AlmostEqual(Vector2 v, System.Single delta = 0.0001)public System.Single Angle(Vector2 other)public Vector2 Approach(System.Single length, System.Single amount)public Vector2 Clamp(System.Single min, System.Single max)public Vector2 Clamp(Vector2 otherMin, Vector2 otherMax)public static Vector2 Clamp(Vector2 value, Vector2 min, Vector2 max)public Vector2 ClampLength(System.Single minLength, System.Single maxLength)public Vector2 ClampLength(System.Single maxLength)public Vector2 ComponentMax(Vector2 other)public Vector2 ComponentMin(Vector2 other)public static Vector2 CubicBezier(Vector2 source, Vector2 target, Vector2 sourceTangent, Vector2 targetTangent, System.Single t)public static Vector2 Direction(Vector2 from, Vector2 to)public System.Single Distance(Vector2 target)public static System.Single Distance(Vector2 a, Vector2 b)public static System.Single DistanceBetween(Vector2 a, Vector2 b)public static System.Single DistanceBetweenSquared(Vector2 a, Vector2 b)public System.Single DistanceSquared(Vector2 target)public static System.Single DistanceSquared(Vector2 a, Vector2 b)public static System.Single Dot(Vector2 a, Vector2 b)public System.Single Dot(Vector2 b)public static Vector2 FromDegrees(System.Single degrees)public static Vector2 FromRadians(System.Single radians)public static System.Single GetAngle(Vector2 v1, Vector2 v2)public System.Boolean IsNearlyZero(System.Single tolerance = 0.0001)public static Vector2 Lerp(Vector2 a, Vector2 b, System.Single frac, System.Boolean clamp = True)public static Vector2 Lerp(Vector2 a, Vector2 b, Vector2 t, System.Boolean clamp = True)public Vector2 LerpTo(Vector2 target, System.Single t, System.Boolean clamp = True)public Vector2 LerpTo(Vector2 target, Vector2 t, System.Boolean clamp = True)public static Vector2 Max(Vector2 a, Vector2 b)public static Vector2 Min(Vector2 a, Vector2 b)public static override Vector2 Parse(System.String str, System.IFormatProvider provider)public static Vector2 Parse(System.String str)public static Vector2 Reflect(Vector2 direction, Vector2 normal)public Vector2 RotateAround(Vector2 center, System.Single angleDegrees)public static Vector2 SmoothDamp(Vector2 current, Vector2 target, Vector2 velocity, System.Single smoothTime, System.Single deltaTime)public Vector2 SnapToGrid(System.Single gridSize, System.Boolean sx = True, System.Boolean sy = True)public static System.Void Sort(Vector2 min, Vector2 max)public static Vector2 SpringDamp(Vector2 current, Vector2 target, Vector2 velocity, System.Single smoothTime, System.Single deltaTime, System.Single frequency = 2, System.Single damping = 0.5)public static Vector2 SpringDamp(Vector2 current, Vector2 target, Vector2 velocity, System.Single deltaTime, System.Single frequency = 2, System.Single damping = 0.5)public Vector2 SubtractDirection(Vector2 direction, System.Single strength = 1)public static override System.Boolean TryParse(System.String str, System.IFormatProvider provider, Vector2 result)public static System.Boolean TryParse(System.String str, Vector2 result)public Vector2 WithAcceleration(Vector2 target, System.Single accelerate)public Vector2 WithFriction(System.Single frictionAmount, System.Single stopSpeed = 140)public Vector2 WithX(System.Single x)public Vector2 WithY(System.Single y)Propertiespublic System.Single Vector2.Degrees { get; set; }public static Vector2 Vector2.Down { get; set; }public Vector2 Vector2.Inverse { get; set; }public System.Boolean Vector2.IsInfinity { get; set; }public System.Boolean Vector2.IsNaN { get; set; }public System.Boolean Vector2.IsNearZeroLength { get; set; }public System.Single Vector2.Item { get; set; }public static Vector2 Vector2.Left { get; set; }public System.Single Vector2.Length { get; set; }public System.Single Vector2.LengthSquared { get; set; }public Vector2 Vector2.Normal { get; set; }public static Vector2 Vector2.One { get; set; }public Vector2 Vector2.Perpendicular { get; set; }public static Vector2 Vector2.Random { get; set; }public static Vector2 Vector2.Right { get; set; }public static Vector2 Vector2.Up { get; set; }public System.Single Vector2.x { get; set; }public System.Single Vector2.y { get; set; }public static Vector2 Vector2.Zero { get; set; }Metadata