Skip to main content
s&box provides a set of math types for working with positions, directions, and transforms in 3D space. These types are available in all game and editor code.

Vectors

A vector is a set of numbers that describes a position or direction in space.
TypeDescription
Vector22D position or direction — x and y
Vector33D position or direction — x, y, and z
Vector43D with a bonus W value — used for shaders, margins, and borders
Vector2IntInteger version of Vector2
Vector3IntInteger version of Vector3
// Create vectors
var a = new Vector3( 1, 2, 3 );
var b = new Vector3( 4, 5, 6 );

// Read and write components
float x = a.x;
a.z = 10;

// Arithmetic
var sum        = a + b;
var difference = b - a;
var scaled     = a * 2;
var halved     = a / 2;

// Length and normalization
float length    = a.Length;
Vector3 normal  = a.Normal;

// Distance between two points
float distance = a.Distance( b );

// Dot product — how similar two directions are
float dot = Vector3.Dot( a, b );

// Cross product — direction perpendicular to both inputs
Vector3 cross = Vector3.Cross( a, b );

// Smooth interpolation (0.5 = halfway between a and b)
Vector3 halfway = Vector3.Lerp( a, b, 0.5f );

Rotation and Angles

Rotation

A Rotation represents an orientation in 3D space. It is implemented as a quaternion, which avoids gimbal lock and composes reliably. Use Rotation in code wherever you store or manipulate orientations.
// Create axis rotations
Rotation ry = Rotation.FromYaw( 90 );      // turn right 90°
Rotation rx = Rotation.FromPitch( 45 );    // look up 45°
Rotation combined = ry * rx;               // apply both

// Get direction vectors
Vector3 fwd   = combined.Forward;
Vector3 up    = combined.Up;
Vector3 right = combined.Right;

// Create a rotation that faces a direction
Rotation look = Rotation.LookAt( new Vector3( 1, 0, 0 ), Vector3.Up );

// Apply a rotation to a direction vector
Vector3 rotated = combined * Vector3.Forward;

// Invert a rotation
Rotation inverse = combined.Inverse;

// Smooth interpolation between two rotations
Rotation start   = Rotation.FromYaw( 0 );
Rotation end     = Rotation.FromYaw( 180 );
Rotation halfway = Rotation.Slerp( start, end, 0.5f );

Angles

Angles stores orientation as pitch, yaw, and roll — easier to read and write by hand, but prone to gimbal lock. Use it for authoring values in the editor or for player input, then convert to Rotation for calculations.
// Create from pitch, yaw, roll
Angles ang = new Angles( 30, 90, 0 );

// Convert to Rotation for use in code
Rotation rot = ang.ToRotation();

// Convert back to Angles if needed
Angles ang2 = rot.ToAngles();

Transform

A Transform holds a Position (Vector3), a Rotation (Rotation), and a Scale (Vector3). Together these fully describe where something is, which way it faces, and how large it is. Every GameObject exposes two transforms:
  • GameObject.WorldTransform — position in world space
  • GameObject.LocalTransform — position relative to the parent
// Identity transform at the world origin
Transform t = Transform.Zero;

// Create a transform at a custom position and rotation
Vector3 pos    = new Vector3( 10, 0, 0 );
Rotation rot   = Rotation.FromYaw( 90 );
Transform custom = new Transform( pos, rot, 1f );  // scale = 1

// Read individual parts
Vector3  p     = custom.Position;
Rotation r     = custom.Rotation;
float    scale = custom.Scale;

// Get direction vectors
Vector3 forward = custom.Forward;
Vector3 up      = custom.Up;
Vector3 right   = custom.Right;

// Convert between local and world space
Vector3 local       = new Vector3( 1, 0, 0 );
Vector3 world       = custom.PointToWorld( local );
Vector3 backToLocal = custom.PointToLocal( world );

// Convert to and from a Matrix
Matrix    mat      = custom.ToMatrix();
Transform fromMat  = Transform.FromMatrix( mat );