Math Library
The math entity is a built-in standard library. Import it once and call any function with emit math.functionName(args) -> result.
import "math"
entity game {
has hp: 75
has max_hp: 100
event init {
emit math.clamp(hp, 0, max_hp) -> hp
emit math.sqrt(16) -> root
emit log(root) # 4.0
}
}
Usage Pattern
All math functions are called via sync emit on the math entity. Results are written to a local variable or field:
emit math.abs(-5) -> value # value = 5.0
emit math.lerp(0, 100, 0.5) -> x # x = 50.0
emit math.sin(math.PI) -> s # s ≈ 0.0
Functions that return multiple values write to math.out_x, math.out_y, math.out_z:
Constants
math.PI # 3.1415926535
math.TAU # 6.2831853071 (2 * PI)
math.E # 2.7182818284
math.EPSILON # 0.000001
math.INFINITY # 1000000000.0
Basic Arithmetic
abs
Returns the absolute value of a number.
min
Returns the smaller of two values.
max
Returns the larger of two values.
clamp
Clamps a value between a minimum and maximum.
emit math.clamp(150, 0, 100) -> result # 100.0
emit math.clamp(-5, 0, 100) -> result # 0.0
emit math.clamp(50, 0, 100) -> result # 50.0
sign
Returns 1.0 for positive, -1.0 for negative, 0.0 for zero.
emit math.sign(5) -> result # 1.0
emit math.sign(-3) -> result # -1.0
emit math.sign(0) -> result # 0.0
Rounding
floor
Rounds down to the nearest integer.
ceil
Rounds up to the nearest integer.
round
Rounds to the nearest integer (0.5 rounds up).
snap
Snaps a value to the nearest grid step.
to_fixed
Rounds to a specific number of decimal places.
Interpolation
lerp
Linear interpolation between a and b by factor t. t=0 returns a, t=1 returns b.
lerp_clamped
Same as lerp but t is clamped to [0, 1] — no extrapolation outside the range.
lerp_2d
Interpolates two 2D vectors. Result in math.out_x, math.out_y.
lerp_3d
Interpolates two 3D vectors. Result in math.out_x, math.out_y, math.out_z.
emit math.lerp_3d(0, 0, 0, 10, 10, 10, 0.5)
math.out_x -> px # 5.0
math.out_y -> py # 5.0
math.out_z -> pz # 5.0
inverse_lerp
Finds t such that lerp(a, b, t) == value.
smooth_step
Smooth hermite interpolation — eases in and out.
slerp_angle
Spherical linear interpolation for angles — constant angular velocity.
catmull_rom
Catmull-Rom spline through 4 control points (1D).
catmull_rom_2d
Catmull-Rom spline for 2D paths. Result in math.out_x, math.out_y.
spring
Physical spring towards a target with momentum. Pass out_velocity back each frame.
emit math.spring(current, target, velocity, stiffness, damping, dt) -> current
math.out_velocity -> velocity
Normalization & Mapping
normalize
Maps a value from [min, max] to [0, 1].
remap
Maps a value from one range to another.
move_towards
Moves current towards target by at most max_delta.
move_towards_angle
Moves an angle towards a target by at most max_delta, taking the shortest path.
Distance & Magnitude
distance_1d
Absolute distance between two values on a line.
distance_2d
Euclidean distance between two 2D points.
distance_3d
Euclidean distance between two 3D points.
magnitude_2d
Length of a 2D vector.
magnitude_3d
Length of a 3D vector.
Power & Roots
sqrt
Square root using Newton's method (10 iterations).
inv_sqrt
Inverse square root: 1 / sqrt(x).
fast_inv_sqrt
Fast inverse square root approximation — one Newton iteration. Use for performance-critical normalization.
pow
Power function. Supports integer and fractional exponents.
emit math.pow(2, 3) -> result # 8.0
emit math.pow(10, -2) -> result # 0.01
emit math.pow(2, 0.5) -> result # ≈ 1.414
Trigonometry
All angles are in radians unless the function name contains _angle or _deg.
sin
cos
tan
asin, acos, atan
Inverse trig functions. Input clamped to [-1, 1] for asin/acos.
emit math.asin(1) -> result # ≈ 1.5708 (PI/2)
emit math.acos(0) -> result # ≈ 1.5708 (PI/2)
emit math.atan(1) -> result # ≈ 0.785 (PI/4)
atan2
Angle in radians from origin to point (x, y). Essential for "look at" direction.
deg_to_rad / rad_to_deg
Logarithm & Exponential
exp_approx
Approximates e^x via Taylor series (10 terms). Accurate for x in [-5, 5].
ln_approx
Approximates natural logarithm ln(x) for x > 0.
log10
Base-10 logarithm.
log_base
Logarithm of any base.
Angle Operations
delta_angle
Shortest rotation between two angles in degrees. Returns value in [-180, 180].
emit math.delta_angle(350, 10) -> result # 20.0 (not 340)
emit math.delta_angle(10, 350) -> result # -20.0
repeat
Wraps a value between 0 and length (modulo).
ping_pong
Value bounces back and forth between 0 and length.
clamp_angle
Clamps an angle (degrees) between min and max, respecting 360° wrap.
Vector Operations
normalize_2d
Normalizes a 2D vector to magnitude 1. Result in math.out_x, math.out_y.
normalize_3d
Normalizes a 3D vector. Result in math.out_x, math.out_y, math.out_z.
dot_2d
Dot product of two 2D vectors. Positive = same direction, 0 = perpendicular, negative = opposite.
dot_3d
Dot product of two 3D vectors.
cross_product_2d
2D cross product (determinant). Positive = B is left of A, negative = right.
cross_product_3d
3D cross product. Result in math.out_x, math.out_y, math.out_z.
emit math.cross_product_3d(1, 0, 0, 0, 1, 0)
math.out_x -> cx # 0.0
math.out_y -> cy # 0.0
math.out_z -> cz # 1.0
project_2d
Projects vector A onto vector B. Result in math.out_x, math.out_y.
reflect_2d
Reflects a vector off a surface normal. Result in math.out_x, math.out_y.
# Bullet bouncing off a horizontal floor (normal = 0, 1)
emit math.reflect_2d(1, -1, 0, 1)
math.out_x -> rx # 1.0
math.out_y -> ry # 1.0
rotate_vector_2d
Rotates a direction vector by an angle in radians. Result in math.out_x, math.out_y.
emit math.deg_to_rad(90) -> angle_rad
emit math.rotate_vector_2d(1, 0, angle_rad)
math.out_x -> rx # ≈ 0.0
math.out_y -> ry # ≈ 1.0
rotate_point
Rotates a point around a center. Result in math.out_x, math.out_y.
get_perpendicular_2d
Returns a vector 90° counter-clockwise. Result in math.out_x, math.out_y.
smooth_damp
Dampened spring motion. Read math.out_velocity and pass it back next frame.
event tick(dt) {
emit math.smooth_damp(pos_x, target_x, vel_x, smooth_time, dt) -> pos_x
math.out_velocity -> vel_x
}
Geometry & Collision
point_in_rect
Returns 1.0 if point (px, py) is inside rectangle at (rx, ry) with size (rw, rh).
emit math.point_in_rect(5, 5, 0, 0, 10, 10) -> result # 1.0
emit math.point_in_rect(15, 5, 0, 0, 10, 10) -> result # 0.0
circles_intersect
Returns 1.0 if two circles overlap.
rects_intersect
AABB rectangle overlap check.
lines_intersect
Returns 1.0 if two line segments intersect.
line_intersects_circle
Returns 1.0 if a line segment hits a circle.
point_in_triangle
Returns 1.0 if point is inside triangle (barycentric coordinates).
triangle_signed_area
Signed area of a triangle. Positive = counter-clockwise vertices.
distance_point_to_line
Shortest distance from a point to a line segment.
Raycasting & AI
ray_intersects_circle
Returns distance to hit point, or -1.0 on miss.
is_in_cone
Returns 1.0 if point is within a vision cone.
# Is target at (5, 0) visible from origin, facing right, 90 deg fov, range 10?
emit math.is_in_cone(5, 0, 90, 10, 0, 0, 1, 0) -> result # 1.0
look_at_angle
Returns the rotation in degrees to face a target point.
emit math.look_at_angle(0, 0, 1, 0) -> result # 0.0
emit math.look_at_angle(0, 0, 0, 1) -> result # 90.0
Wave Shapes
triangle_wave
Linear triangle wave between 0 and 1.
square_wave
Returns 1.0 or -1.0. Use for blinking, pulses, or digital logic.
Easing Functions
All easing functions take t in [0, 1] and return a remapped value.
| Function | Description |
|---|---|
ease_in_quad(t) |
Slow start, accelerating |
ease_out_quad(t) |
Fast start, decelerating |
ease_in_out_quad(t) |
Slow start and end |
ease_in_cubic(t) |
Strong slow start |
ease_out_cubic(t) |
Strong deceleration |
ease_in_out_cubic(t) |
Very smooth |
ease_out_back(t) |
Overshoots then settles |
ease_in_back(t) |
Pulls back before moving |
ease_in_out_back(t) |
Both ends |
ease_out_bounce(t) |
Bouncing landing |
ease_in_bounce(t) |
Bouncing start |
ease_in_out_bounce(t) |
Bounce both ends |
entity ui {
has t: 0.0
event tick(dt) {
t + dt / 1000 -> t
emit math.ease_out_back(t) -> eased
emit unity("SetScale", eased)
}
}
Statistics & Ratio
average_2 / average_3 / average_4
emit math.average_2(10, 20) -> result # 15.0
emit math.average_3(10, 20, 30) -> result # 20.0
emit math.average_4(10, 20, 30, 40) -> result # 25.0
percentage / percent_of / ratio
emit math.percentage(25, 100) -> result # 25.0
emit math.percent_of(50, 200) -> result # 100.0
emit math.ratio(3, 4) -> result # 0.75
in_range / in_range_exclusive
emit math.in_range(5, 0, 10) -> result # 1.0
emit math.in_range_exclusive(10, 0, 10) -> result # 0.0
approximately / approximately_default
emit math.approximately(0.1 + 0.2, 0.3, 0.0001) -> result # 1.0
emit math.approximately_default(0.1 + 0.2, 0.3) -> result # 1.0
Matrix Operations
3x3 Matrices
Results write to math.out_m00 through math.out_m22 (row-major).
emit math.matrix_identity()
emit math.matrix_translation(10, 5)
emit math.matrix_rotation(math.PI / 4)
emit math.matrix_scaling(2, 2)
matrix_multiply_point
Transforms a 2D point by a 3x3 matrix. Result in math.out_x, math.out_y.
emit math.matrix_translation(10, 5)
emit math.matrix_multiply_point(0, 0, math.out_m00, math.out_m01, math.out_m02, math.out_m10, math.out_m11, math.out_m12)
math.out_x -> px # 10.0
math.out_y -> py # 5.0
4x4 Projection
# fov_rad, aspect, near, far
emit math.matrix_perspective(1.047, 1.777, 0.1, 1000)
# Result in math.out_m00 through math.out_m33
Noise & Procedural
simple_noise
Pseudo-random value in [0, 1] from a seed.
noise_2d
2D pseudo-random noise.
fractal_noise
Fractal Brownian Motion (3 octaves) for organic-looking randomness.
Random
Warning
Not cryptographically secure. Use for game variance only.
set_random_seed
Seed the RNG — call with frame delta time for variance each frame.
random
Returns a value in [0, 1].
random_range
Returns a value between min and max.
Smoothing
low_pass_filter
Smooths jittery input (mouse, joystick). factor 0.1 = very smooth, 0.9 = very responsive.
Physics
gravity_force
Newton's universal gravitation.
air_resistance
Drag force opposing velocity.
resolve_collision
Elastic collision — returns new velocity after impact. e is restitution (0–1).
pid_update
PID controller for smooth stabilization.
AI / Neural Activations
sigmoid
Squashes any value to (0, 1). Standard for probability outputs.
relu
Returns x if positive, else 0. Core of modern neural networks.
tanh
Squashes any value to (-1, 1).
Bezier Curves
get_bezier_quadratic_2d
Quadratic bezier — one control point. Result in math.out_x, math.out_y.
get_bezier_cubic_2d
Cubic bezier — two control points. Result in math.out_x, math.out_y.
Full Example
import "math"
entity player {
has x: 0.0
has y: 0.0
has target_x: 100.0
has target_y: 100.0
has vel_x: 0.0
has vel_y: 0.0
has hp: 100.0
has max_hp: 100.0
event tick(dt) {
# Smooth follow with spring damping
emit math.smooth_damp(x, target_x, vel_x, 0.2, dt) -> x
math.out_velocity -> vel_x
emit math.smooth_damp(y, target_y, vel_y, 0.2, dt) -> y
math.out_velocity -> vel_y
# Clamp HP just in case
emit math.clamp(hp, 0, max_hp) -> hp
}
event damage(amount) {
emit math.max(0, hp - amount) -> hp
check hp <= 0: emit die
}
event look_at(tx, ty) {
emit math.look_at_angle(x, y, tx, ty) -> angle
emit unity("SetRotation", angle)
}
}