A work in progress interactive Physics Engine, using Macroquad to handle the graphics and egui for the UI. Especially focused on basics of soft-body mechanics.
You can generate new soft-bodies
And change the geomtery of the environment
You can visit the hosted version or cargo run
to run locally (cargo required ofc).
I went with a very basic spring-mass model, with a traditional spring connection pattern with triangles to mantain the shape of the structure.
The springs follow Hooks low with damping, so we can get the total force exerted by a single spring with:
- the stiffness factor of the spring (
$k_s$ ) - the damping factor of the spring (
$k_d$ ) - the resting length of the spring (
$L_0$ ) - the positions (
$A$ ,$B$ ) and velocities ($v_A$ ,$v_B$ ) of the masses at the edges of the spring$$F_{tot} = k_s \cdot(|B-A| - L_0) + k_d \cdot\left(\frac{B-A}{|B-A|}\right)\cdot(v_b - v_a)$$
Of the many existing iterative methods to calculate the approximate solution of the motion integration, I went with the classic Runge-Kutta method, because of its high accuracy, which is critical for the fight against self collision and tunnelling.
Given the general definition of the Runge-Kutta method of order
we can evaluate the fourth order and find the coefficients of the sum as:
Every slope of the integration (
So the final formula will be:
Or, even better:
We'll iteratevely run this calculation with the motion obtained by the spring force represented as the function
- Prevent self collision
- Add friction between masses and polygons
- Add bend and shear mechanics