s&box docs

public sealed struct Vector2

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

Broader workflow and conceptual references connected to this API.

Constructors

Showing 6 constructors

Methods

Showing 49 methods

public Vector2 AddClamped(Vector2 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.

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

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

Parameters

  • v: The value to compare with
  • delta: The max difference between component values Default: 0.0001

Returns

True if nearly equal

public float Angle(Vector2 other)

Returns the distance between this vector and another in degrees.

public Vector2 Approach(float length, float amount)

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

public Vector2 ComponentMax(Vector2 other)

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

public Vector2 ComponentMin(Vector2 other)

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

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

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

Parameters

  • source: Point A of the curve.
  • target: Point B of the curve.
  • sourceTangent: Tangent for the Point A.
  • targetTangent: Tangent for the Point B.
  • t: How far along the path to get a point on. Range is 0 to 1, inclusive.

Returns

The point on the curve

public static Vector2 Direction(Vector2 from, Vector2 to)

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

public static float DistanceBetween(Vector2 a, Vector2 b)

Returns distance between the 2 given vectors.

public static float DistanceBetweenSquared(Vector2 a, Vector2 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.

public static Vector2 FromDegrees(float degrees)

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

public static Vector2 FromRadians(float radians)

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

public static float GetAngle(Vector2 v1, Vector2 v2)

Returns the distance between two direction vectors in degrees.

public bool IsNearlyZero(float tolerance = 0.0001)

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

Parameters

  • tolerance: Default: 0.0001

public static Vector2 Max(Vector2 a, Vector2 b)

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

public static Vector2 Min(Vector2 a, Vector2 b)

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

public static Vector2 Reflect(Vector2 direction, Vector2 normal)

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

public Vector2 RotateAround(Vector2 center, float angleDegrees)

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

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

Smoothly move towards the target vector

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

Snap to grid along all 3 axes.

Parameters

  • sx: Default: True
  • sy: Default: True

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

public Vector2 SubtractDirection(Vector2 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.

Parameters

  • strength: Default: 1

public Vector2 WithAcceleration(Vector2 target, float accelerate)

Move to the target vector, by amount acceleration

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

Parameters

  • stopSpeed: Default: 140

public Vector2 WithX(float x)

Return this vector with given X.

public Vector2 WithY(float y)

Return this vector with given Y.

Properties

Showing 19 properties

public float Vector2.Degrees { get; set; }

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

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

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

public Vector2 Vector2.Inverse { get; set; }

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

public bool Vector2.IsInfinity { get; set; }

Returns true if x, y, or z are infinity

public bool Vector2.IsNaN { get; set; }

Returns true if x, y, or z are NaN

public bool Vector2.IsNearZeroLength { get; set; }

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

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

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

public float Vector2.Length { get; set; }

Returns the magnitude of the vector

public float Vector2.LengthSquared { get; set; }

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

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

Returns a 2D vector with every component set to 1

public Vector2 Vector2.Perpendicular { get; set; }

Returns a vector that runs perpendicular to this one

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

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

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

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

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

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

public float Vector2.x { get; set; }

X component of this vector.

public float Vector2.y { get; set; }

Y component of this vector.

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

Returns a 2D vector with every component set to 0

Metadata

FieldValue
Namespaceglobal
Typeclass
AssemblySandbox.System
Doc IDT: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