This project is a JavaScript-based simulation of a soft body using the Verlet integration method. The goal is to model the behavior of a soft body under various forces and constraints, making it appear and react in a realistic manner. This simulation is inspired by concepts in computational physics and demonstrates the practical application of numerical methods in simulating physical systems.
The Verlet integration method is used to solve the equations of motion. It is particularly well-suited for systems where energy conservation is crucial, like in simulations of physical bodies. The Verlet integration formula is given by:
where:
-
$x(t)$ is the position of the point at time$t$ , -
$F(t)$ is the force applied at time$t$ , -
$m$ is the mass of the point, -
$\Delta t$ is the time step.
Springs connect the points in the soft body and obey Hooke's Law:
where:
-
$k_s$ is the spring constant, -
$k_d$ is the damping constant, -
$x$ is the current length of the spring, -
$x_0$ is the rest length of the spring, -
$v$ is the velocity difference between the connected points.
The pressure inside the soft body is simulated by applying forces to the boundary points, making the body expand. The pressure
The force due to pressure on a point is proportional to the normal vector at that point and the pressure value:
where:
-
$n$ is the normal vector, -
$V$ is the volume of the soft body.
To apply pressure forces correctly, the volume of the soft body needs to be calculated. This is done using the positions of the points and the normal vectors:
where:
-
$n$ is the number of points, -
$x_i$ and$x_{i+1}$ are the positions of adjacent points, -
$n_i$ is the normal vector at point$i$ , -
$d_i$ is the distance between points$i$ and$i+1$ .
Gravity is a constant force applied to all points, pulling them downward:
where:
-
$m$ is the mass of the point, -
$g$ is the acceleration due to gravity.
Collision with the ground or other boundaries is handled by applying a restoring force when points exceed the boundaries:
where:
-
$k_{collision}$ is the collision stiffness, -
$x$ is the position of the point, -
$x_{boundary}$ is the boundary position.
-
Initialization: The simulation initializes the positions, velocities, and forces of points on the soft body. Springs are created between adjacent points to form a mesh structure.
-
Force Calculation: Forces due to gravity, spring elasticity, pressure, and collision are calculated. Gravity pulls the points downward, springs attempt to restore points to their rest positions, and pressure forces the body to expand.
-
Integration: Using Verlet integration, the new positions of points are computed based on the calculated forces. Velocities are updated by comparing the new and old positions.
-
Rendering: The points and springs are rendered on a canvas element, creating a visual representation of the soft body.
-
Simulation Loop: The above steps are repeated in a loop, creating a continuous simulation of the soft body's behavior.
Feel free to explore and modify the code to understand the underlying principles better and create your own soft body simulations!