Skip to content
shmellyorc edited this page Sep 13, 2026 · 4 revisions

Vect2 is a 2D vector structure with floating-point components for position, direction, and velocity calculations. It is used extensively throughout the engine for spatial calculations.


Fields

Field Type Description
X float The X-component of the vector
Y float The Y-component of the vector

Properties

Property Description
Zero Vector with both components set to zero
One Vector with both components set to one
Up Vector pointing upward (0, -1)
Down Vector pointing downward (0, 1)
Left Vector pointing left (-1, 0)
Right Vector pointing right (1, 0)
IsZero Returns true if both components are approximately zero

Constructors

// Create from X and Y components
var v1 = new Vect2(10f, 20f);

// Create with both components set to the same value
var v2 = new Vect2(5f); // (5, 5)

Operators

Operator Description
+ Adds two vectors component-wise
- Subtracts two vectors component-wise
* Multiplies vector by scalar or component-wise
/ Divides vector by scalar or component-wise
== Compares two vectors for equality
!= Compares two vectors for inequality
- Negates a vector
var a = new Vect2(10f, 20f);
var b = new Vect2(5f, 10f);

var sum = a + b;       // (15, 30)
var diff = a - b;      // (5, 10)
var scaled = a * 2f;   // (20, 40)
var negated = -a;      // (-10, -20)

Camera Transform

Vect2.Transform converts a logical viewport-space point to world space through a BaseCamera:

Vect2 world =
    viewportPosition.Transform(camera);

Equivalent static form:

Vect2 world =
    Vect2.Transform(
        viewportPosition,
        camera);

This calls camera.ScreenToWorld(...).

See Camera → Screen and World Conversion.


Equality and Hashing

Vect2 equality is precision-aware.

Before comparing or hashing components, VOID snaps them using MathHelper.Epsilon. Equals, ==, !=, and GetHashCode therefore use the same quantized semantics.

That consistency matters when vectors are used as dictionary keys or hash-set entries.


Distance

Camera Transform

Vect2.Transform converts a logical viewport-space point to world space through a BaseCamera:

Vect2 world =
    viewportPosition.Transform(camera);

Equivalent static form:

Vect2 world =
    Vect2.Transform(
        viewportPosition,
        camera);

This calls camera.ScreenToWorld(...).

See Camera → Screen and World Conversion.


Equality and Hashing

Vect2 equality is precision-aware.

Before comparing or hashing components, VOID snaps them using MathHelper.Epsilon. Equals, ==, !=, and GetHashCode therefore use the same quantized semantics.

That consistency matters when vectors are used as dictionary keys or hash-set entries.


Distance

Calculates the distance between two vectors.

var a = new Vect2(0f, 0f);
var b = new Vect2(10f, 10f);

float dist = a.Distance(b);       // ~14.14
float dist2 = Vect2.Distance(a, b);

DistanceSquared

Calculates the squared distance between two vectors. More efficient when you only need to compare distances.

float distSq = a.DistanceSquared(b);  // 200

Length

Length

Calculates the length (magnitude) of the vector.

var v = new Vect2(3f, 4f);
float len = v.Length();  // 5

LengthSquared

Calculates the squared length of the vector. More efficient when you only need to compare lengths.

float lenSq = v.LengthSquared();  // 25

Normalize

Returns a normalized version of the vector with a length of 1.

var v = new Vect2(3f, 4f);
var normalized = v.Normalized();  // (0.6, 0.8)

Dot Product

Calculates the dot product between two vectors.

var a = new Vect2(1f, 0f);
var b = new Vect2(0f, 1f);

float dot = a.Dot(b);  // 0
float dot2 = Vect2.Dot(a, b);

Cross Product

Calculates the 2D cross product (scalar) between two vectors.

var a = new Vect2(1f, 0f);
var b = new Vect2(0f, 1f);

float cross = a.Cross(b);  // 1
float cross2 = Vect2.Cross(a, b);

Lerp

Linearly interpolates between two vectors.

var a = new Vect2(0f, 0f);
var b = new Vect2(10f, 10f);

var mid = a.Lerp(b, 0.5f);  // (5, 5)

Clamp

Clamps the vector components between a minimum and maximum vector.

var v = new Vect2(15f, 5f);
var min = new Vect2(0f, 0f);
var max = new Vect2(10f, 10f);

var clamped = v.Clamp(min, max);  // (10, 5)

Min and Max

Returns a vector with the smaller or larger components from two vectors.

var a = new Vect2(5f, 15f);
var b = new Vect2(10f, 10f);

var min = a.Min(b);  // (5, 10)
var max = a.Max(b);  // (10, 15)

MoveTowards

Moves a vector towards a target by a maximum distance.

var current = new Vect2(0f, 0f);
var target = new Vect2(10f, 0f);

var moved = current.MoveTowards(target, 3f);  // (3, 0)

Rotate

Rotates the vector by an angle in radians.

var v = new Vect2(1f, 0f);
var rotated = v.Rotate(MathHelper.HalfPI);  // (0, 1)

Reflect

Reflects a vector off a surface with the specified normal.

var direction = new Vect2(1f, -1f);
var normal = new Vect2(0f, 1f);

var reflected = direction.Reflect(normal);  // (1, 1)

Perpendicular

Returns a vector perpendicular (rotated 90 degrees clockwise).

var v = new Vect2(1f, 0f);
var perp = v.Perpendicular();  // (0, -1)

Round, Floor, Ceiling

Rounds each component to the nearest integer, floor, or ceiling.

var v = new Vect2(3.7f, 4.2f);

var rounded = v.Round();    // (4, 4)
var floored = v.Floor();    // (3, 4)
var ceiled = v.Ceiling();   // (4, 5)

Angle

Converts a direction vector to an angle in radians.

var direction = new Vect2(1f, 0f);
float angle = direction.AngleTo(new Vect2(0f, 1f));  // PI/2

Summary

Method Description
Distance Distance between two vectors
DistanceSquared Squared distance between two vectors
Length Length of the vector
LengthSquared Squared length of the vector
Normalized Normalized version of the vector
Dot Dot product with another vector
Cross Cross product with another vector
Lerp Linear interpolation between two vectors
Clamp Clamp components between min and max
Min Component-wise minimum
Max Component-wise maximum
MoveTowards Move towards a target by a maximum distance
Rotate Rotate by an angle in radians
Reflect Reflect off a surface with a normal
Perpendicular Perpendicular vector (90 degrees clockwise)

Back to Math Library

Clone this wiki locally