Math

June 19, 2026 · View on GitHub

Mathematical constants, random utilities, and geometry helpers in FronkonGames.GameWork.Foundation. All types are static, no components or setup required.

Rand wraps UnityEngine.Random with gameplay-oriented helpers. MathUtils is a partial class split across angle, trigonometry, and curve files.


MathConstants

MathConstants.cs, shared float constants safer than scattering magic numbers.

ConstantValue / role
Pi, PiHalf, Pi2, TauCircle constants
E, GoldenRatioCommon math constants
Deg2Rad, Rad2DegAngle unit conversion
OneThird, TwoThirds, OneSixthFraction literals
EpsilonUnity-safe epsilon (prefer over float.Epsilon)
Infinity, NegativeInfinityInfinities
NaNVector2/3/4, InfinityVector2/3/4Sentinel vectors
EmptyRayDefault Ray
using FronkonGames.GameWork.Foundation;

float radians = 90.0f * MathConstants.Deg2Rad;
bool equal = Mathf.Abs(a - b) < MathConstants.Epsilon;

Rand

Rand.cs, random numbers, directions, dice rolls, weighted picks, and probability checks.

Scalars and dice

MemberDescription
ValueFloat in [0, 1]
Sign / Direction1D+1 or -1 at 50%
Range(int/float min, max)Inclusive random range
D4, D6, D10, D20, D100Tabletop-style die rolls

Vectors and orientation

MemberDescription
OnUnitCircle / Direction2DRandom unit vector on XY circle
InUnitCircle, InUnitSquareRandom point in 2D unit shapes
OnUnitSphere / Direction3DRandom unit 3D direction
InUnitSphere, InUnitCubeRandom point in 3D unit shapes
AngleRandom radians in [0, Tau]
RotationUniform random Quaternion

Gameplay helpers

MethodDescription
PickWeighted(weights)Weighted index for loot tables, AI, spawns
NextNormal(mean, stdDev)Normally distributed sample (Box-Muller)
Chance(n, d)true when random in [0, d]n
ChancePercent(%)Percentage-based roll (0–100)
Lerp(a, b)Random interpolation for float, Vector2/3, Color
using FronkonGames.GameWork.Foundation;

int lootIndex = Rand.PickWeighted(new[] { 50.0f, 30.0f, 15.0f, 5.0f });

if (Rand.ChancePercent(25.0f) == true)
  ApplyCriticalHit();

Vector3 scatter = origin + Rand.OnUnitSphere * radius;
float jitter = Rand.NextNormal(mean: 100.0f, stdDev: 10.0f);

MathUtils

Partial static class across three files.

Angles, MathUtils.Angle.cs

MethodDescription
AngToDir(radian)Convert angle to unit Vector2 on XY plane
ClampAngle(angle, min, max)Clamp degrees with 360° wrap-around
ClampAngleNormalized(angle, min, max)Normalize then clamp within a degree range
float yaw = MathUtils.ClampAngle(cameraYaw, -80.0f, 80.0f);

Vector2 aim = MathUtils.AngToDir(Rand.Angle);

Trigonometry, MathUtils.Trigonometry.cs

Thin Mathf wrappers plus reciprocal and historic trig helpers.

StandardExtra
Sin, Cos, TanCsc, Sec, Cot
Asin, Acos, Atan, Atan2Ver, Cvs, Crd

Curves, MathUtils.Curve.cs

MethodDescription
Bezier(start, end, t, controlPoints)De Casteljau evaluation for Vector2 or Vector3

With no control points, behaves as linear Lerp. Used by GizmoDraw for curve visualization.

Vector3[] handles = { midPoint + Vector3.up * 2.0f };
Vector3 point = MathUtils.Bezier(start, end, t: 0.5f, handles);

Source files

FileType
MathConstants.csConstants
Rand.csRandom utilities
MathUtils.Angle.csMathUtils, angles
MathUtils.Trigonometry.csMathUtils, trig
MathUtils.Curve.csMathUtils, Bezier curves

  • EnumExtensions, PickRandom, PickWeighted on enums (uses Rand internally)
  • GizmoDraw, WireBezier / WireBezier2D curve drawing

Tests