Background
I have followed the implementation provided by Maciej Matyka in this paper to create a 2D physics simulation of a soft body.
The paper provides a minimum implementation of a pressure soft-body model using the C programming language — I have adapted this in Javascript so that it can be accessed online.
I have also used the p5.js library for handling the vector manipulations and for visualising the simulation.
How It Works
As opposed to rigid bodies (where the object's shape is fixed), the shape of soft bodies can deform and change as they interact with other bodies or when a force is applied to them.
In this simulation, the soft body is composed of many points/nodes, which are connected by invisible springs. Each node is connected by two springs (one for each adjacent node).
The simulation first calculates and accumulates the forces that are acting on each node. External forces (e.g. gravity) are processed first, followed by the internal forces of the springs. Finally, an internal pressure force is added which inflates the object like a balloon and prevents it from collapsing.
Once all forces have been accumulated, Euler Integration is used to work out the new positions of every node. This is used to update the animation.
Collision Detection and Handling
To simulate collision detection with the walls, the program regularly checks every node to see whether its position is outside the canvas.
The program checks the x and y position values separately, and if either value goes outside the canvas, the program will invert the relevant component of the node's velocity. For example, if a node's x position value is outside the canvas, the x component of the node's velocity will be inverted. This simulates an effect similar to a laser beam bouncing off a mirror.
I have also used min() and max() functions to constrain each node's position. This ensures that if a node is outside the canvas, it will be moved back within the bounds of the canvas.
Note: Checking every node is probably not the best way to implement collision detection, but I've chosen to use this simple implementation. There are other more efficient ways — For example, you could use Spatial Partitioning to reduce the number of nodes that need to be checked.
Interact With This Simulation
You can move the soft body by clicking and dragging your mouse pointer across the screen.
You can also modify the properties of the soft body by changing the parameters below.