Skip to main content

Overview

Forces in Particle Simulator are defined as mathematical functions that can depend on time (t) and position (x, y, z). The system evaluates these formulas in real-time to compute the acceleration and trajectory of each particle.

Force as a Function: F(t, x, y, z)

Every force component can be written as a mathematical expression using:
  • t - Elapsed time in seconds
  • x, y, z - Current position of the particle
  • Any mathematical function from the available functions library

Vector Force Format

Forces are always specified as three components:
[Fx, Fy, Fz]
Each component is a string containing a mathematical formula:
{
  "vec": ["-x", "-y", "-z"]
}

Time-Dependent Forces

Forces that change over time use the t variable:
{
  "vec": [
    "0",
    "0",
    "sin(t * 0.5) * 5"
  ]
}
// Oscillating force in Z direction

Position-Dependent Forces

Forces that depend on the particle’s current position:

Spring/Restoring Forces

{
  "vec": ["-x", "-y", "-z"]
}
// Hooke's law: F = -kx (k=1)
// Always points toward origin

Inverse-Square Gravity

Central Force
{
  "vec": [
    "-x / (sqrt(x^2 + y^2 + z^2)^3)",
    "-y / (sqrt(x^2 + y^2 + z^2)^3)",
    "-z / (sqrt(x^2 + y^2 + z^2)^3)"
  ]
}
This creates a gravitational force:
  • Magnitude: 1/r²
  • Direction: Always toward origin
  • Note: The denominator comes from normalizing the direction vector

Combined Forces

You can combine time and position dependencies:
{
  "vec": [
    "-x * abs(cos(t*0.2))",
    "-y * abs(cos(t*0.2))",
    "-z * 0.5"
  ]
}
// Spring constant pulses with time
// Creates expanding/contracting motion

Force Evaluation Modes

The simulator supports two calculation modes:

Dynamic Mode: F = ma

Forces are evaluated and Newton’s second law is applied:
// From Movimiento.ts evaluarFormula function
const Fx = evaluarFormula(force.vec[0], t, x, y, z);
const Fy = evaluarFormula(force.vec[1], t, x, y, z);
const Fz = evaluarFormula(force.vec[2], t, x, y, z);

// Acceleration: a = F/m
ax = Fx / mass;
ay = Fy / mass;
az = Fz / mass;
When to use:
  • Realistic physics simulations
  • When mass matters
  • Multiple interacting forces
Example:
{
  "mass": 5,
  "forces": [
    {"id": 1, "vec": ["-x", "-y", "0"]},
    {"id": 2, "vec": ["0", "0", "-9.8"]}
  ]
}

Implementation Details

The force evaluation uses function caching for performance:
// From Movimiento.ts
const formulaCache: Record<string, Function> = {};

export const evaluarFormula = (
  formula: string,
  t: number,
  x: number = 0,
  y: number = 0,
  z: number = 0
): number => {
  // Replace ^ with ** for exponentiation
  const cleanedFormula = formula.replace(/\^/g, "**");
  
  // Create function with Math context injected
  fn = new Function(
    "t", "x", "y", "z", ...mathKeys,
    `return ${cleanedFormula}`
  );
  
  // Execute with current values
  return fn(t, x, y, z, ...mathValues);
};

Key Features:

Function Caching

Formulas are compiled once and cached to avoid repeated parsing

Math Injection

All JavaScript Math functions are automatically available

Power Operators

Both ^ and ** work for exponentiation

Error Handling

Invalid formulas return 0 instead of crashing

Common Patterns

Oscillators

// Simple Harmonic: F = -kx
["-x", "-y", "-z"]

// Damped: F = -kx - bv (approximated)
["-x * 1.5", "-y * 1.5", "-z * 1.5"]

// Driven: F = -kx + F₀sin(ωt)
["-x", "-y", "-z + 10*sin(2*t)"]

Central Forces

// Attractive 1/r²
[
  "-x / (sqrt(x^2 + y^2 + z^2)^3)",
  "-y / (sqrt(x^2 + y^2 + z^2)^3)",
  "-z / (sqrt(x^2 + y^2 + z^2)^3)"
]

// Repulsive 1/r²
[
  "x / (sqrt(x^2 + y^2 + z^2)^3)",
  "y / (sqrt(x^2 + y^2 + z^2)^3)",
  "z / (sqrt(x^2 + y^2 + z^2)^3)"
]

Rotating Forces

// Vortex (rotates around Z axis)
[
  "-y",  // Fy ∝ -y creates rotation
  "x",   // Fx ∝ x creates rotation
  "-z"   // Fz ∝ -z keeps Z bounded
]

// Pulsing Vortex
[
  "-y * abs(cos(t*0.2))",
  "x * abs(cos(t*0.2))",
  "-z"
]
Pro Tip: Start with simple formulas and add complexity gradually. Use the trajectory examples as a starting point for your own creations.

Build docs developers (and LLMs) love