Simulating Forces: Gravity and Wind in p5.js | The Nature of Code

Added:

Procedural and Noise-Driven Motion

While Newtonian force simulation (calculating acceleration, velocity, and location) is the standard for realistic physics, many creative coders argue that it is often unnecessary and overly restrictive for artistic expression. An alternative approach relies on kinematic motion, trigonometric functions, and Perlin noise (e.g., Vector Flow Fields). Instead of simulating physical laws like gravity or wind using mass and force vectors, artists can directly manipulate positions to create organic, complex behaviors. This procedural approach offers precise aesthetic control, avoids the numerical instability of Euler integration, and is often computationally cheaper, demonstrating that believable or beautiful motion does not require strict adherence to real-world physics.

Basic familiarity with JavaScript syntax and the p5.js structure, specifically how the setup() and draw() loops function.
Understanding Euclidean vectors (addition, subtraction, and scaling) and how they represent position and velocity in 2D space.
Conceptual knowledge of Newton's Laws of Motion, especially the relationship between force, mass, and acceleration (F = ma).
Basic Object-Oriented Programming (OOP) in JavaScript, including how to define classes, constructors, and methods to manage state.
Integrating mass into the simulation to observe how objects of different weights react uniquely to the same forces (A = F / M).
Simulating realistic environmental resistance, such as friction and fluid drag (air or water resistance), which depend on velocity.
Creating mutual gravitational attraction models, where multiple independent objects exert gravitational pull on each other (N-body simulations).
Transitioning to autonomous agents and steering behaviors to simulate lifelike decision-making and flocking patterns (e.g., Craig Reynolds' Boids algorithm).
106.3K views2.7Klikes24:11@TheCodingTrainOriginal Release: 2020-03-27

This video demonstrates how to simulate forces in p5.js by applying Newton's Laws of Motion: Newton's First Law states that objects maintain constant velocity unless acted upon by an unbalanced force, while Newton's Second Law (F=ma) relates force, mass, and acceleration; the tutorial shows how to implement these principles by creating a Mover object that accumulates multiple forces (like gravity and wind) through Euler integration, where acceleration is calculated as force divided by mass, and then applied to update velocity and position over time.