Artromskiy/DVG.Maths
Pure C# Vector Library with Fixed Point Math
DVG.Maths
A lightweight, engine-agnostic mathematics library for .NET.
This library provides fixed-point arithmetic, vector types, swizzling support, and a wide set of math utilities without any dependency on System.Numerics, Unity, or other game engines. It is designed for deterministic simulations, custom engines, server-side logic, and projects where full control over numeric behavior is required.
โจ Features
โ No External Engine Dependencies
- No
System.Numerics - No Unity types
- Pure .NET implementation
- Suitable for runtime and server environments
๐ข Fixed-Point Arithmetic (fix)
A 16.16 fixed-point implementation focused on deterministic behavior.
Characteristics
-
16-bit integer part, 16-bit fractional part
-
Overflow-checked arithmetic
-
Deterministic string formatting (via
decimal) -
Explicit/implicit conversions to and from:
intfloatdoubledecimal
Constants
ZeroOneMinValueMaxValuePiE
Supported Operations
- Arithmetic operators (
+,-,*,/,%) - Bitwise operators (
&,|,^,~,<<,>>) - Comparison operators
- Increment / decrement
- Parsing (
Parse) - Hashing and comparison
Ideal for deterministic gameplay logic, lockstep multiplayer, and simulation systems.
๐ Vector Types
The library provides vector types for multiple numeric formats.
Boolean Vectors
bool2bool3bool4
Useful for:
- Masking
- Per-component comparisons
- SIMD-like logic patterns
- Branch-free math flows
2D
fix2,float2,double2int2,uint2bool2
3D
fix3,float3,double3int3,uint3bool3
4D
fix4,float4,double4int4,uint4bool4
๐ Swizzling
Swizzling is supported up to 4 dimensions.
You can reorder components or construct new vectors from existing ones in a familiar shader-style way.
Examples:
fix3 v = new fix3(3, 1, 5);
var xy = v.xy; // fix2(3, 1)
var yzx = v.yzx; // fix3(1, 5, 3)
var xxxx = v.xxxx; // fix4(3, 3, 3, 3)Zero-Swizzle Support
Special underscore-prefixed swizzles allow injecting zero into specific components.
Example:
fix3 v = new fix3(3, 1, 5);
var result = v._xy; // fix3(0, 1, 5)This allows convenient construction of partially zeroed vectors without extra allocations or helper calls.
Swizzling is available up to 4 components.
๐ Common Vector Operations
Across numeric vector types:
Length,SqrLengthDistance,SqrDistanceDot,CrossNormalizeClampLengthMoveTowardsRotateTowardsReflect,RefractFaceForwardLerp,Mix,SmoothStepSmoothDamp,SmoothDampAngleDeltaAngleRepeatRemap
Comparison helpers:
LesserThan,LesserThanEqualGreaterThan,GreaterThanEqualEqual,NotEqual
๐งฎ Maths Utility Class
The Maths static class provides a consistent math API for:
floatdoubleintuintlong
Interpolation & Mapping
LerpInvLerpSmoothStepStepRemapMix
Trigonometry
Sin,Cos,TanAsin,Acos,Atan,Atan2- Hyperbolic functions
Radians,Degrees
Exponential & Logarithmic
PowExp,Exp2Log,Log2,Log10Sqrt,InverseSqrtCbrt
Rounding & Precision
FloorCeilRoundRoundEvenTruncateFractModFma
Utility
-
Min,Max,Clamp -
Abs,Sign -
Bit conversions:
FloatBitsToIntFloatBitsToUIntIntBitsToFloatUIntBitsToFloat
-
IsNaN -
IsInfinity
The API is intentionally familiar to developers coming from GLSL, HLSL, Unity.Mathematics, or System.Math โ while remaining completely independent.
๐ฏ Use Cases
- Deterministic multiplayer games (lockstep / rollback)
- ECS-based architectures
- Custom engines
- Server-side simulations
- Physics logic
- Tools requiring predictable numeric behavior
- Projects where floating-point nondeterminism is unacceptable
๐งฉ Design Goals
- Determinism
- Explicit numeric control
- Minimal dependencies
- High inlining potential
- Familiar API surface
- Engine independence
Example
using DVG;
fix a = (fix)1.5f;
fix b = (fix)2;
fix result = a * b;
fix3 position = new fix3(a, b, fix.One);
fix length = position.Length();
float angle = Maths.Radians(90f);If you are building systems where numeric behavior must be predictable โ especially in multiplayer or simulation-heavy environments โ this library provides a portable and deterministic math foundation without engine constraints.