struct Vec2

A vector with 2 components: x and y. This can represent a point in 2D space, a directional vector, or any other sort of value with 2 dimensions to it!

Instance Fields and Properties

   
float Length This is the length of the vector! Or the distance from the origin to this point. Uses Math.Sqrt, so it’s not dirt cheap or anything.
float LengthSq This is the squared length/magnitude of the vector! It skips the Sqrt call, and just gives you the squared version for speedy calculations that can work with it squared.
float Magnitude Magnitude is the length of the vector! Or the distance from the origin to this point. Uses Math.Sqrt, so it’s not dirt cheap or anything.
float MagnitudeSq This is the squared magnitude of the vector! It skips the Sqrt call, and just gives you the squared version for speedy calculations that can work with it squared.
Vec2 Normalized Creates a normalized vector (vector with a length of 1) from the current vector. Will not work properly if the vector has a length of zero.
Vector2 v The internal, wrapped System.Numerics type. This can be nice to have around so you can pass its fields as ‘ref’, which you can’t do with properties. You won’t often need this, as implicit conversions to System.Numerics types are also provided.
float x X component.
Vec3 X0Y Promotes this Vec2 to a Vec3, using 0 for the Y axis.
Vec3 XY0 Promotes this Vec2 to a Vec3, using 0 for the Z axis.
float y Y component.
Vec2 YX A transpose swizzle property, returns (y,x)

Instance Methods

   
Vec2 A basic constructor, just copies the values in!
Angle Returns the counter-clockwise degrees from [1,0]. Resulting value is between 0 and 360. Vector does not need to be normalized.
InRadius Checks if a point is within a certain radius of this one. This is an easily readable shorthand of the squared distance check.
Normalize Turns this vector into a normalized vector (vector with a length of 1) from the current vector. Will not work properly if the vector has a length of zero.
ToString Mostly for debug purposes, this is a decent way to log or inspect the vector in debug mode. Looks like “[x, y]”

Static Fields and Properties

   
Vec2 One A Vec2 with all components at one, this is the same as new Vec2(1,1).
Vec2 UnitX A normalized Vector that points down the X axis, this is the same as new Vec2(1,0).
Vec2 UnitY A normalized Vector that points down the Y axis, this is the same as new Vec2(0,1).
Vec2 Zero A Vec2 with all components at zero, this is the same as new Vec2(0,0).

Static Methods

   
AngleBetween Calculates a signed angle between two vectors in degrees! Sign will be positive if B is counter-clockwise (left) of A, and negative if B is clockwise (right) of A. Vectors do not need to be normalized. NOTE: Since this will return a positive or negative angle, order of parameters matters!
Distance Calculates the distance between two points in space! Make sure they’re in the same coordinate space! Uses a Sqrt, so it’s not blazing fast, prefer DistanceSq when possible.
DistanceSq Calculates the distance between two points in space, but leaves them squared! Make sure they’re in the same coordinate space! This is a fast function :)
Dot The dot product is an extremely useful operation! One major use is to determine how similar two vectors are. If the vectors are Unit vectors (magnitude/length of 1), then the result will be 1 if the vectors are the same, -1 if they’re opposite, and a gradient in-between with 0 being perpendicular. See Freya Holmer’s excellent visualization of this concept
FromAngle Creates a vector pointing in the direction of the angle, with a length of 1. Angles are counter-clockwise, and start from (1,0), so an angle of 90 will be (0,1).
Lerp Blends (Linear Interpolation) between two vectors, based on a ‘blend’ value, where 0 is a, and 1 is b. Doesn’t clamp percent for you.
Max Returns a vector where each elements is the maximum value for each corresponding pair.
Min Returns a vector where each elements is the minimum value for each corresponding pair.

Operators

   
+ Adds matching components together. Commutative.
/ A component-wise vector division. Not commutative
Implicit Conversions Allows implicit conversion from System.Numerics.Vector2 to StereoKit.Vec2.
* A component-wise vector multiplication, same thing as a non-uniform scale. NOT a dot product! Commutative.
- Subtracts matching components from eachother. Not commutative.
- Vector negation, returns a vector where each component has been negated.




Found an issue with these docs, or have some additional questions? Create an Issue on Github!