Vector2
A 2-dimensional vector for positions, directions, and sizes in 2D space.
using Xengine.Math;
Constructors
var v1 = new Vector2(3, 4); // X=3, Y=4
var v2 = new Vector2(5); // X=5, Y=5 (uniform)
Static Values
| Constant | Value | Description |
|---|---|---|
Vector2.Zero | (0, 0) | Origin point |
Vector2.One | (1, 1) | All ones |
Vector2.Up | (0, 1) | Up direction (+Y) |
Vector2.Down | (0, -1) | Down direction (-Y) |
Vector2.Left | (-1, 0) | Left direction (-X) |
Vector2.Right | (1, 0) | Right direction (+X) |
Properties
| Property | Type | Description |
|---|---|---|
X | float | X component |
Y | float | Y component |
Length | float | Magnitude of the vector |
LengthSquared | float | Squared magnitude (faster) |
Normalized | Vector2 | Unit vector (length = 1) |
Methods
Distance
static float Distance(Vector2 a, Vector2 b)
float dist = Vector2.Distance(playerPos, targetPos);
DistanceTo
float DistanceTo(Vector2 other)
float dist = myPos.DistanceTo(targetPos);
Dot
static float Dot(Vector2 a, Vector2 b)
float dot = Vector2.Dot(direction, forward);
Lerp
static Vector2 Lerp(Vector2 a, Vector2 b, float t)
// Smooth movement
position = Vector2.Lerp(position, target, 5f * Time.Delta);
Reflect
Vector2 Reflect(Vector2 normal)
velocity = velocity.Reflect(wallNormal);
Clamp
Vector2 Clamp(Vector2 min, Vector2 max)
position = position.Clamp(Vector2.Zero, screenSize);
Operators
var a = new Vector2(1, 2);
var b = new Vector2(3, 4);
var sum = a + b; // (4, 6)
var diff = a - b; // (-2, -2)
var scaled = a * 2f; // (2, 4)
var divided = a / 2f; // (0.5, 1)
var negated = -a; // (-1, -2)
Common Uses
// Screen positions
var screenPos = new Vector2(640, 360);
// Texture sizes
var size = new Vector2(128, 128);
// 2D movement
var direction = new Vector2(1, 0);
position += direction * speed * Time.Delta;
// UI bounds
var uiSize = new Vector2(200, 50);