Intro In computer simulations of
the physics in solid objects, there are two main types of “bodies” which act as a representation
of whatever object is being simulated. While it’s physically impossible for a rigid body to
exist in real life, it presents a simplicity that makes it convenient and optimal to use as
a representation of most hard objects. Soft bodies take care of what rigid bodies lack
the capability to represent – shapes that are elastic, can be deformed and can be compressed.
This is Inspecto, and in this video I attempt to find out exactly how soft bodies work,
and explain it to anyone else who’s also been curious about them. This video will present
the implementation of soft bodies in 2D for the sake of simplicity, but these principles can be
extended to 3D as well. Representing Motion
Let’s start off by going over the representation of motion that we’ll use in our simulated
environment. Our body will be made of a bunch of mass points, each storing its own
position, velocity, force and mass. Force is used instead of acceleration as it’s equal to
mass times acceleration, as Newton’s second law of motion describes. This means the mass
variable can be used to alter how “heavy” it is, and consequently how it behaves.
And here’s how these values will be updated every frame. First, we set the force to zero,
as force is recalculated every frame rather than accumulated across frames. Then we add
on different forces, such as gravity and forces we’ll cover later like spring force. Since the
gravity force is proportional to the mass of the object and acts more like an acceleration,
it must be multiplied by the object’s mass when it’s applied.
Next, we add the force divided by the mass, then multiplied by delta time,
to the velocity – this is a value which accumulates across frames, unlike force.
Lastly, we add velocity times delta time to the position, and the object is moved. This
simple way of updating velocity and position is known as Euler integration, and is widely
used in game development. Spring-Mass System.
To represent mass and elasticity, two of the most fundamental properties of a soft body,
there aren’t many models better suited than the spring-mass model. This is a model involving a
spring connecting two mass points, and the data that needs to be stored by the spring includes:
a reference to the two mass points that it connects, its stiffness,
its rest length and its damping factor. Hooke’s law states that the force produced by the spring
is simply equal to the difference between its current length, which is the distance between
the two points, and its resting length, and this difference is multiplied by its stiffness.
Conventionally, Hooke’s law is either written with a negative sign, to represent the force that the
spring produces, or without a negative sign, to represent the force being applied on the spring.
However this only applies to a simplified model where only the length of the spring is considered.
In our 2D world where we are concerned with the individual positions of the two mass points,
it’s more convenient to not use the negative sign. If the spring is too long and the masses
have to come closer, the force is positive, while if the spring is too short and the masses have to
move farther apart, the force is negative. The issue with such a simple spring-mass system is
that even if it’s just displaced by a little bit, it will move in simple harmonic motion and never
stop. Its energy is conserved, and there’s simply nothing to make it come to a stop.
Hence, we need to add damping, which is a force proportional to how fast the spring’s length
changes, acting in the opposite direction of the change in length – this slows the spring down,
and would be equivalent to something like air resistance in real life.
Let’s call our two mass points A and B, and here’s an easy way to figure out how fast they’re moving
towards or away from each other, given their positions and velocities. We first find the
normalized direction vector from one point to the other – let’s say B minus A, normalized. Then
we find the corresponding velocity difference, which is just B’s velocity minus A’s velocity,
not normalized. All we need to find is the dot product between these two vectors. Whenever the
points are moving away from each other, and the damp force should act in the direction facing each
other, the two vectors will point in the same direction, and that results in a positive dot
product. If they’re moving towards each other and the damp force should act in the direction away
from each other, the two vectors will face away from each other, making a negative dot product.
If the distance between them is not changing, the dot product is zero. Now we just multiply
this dot product by our damping factor, and add it to the spring force which we previously found.
We’ll call this sum between the spring force and damp force the total spring force.
To convert this total spring force into 2 dimensional movement for each mass point,
we can multiply it by the normalized direction vector to the other mass. This way, a positive
force means the mass points move towards each other, while a negative one means they move away
from each other. Collision
Let’s quickly cover collision. Collision is usually done between polygons, but the
use of mass points to constitute our soft body means we can just implement collision between a
point and a polygon, which is much simpler, and simply run it for every point in the body.
At the start of every collision detection algorithm should be rough detection step, which
can save the computer the effort of analyzing polygons which are way too far away to even be
considered. Here we can just see whether the point is within the bounding box of each polygon, using
its minimum and maximum X and Y values, and only if it’s in the bounding box we’ll continue on.
Then there’s an easy way to know if the point is within the polygon,
called raycasting. If you were to draw a line from anywhere outside the polygon to the point,
if it intersects an odd number of edges, it’s inside the polygon, and if it intersects an
even number of edges, it’s outside the polygon. This works for convex polygons, concave polygons,
even ones with holes in them. We won’t go into the details of implementation, but as a tip, using
an axis aligned, vertical or horizontal line is easier to implement than using a diagonal one.
Now, this raycasting method involves looping through every edge of the polygon to check for
an intersection. During this loop, just add an additional step that finds the closest point on
each edge to the mass point, and keep track of the closest point out of all the edges. At the end,
if the point is inside the polygon, just push it out to that aforementioned closest point,
and use the normalized push vector as a normal vector to reflect its velocity off of.
Since it’s the closest point on the edge, that normal vector will be
perpendicular to the edge anyway. Particle Spring Mass Body
Now we’ve got a spring mass system with damping, and we’ve got collision, but we still don’t have
a soft body. This next step, however, quickly brings it all together. You see, any solid,
filled in elastic body, such as a piece of rubber, a chunk of jelly and a gummy bear,
is made of many little particles – molecules, to be exact – stuck together in 3 dimensions.
So of course, it would make sense that to simulate this type of soft body, we
should use a whole bunch of particles as well. Here’s where the spring-mass system fits into our
purposes perfectly. As long as we make a bunch of masses, and connect them all with springs,
that’s basically an elastic object! We’re not concerned with the actual molecular structure of
real life objects, but just need a structure that helps our particles keep their shape.
Since a polygon with given side lengths must be a triangle for it to maintain all its angles,
we need a spring connection pattern that involves triangles. Here we’ll use boxes with crosses
inside them – they’re nice and symmetrical, and can be seen in real life structures such as
transmission towers and even the Eiffel tower. All we have to do is get all the springs to
generate their spring force, and there we go, we’ve got a working solid soft body!
In order to prevent the spring structure from collapsing into itself,
we can add self collision – give each mass point a radius, and if a mass point travels too close to
another one, simply push it away, also using this normalized push vector to reflect its velocity.
Be careful if the distance is zero though, just skip the collision step for that frame to avoid
dividing by zero, or else your simulation might break – this also goes for any step involving
division by a distance that might be zero. Limitations
This video is only meant as an introduction to the world of soft bodies. The simulation model
presented is one of the simplest and easiest to understand, but it definitely doesn’t cover every
possible feature a soft body can have, and is also not the only way you can implement a soft body.
For example, another soft body model is the pressure spring mass model,
as opposed to the particle spring mass model we covered. This is better suited for hollow
bodies which rely on air pressure to keep their shape, such as balloons and yoga balls,
so only the perimeter of the body is made with springs and masses. Every frame, the volume or
area of the body has to be calculated – a whole nother process that can be done in different
ways – which is then used to calculate a pressure force using the ideal gas law,
which is then applied to every mass point in the direction of the springs’ normal vectors.
The motion integration, in this case Euler integration, also leaves a bit more to be
desired in terms of accuracy, as there’s only so many frames a second you can run a
soft body simulation at. Only being able to update velocity and position values
60 times a second is equivalent to approximating the area under a curve using rectangles,
and with stiffness and damping values that are too high, the simulation is bound to explode.
Researchers in the field have been working on other ways of integrating motion such as Euler
implicit integration, Heun predictor-corrector integration and midpoint integration, which are
more accurate but much more complicated. Even other than that, we haven’t even covered
features like plasticity, friction and different spring network structures. It’s clear to see
that there’s so much more to learn in the world of soft body physics, but hopefully
this video has given you a general idea of what this field of computer science entails.