-
Notifications
You must be signed in to change notification settings - Fork 1
Vect2
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.
| Field | Type | Description |
|---|---|---|
X |
float |
The X-component of the vector |
Y |
float |
The Y-component of the vector |
| 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 |
// 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)| 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)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);Calculates the squared distance between two vectors. More efficient when you only need to compare distances.
float distSq = a.DistanceSquared(b); // 200Calculates the length (magnitude) of the vector.
var v = new Vect2(3f, 4f);
float len = v.Length(); // 5Calculates the squared length of the vector. More efficient when you only need to compare lengths.
float lenSq = v.LengthSquared(); // 25Returns 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)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);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);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)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)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)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)Rotates the vector by an angle in radians.
var v = new Vect2(1f, 0f);
var rotated = v.Rotate(MathHelper.HalfPI); // (0, 1)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)Returns a vector perpendicular (rotated 90 degrees clockwise).
var v = new Vect2(1f, 0f);
var perp = v.Perpendicular(); // (0, -1)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)Converts a direction vector to an angle in radians.
var direction = new Vect2(1f, 0f);
float angle = direction.AngleTo(new Vect2(0f, 1f)); // PI/2Vect2 can be implicitly converted to and from SFML vector types.
SFVector2f sfmlVec = new SFVector2f(10f, 20f);
Vect2 v = sfmlVec; // Implicit conversion
SFVector2f sfmlVec2 = v; // Implicit conversion| 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) |
- MathHelper
- FileHelper
- HashHelper
- JsonHelper
- MapHelper
- TextHelper
- SoundHelper
- AlignHelpers
- Instance Helper
- Enum Extensions
- String Extensions
- Int Extensions
- Float Extensions
- IEnumerable Extensions
- Random Extensions
- Sound Extensions
- Font Extensions