Overview
The force system allows you to define custom forces applied to particles using mathematical formulas. Forces can be constant, time-dependent, position-dependent, or any combination, enabling complex physical behaviors.Force Interface
Fields
Unique identifier for the force. Used to distinguish between multiple forces on the same particle.
Force vector as an array of three formula strings
[Fx, Fy, Fz] representing the force components in x, y, and z directions.Each formula is evaluated at runtime and can reference:t- elapsed simulation timex,y,z- current particle position- All
Mathfunctions (sin, cos, tan, abs, sqrt, exp, log, etc.) - Mathematical operators (+, -, *, /, ^, **)
Formula Evaluation
Available Variables
Elapsed simulation time in seconds. Increases by
deltaT each step.Current x-position of the particle.
Current y-position of the particle.
Current z-position of the particle.
Supported Math Functions
All JavaScriptMath functions are available:
- Trigonometric:
sin,cos,tan,asin,acos,atan,atan2 - Hyperbolic:
sinh,cosh,tanh,asinh,acosh,atanh - Exponential:
exp,log,log10,log2 - Power:
pow,sqrt,cbrt - Rounding:
floor,ceil,round,trunc - Other:
abs,sign,min,max,random - Constants:
PI,E
Operators
- Addition:
+ - Subtraction:
- - Multiplication:
* - Division:
/ - Exponentiation:
**or^ - Parentheses:
()
Force Combination
When multiple forces are applied to a particle, they are combined using vector addition:- Each force’s
vecformulas are evaluated using currentt,x,y,z - All x-components are summed:
F_net_x = F1_x + F2_x + ... - All y-components are summed:
F_net_y = F1_y + F2_y + ... - All z-components are summed:
F_net_z = F1_z + F2_z + ... - Net force is applied:
a = F_net / mass
Forces defined in the particle’s
forces array are combined with the legacy fx, fy, fz fields if present.Examples
Constant Force
Damping Force
Time-Varying Force
Complex Attractor Force
Gravitational Force
Drag Force
Currently, velocity variables
vx, vy, vz are not directly available in force formulas. This is a limitation of the current implementation.Use Cases
Spring Systems
Create harmonic oscillators by using position-dependent restoring forces:Central Force Fields
Simulate planetary motion or charged particle behavior:Pulsating Fields
Create time-varying force magnitudes:Vortex Motion
Create swirling trajectories:Performance Considerations
Formula Caching
Formulas are compiled and cached on first use. Subsequent evaluations reuse the compiled function, making repeated calculations efficient.Complexity
Complex formulas with many function calls may impact performance when simulating many particles. Keep formulas as simple as possible for real-time simulation.Error Handling
If a formula evaluation fails (division by zero, invalid syntax, etc.), the force component returns0 and simulation continues.
Related
- PData Interface - Complete particle structure
- Configuration Guide - Configure forces in configuration files
- Physics Update - Underlying physics calculations