Vector3
A 3-dimensional vector representing a position, direction, or size in 3D space.
using Xengine.Math;
Constructors
public Vector3(float x, float y, float z)
var position = new Vector3(1, 2, 3);
var uniform = new Vector3(5); // (5, 5, 5)
Static Values
| Constant | Value | Description |
|---|---|---|
Vector3.Zero | (0, 0, 0) | Origin point |
Vector3.One | (1, 1, 1) | All ones |
Vector3.Forward | (0, 0, 1) | Forward direction (+Z) |
Vector3.Backward | (0, 0, -1) | Backward direction (-Z) |
Vector3.Up | (0, 1, 0) | Up direction (+Y) |
Vector3.Down | (0, -1, 0) | Down direction (-Y) |
Vector3.Right | (1, 0, 0) | Right direction (+X) |
Vector3.Left | (-1, 0, 0) | Left direction (-X) |
Properties
Length
float Length { get; }
The magnitude (length) of the vector.
var v = new Vector3(3, 4, 0);
float len = v.Length; // 5.0
LengthSquared
float LengthSquared { get; }
Squared length. Faster than Length for comparisons.
// Check if within range (faster than using Length)
if (v.LengthSquared < range * range) { ... }
Normalized
Vector3 Normalized { get; }
Returns vector with length of 1 (unit vector).
var direction = (target - position).Normalized;
position += direction * speed * Time.Delta;
Methods
Dot
static float Dot(Vector3 a, Vector3 b)
Dot product. Returns how much two vectors point in the same direction.
float dot = Vector3.Dot(forward, toTarget);
// dot > 0: target is in front
// dot < 0: target is behind
// dot = 0: target is to the side
Cross
static Vector3 Cross(Vector3 a, Vector3 b)
Cross product. Returns a vector perpendicular to both inputs.
var right = Vector3.Cross(Vector3.Up, forward);
Distance
static float Distance(Vector3 a, Vector3 b)
Distance between two points.
float dist = Vector3.Distance(player.Position, enemy.Position);
if (dist < attackRange) { Attack(); }
DistanceTo
float DistanceTo(Vector3 other)
Instance method version of Distance.
float dist = myPosition.DistanceTo(targetPosition);
Lerp
static Vector3 Lerp(Vector3 a, Vector3 b, float t)
Linear interpolation between two vectors. t is clamped to [0, 1].
// Smooth movement towards target
position = Vector3.Lerp(position, target, 5f * Time.Delta);
// Get point halfway between A and B
var midpoint = Vector3.Lerp(a, b, 0.5f);
LerpTo
Vector3 LerpTo(Vector3 target, float t)
Instance method version of Lerp.
position = position.LerpTo(target, 0.1f);
Reflect
Vector3 Reflect(Vector3 normal)
Reflects vector off a surface with the given normal.
// Bounce off a wall
velocity = velocity.Reflect(wallNormal);
Clamp
Vector3 Clamp(Vector3 min, Vector3 max)
Clamps each component between min and max.
// Keep player in bounds
position = position.Clamp(new Vector3(-10, 0, -10), new Vector3(10, 5, 10));
ClampLength
Vector3 ClampLength(float maxLength)
Clamps the vector's length to a maximum value.
// Limit velocity to max speed
velocity = velocity.ClampLength(maxSpeed);
Operators
var a = new Vector3(1, 2, 3);
var b = new Vector3(4, 5, 6);
var sum = a + b; // (5, 7, 9)
var diff = a - b; // (-3, -3, -3)
var scaled = a * 2f; // (2, 4, 6)
var divided = a / 2f; // (0.5, 1, 1.5)
var negated = -a; // (-1, -2, -3)
var product = a * b; // Component-wise: (4, 10, 18)
Common Patterns
Movement Direction
var move = Vector3.Zero;
if (RaylibInput.IsKeyDown(KeyCode.W)) move.Z += 1;
if (RaylibInput.IsKeyDown(KeyCode.S)) move.Z -= 1;
if (RaylibInput.IsKeyDown(KeyCode.A)) move.X -= 1;
if (RaylibInput.IsKeyDown(KeyCode.D)) move.X += 1;
if (move.LengthSquared > 0)
{
move = move.Normalized * speed * Time.Delta;
Transform.Position += move;
}
Follow Target
var direction = (target.Position - Transform.Position).Normalized;
Transform.Position += direction * speed * Time.Delta;
Orbit Around Point
float angle = Time.Now * orbitSpeed;
Transform.Position = new Vector3(
center.X + MathF.Cos(angle) * radius,
center.Y,
center.Z + MathF.Sin(angle) * radius
);