Overview
The Particle Simulator uses a powerful formula evaluation engine that supports mathematical expressions with variables, functions, and constants. Formulas can be used to define forces in dynamics mode or position functions in kinematics mode.Available Variables
Your formulas have access to four key variables that represent the particle’s state:| Variable | Description | Type |
|---|---|---|
t | Elapsed time in seconds | Number |
x | Current X-axis position of the particle | Number |
y | Current Y-axis position of the particle | Number |
z | Current Z-axis position of the particle | Number |
In Kinematics mode, formulas represent position components as functions of time:
f(t).In Dynamics mode, formulas represent force components that can depend on time, position, or both: F(t, x, y, z).Mathematical Functions
All functions from JavaScript’sMath object are available in your formulas:
Trigonometric Functions
| Function | Description | Example |
|---|---|---|
sin(n) | Sine of n (radians) | 10 * sin(t) |
cos(n) | Cosine of n (radians) | 5 * cos(2*t) |
tan(n) | Tangent of n (radians) | tan(t/3) |
atan2(y, x) | Arctangent of y/x (in radians) | atan2(y, x) |
Hyperbolic Functions
| Function | Description | Example |
|---|---|---|
sinh(n) | Hyperbolic sine | sinh(t) |
cosh(n) | Hyperbolic cosine | cosh(t) |
tanh(n) | Hyperbolic tangent | 20 * tanh(sin(t)) |
Power and Logarithmic Functions
| Function | Description | Example |
|---|---|---|
sqrt(n) | Square root | sqrt(x^2 + y^2 + z^2) |
log(n) | Natural logarithm (base e) | log(t + 1) |
log10(n) | Base-10 logarithm | log10(abs(x)) |
Utility Functions
| Function | Description | Example |
|---|---|---|
abs(n) | Absolute value | abs(x) |
floor(n) | Round down to nearest integer | floor(t) |
ceil(n) | Round up to nearest integer | ceil(t) |
round(n) | Round to nearest integer | round(t) |
sign(n) | Sign of number (-1, 0, or 1) | sign(x) |
Advanced Functions
| Function | Description | Example |
|---|---|---|
min(a, b) | Minimum of two values | min(x, y) |
max(a, b) | Maximum of two values | max(x, 0) |
random() | Random number between 0 and 1 | random() * 10 |
Mathematical Constants
These mathematical constants are available directly in your formulas:| Constant | Description | Value (Approx) |
|---|---|---|
PI | Ratio of circle circumference to diameter | 3.14159 |
E | Base of natural logarithms (Euler’s number) | 2.71828 |
SQRT2 | Square root of 2 | 1.41421 |
Operators
Supported mathematical operators:| Operator | Description | Example |
|---|---|---|
+ | Addition | x + 5 |
- | Subtraction | 10 - t |
* | Multiplication | 2 * x |
/ | Division | x / 2 |
^ or ** | Exponentiation | x^2 or x**2 |
Formula Caching Mechanism
The simulator implements an advanced caching system for optimal performance:Movimiento.ts
How It Works
- First Evaluation: When a formula is first encountered, it’s converted to a JavaScript function and cached
- Function Injection: All
Mathobject functions and constants are automatically injected into the formula scope - Subsequent Evaluations: Cached functions are reused, avoiding expensive re-parsing
- Case Insensitive: Formulas are normalized to lowercase for cache lookup
- Error Handling: Invalid formulas return 0 instead of throwing errors
This caching mechanism allows the simulator to maintain high performance even with 100+ particles running complex formulas in real-time.
Examples of Valid Formulas
Kinematics Mode Examples
In kinematics mode, define position as a function of time:Dynamics Mode Examples
In dynamics mode, define forces that can depend on position and time:Best Practices
Performance Tips
- Keep formulas simple: Complex nested functions may impact performance with many particles
- Avoid random(): Using
random()in force formulas prevents caching benefits - Reuse similar formulas: The cache works best when multiple particles use identical formulas
Common Pitfalls
Division by zero errors
Division by zero errors
Always add a small epsilon when dividing by position-dependent values:
Bad
Good
Missing parentheses
Missing parentheses
Be explicit with operator precedence:
Ambiguous
Clear
Using degrees instead of radians
Using degrees instead of radians
All trigonometric functions expect radians:
Wrong (treating as degrees)
Correct (convert to radians)
Testing Your Formulas
To verify your formulas work correctly:- Start with a single particle
- Set
deltaTto a small value (0.001 - 0.01) - Enable Force: INDIVIDUAL mode to visualize force vectors
- Enable the Trail to see the particle’s path
- Check the Info Panel for position and velocity values
Related Topics
Particle Editor
Learn how to add and edit forces in the editor interface
Events System
Trigger actions based on formula-driven conditions