API Vect2 - shmellyorc/Box GitHub Wiki

Vect2

Namespace: Box.Systems

Represents a 2D vector with X and Y components. Supports common vector operations, comparisons, transformations, and conversion.


Constructors

public Vect2(float x, float y);
public Vect2(float value);
public Vect2(Vect2 value);

Copy constructor: creates a new vector from an existing one.


Static Properties

Name Type Description
Zero Vect2 (0, 0)
One Vect2 (1, 1)
Up Vect2 (0, -1)
Right Vect2 (1, 0)
Down Vect2 (0, 1)
Left Vect2 (-1, 0)

Instance Properties

Name Type Description
float X float X component
float Y float Y component
bool IsZero bool True if both X and Y are zero

Operators

Operator Description
+, -, *, / with Vect2 or float Component-wise arithmetic or scalar ops.
unary - Negation: (-X, -Y).
==, != Equality/Inequality by components.
<, >, <=, >= Component-wise comparisons (both axes).

Methods

Signature Description
Vect2 Add(Vect2 other) / static Vect2 Add(Vect2 a, Vect2 b) Component-wise addition.
Vect2 Subtract(Vect2 other) / static Vect2 Subtract(Vect2 a, Vect2 b) Component-wise subtraction.
Vect2 Multiply(float s) / static Vect2 Multiply(Vect2 v, float s) Scale by scalar.
Vect2 Divide(float s) / static Vect2 Divide(Vect2 v, float s) Divide by scalar.
Vect2 Abs() / static Vect2 Abs(Vect2 v) Absolute value of each component.
Vect2 Floor() / static Vect2 Floor(Vect2 v) Floor each component.
Vect2 Ceiling() / static Vect2 Ceiling(Vect2 v) Ceil each component.
Vect2 Round() / static Vect2 Round(Vect2 v, int digits) Round components to nearest / specified decimals.
Vect2 Clamp(Vect2 min, Vect2 max) / static Vect2 Clamp(Vect2 v, Vect2 min, Vect2 max) Clamp components between vectors.
float Dot(Vect2 other) / static float Dot(Vect2 a, Vect2 b) Dot product.
float Length() / static float Length(Vect2 v) Euclidean length (magnitude).
float LengthSquared() / static float LengthSquared(Vect2 v) Squared length (avoid sqrt).
float Distance(Vect2 other) / static float Distance(Vect2 a, Vect2 b) Euclidean distance between vectors.
float DistanceSquared(Vect2 other) / static float DistanceSquared(Vect2 a, Vect2 b) Squared distance.
Vect2 Normalized() / static Vect2 Normalized(Vect2 v) Unit vector (length = 1).
Vect2 Direction(Vect2 other, bool normalized) / static Vect2 Direction(Vect2 a,Vect2 b,bool) Vector from this to other, optionally normalized.
Vect2 Lerp(Vect2 a, Vect2 b, float t) Linear interpolation.
float Lerp(float a, float b, float t) Scalar interpolation.
Vect2 MoveTowards(Vect2 target, float maxDelta) / static Vect2 MoveTowards(Vect2 cur,Vect2 tgt,float) Move component-wise toward target up to maxDelta.
static float ToFps(float seconds) Convert seconds per frame to FPS: 1/seconds.
static Vect2 Rotate(Vect2 v, float radians) / Vect2.Rotate(radians) Rotate vector by angle in radians.
static Vect2 Reflect(Vect2 vec, Vect2 normal) Reflect vector about a normal.
static Vect2 To2D(int index, int size) / overloads Convert 1D index into 2D grid coordinate.
static int To1D(Vect2 loc, int size) Convert 2D coordinate to 1D index in grid.
static float LookAt(Vect2 a, Vect2 b) Angle in radians from a toward b.
static Vect2 AngleTo(float rad, bool normalized=true) Convert angle to direction vector, normalized by default.
static float WrapAngle(float radians) Wrap angle into range [-π, π].
static float TurnToFace(Vect2 target, float curRot, Vect2 origin, float speed) Smoothly turn toward target by max speed.
static Vect2 Transform(Vect2 pos, Camera cam) Map world position to screen coords.

Usage Example

var a = new Vect2(2, 3);
var b = new Vect2(5, 7);

// Arithmetic
var sum = a + b;          // (7, 10)
var scaled = a * 2f;      // (4, 6)

// Length & distance
float len = a.Length();   // ≈3.606
float dist = Vect2.Distance(a, b); // ≈5

// Normalize & dot
var unit = a.Normalized();
float dp = a.Dot(b);

// Rotate
var r = Vect2.Rotate(a, MathF.PI/2);

// Grid conversion
var coord = Vect2.To2D(13, 5); // (3,2)
int idx = Vect2.To1D(new Vect2(3,2), 5); // 13
⚠️ **GitHub.com Fallback** ⚠️