Skip to main content

Overview

Particle Simulator injects the entire JavaScript Math library into your formulas, giving you access to a comprehensive set of mathematical functions and constants.
All functions from JavaScript’s Math object are automatically available - no prefixes or imports needed!

Variables

VariableTypeDescription
tnumberElapsed time in seconds since simulation start
xnumberCurrent X position of the particle
ynumberCurrent Y position of the particle
znumberCurrent Z position of the particle

Trigonometric Functions

Standard trigonometric functions (angles in radians):
// Sine wave
sin(t)           // Range: [-1, 1]

// Cosine wave
cos(t)           // Range: [-1, 1]

// Circular motion
10 * cos(t)      // X component
10 * sin(t)      // Y component

// Phase-shifted oscillation
sin(t * 0.5)     // Slower frequency
sin(t + PI/4)    // 45° phase shift

Available Functions:

  • sin(x) - Sine of x (x in radians)
  • cos(x) - Cosine of x
  • tan(x) - Tangent of x
  • asin(x) - Arcsine (inverse sine)
  • acos(x) - Arccosine (inverse cosine)
  • atan(x) - Arctangent (inverse tangent)
  • atan2(y, x) - Two-argument arctangent (returns angle in correct quadrant)

Example Use Cases:

Oscillations

sin(t * 0.5) * 5
Slow oscillation with amplitude 5

Circular Paths

Fx: cos(t) * 10
Fy: sin(t) * 10
Circular motion, radius 10

Angular Force

atan2(y, x)
Force proportional to angle from origin

Lissajous Patterns

Fx: -x
Fy: -2.25 * y
Frequency ratio creates complex paths

Hyperbolic Functions

Hyperbolic trigonometric functions (useful for damping and bounded growth):
// Hyperbolic tangent
tanh(x)                    // Range: (-1, 1)
                          // Smooth bounded function

// Soft clipping
20 * tanh(sin(t))         // Bounded oscillation

// Sigmoid-like behavior
tanh(t - 5)               // Smooth step at t=5

Available Functions:

  • sinh(x) - Hyperbolic sine: (e^x - e^(-x)) / 2
  • cosh(x) - Hyperbolic cosine: (e^x + e^(-x)) / 2
  • tanh(x) - Hyperbolic tangent: sinh(x) / cosh(x)
  • asinh(x) - Inverse hyperbolic sine
  • acosh(x) - Inverse hyperbolic cosine
  • atanh(x) - Inverse hyperbolic tangent

Why Use Hyperbolic Functions?

tanh(x) provides smooth bounded output:
// Instead of hard clipping:
min(max(value, -1), 1)  // Sharp corners

// Use tanh for smooth saturation:
tanh(value)             // Smooth curve
Perfect for:
  • Limiting forces without discontinuities
  • Soft transitions
  • Sigmoid activation

Exponential and Logarithmic

Exponential growth/decay and logarithmic functions:
// Natural exponential
exp(x)                    // e^x

// Exponential decay
exp(-t * 0.5)            // Decay with half-life

// Gaussian-like
exp(-x^2 - y^2)          // Radial decay from origin

Available Functions:

  • exp(x) - Euler’s number raised to x: e^x
  • log(x) - Natural logarithm (base e)
  • log10(x) - Base-10 logarithm
  • log2(x) - Base-2 logarithm
  • sqrt(x) - Square root: √x
  • cbrt(x) - Cube root: ∛x
  • pow(x, y) - x raised to power y (can also use x^y or x**y)

Operators

Arithmetic Operators

OperatorDescriptionExample
+Additionx + 5
-Subtractiony - 10
*Multiplicationx * 2.5
/Divisionx / (y + 1)
^ or **Exponentiationx^2 or x**2
Power Operator: Both ^ and ** work for exponentiation. The system automatically converts ^ to **.

Comparison & Logic

While primarily for events, these can create conditional behaviors:
OperatorDescriptionExample
>Greater thanUsed in event conditions
<Less thanUsed in event conditions
>=Greater or equalUsed in event conditions
<=Less or equalUsed in event conditions
==Equal toUsed in event conditions
!=Not equalUsed in event conditions

Utility Functions

Practical functions for common operations:
// Absolute value
abs(x)                   // |x|

// Always positive force
abs(cos(t*0.2))         // Pulsing without negative

// Sign function
sign(x)                  // -1, 0, or 1
sign(x) * abs(x)^2      // Preserve sign in power

Available Functions:

  • abs(x) - Absolute value: |x|
  • sign(x) - Sign of x: -1, 0, or 1
  • floor(x) - Round down to nearest integer
  • ceil(x) - Round up to nearest integer
  • round(x) - Round to nearest integer
  • trunc(x) - Remove fractional part
  • min(a, b, ...) - Smallest value
  • max(a, b, ...) - Largest value
  • random() - Random value in [0, 1)

Practical Examples:

Pulsing Force

-x * abs(cos(t*0.2))
Spring force that pulses between 0 and full strength

One-Way Force

max(0, -z)
Only applies when z is positive (acts like a floor)

Stepwise Force

floor(t) * 5
Changes in discrete steps

Random Noise

(random() - 0.5) * 2
Random force in [-1, 1]

Mathematical Constants

Predefined constants from JavaScript Math:
ConstantValueDescription
PI3.141592653589793Ratio of circle circumference to diameter (π)
E2.718281828459045Base of natural logarithms (e)
SQRT21.4142135623730951Square root of 2 (√2)
SQRT1_20.7071067811865476Square root of 1/2 (1/√2)
LN20.6931471805599453Natural logarithm of 2
LN102.302585092994046Natural logarithm of 10
LOG2E1.4426950408889634Base-2 logarithm of e
LOG10E0.4342944819032518Base-10 logarithm of e
// Circular motion period
sin(2 * PI * t)         // Period = 1 second

// Exponential decay
exp(-t / E)             // Characteristic time = e

// 45-degree angle
PI / 4                  // Radians

// Convert degrees to radians
angle_deg * PI / 180

Advanced Techniques

Composition

Combine functions for complex behaviors:
// Modulated oscillation
sin(t) * cos(t * 0.1)

// Exponentially decaying oscillation
exp(-t * 0.2) * sin(t * 3)

// Logarithmic spiral
log(t + 1) * cos(t)
log(t + 1) * sin(t)

// Bounded chaotic-like
tanh(sin(t) * cos(t * 1.7) * 3)

Avoiding Singularities

Be careful with division by zero and invalid inputs!
// BAD: Can divide by zero
x / sqrt(x^2 + y^2)

// GOOD: Add epsilon
x / sqrt(x^2 + y^2 + 0.001)

// BAD: log(0) is undefined
log(t)

// GOOD: Offset to avoid zero
log(t + 1)

// BAD: Negative square root
sqrt(x)

// GOOD: Use absolute value
sqrt(abs(x))

Performance Optimization

The formula cache system means complex formulas are only compiled once. Don’t be afraid to use sophisticated expressions!
// This is fine - compiled once:
-x / (sqrt(x^2 + y^2 + z^2 + 0.1)^3) + 
 sin(t * 0.5) * tanh(sqrt(x^2 + y^2) / 10)

Function Categories Summary

Trigonometric

sin, cos, tan, asin, acos, atan, atan2

Hyperbolic

sinh, cosh, tanh, asinh, acosh, atanh

Exponential

exp, log, log10, log2, pow

Power & Root

sqrt, cbrt, ^, **

Rounding

floor, ceil, round, trunc

Utility

abs, sign, min, max, random

Next Steps

Now that you know all available functions, explore:

Trajectory Examples

See these functions in action with real trajectory patterns

Force Equations

Learn how to structure your force formulas

Build docs developers (and LLMs) love