Abstract
The purpose of this project was to see how easy it would be to simulate cloth
and other soft bodied objects in real time, while keeping the physical calculations
behind the scenes as accurate as possible.
Implementation
Phase I: Advanced Particle System
Because geometrically in the rendering subsystem, the soft body has to be
represented as an array of vertices, the first step would be to accurately model
a system of particles. These particles would have to be able to collide with
any surface by using collision detection, be affected by gravity, drag forces,
and friction, and have a variable lifetime, with lifetime being the length of
time the particle is in existence.
Standard physics equations often depended on some time variable in order to
essentially plot the curve that corresponds to the motion of an object. One
such equation would be:
Current Position = Initial Position + Initial Velocity * Time + Acceleration
* Time Squared / Two
There are a few others for calculating the current velocity at a given point
in time and others. However through testing these equations are far to complex
to be evaluated in real time for systems of greater than approximately one thousand
particles. This overhead can be alleviated however by using a method of numerical
integration, or by calculating the equation at various critical points and using
spline interpolation to model the motion of a particle at any time interval.
If force is equal to mass * acceleration, then the derivative of velocity with
respect to time (acceleration) is equal to force divided by mass. Using Euler's
method, we can then approximate the equation for the motion of a particle over
time by doing numerical integration. Euler's Method with a few more derivatives
calculated or Runge-Kutta could greatly increase the accuracy of the integration,
(Euler's Method alone can be as much as 3% off) since this simulation has hundreds,
if not thousands of points being calculated at the same time, the visible difference
is negliable. If the velocity at time t is given to us by the equation v(t +
delta t) = v(t) + (delta t) * v'(t), and we know our initial v and v', which
is given to us in the differential equation for force, we can approximate the
velocity of the particle at any point in time as long as the velocity for that
particle is continuous down to the first derivative. (C1 continuity) This means
if the particle collides with an object, that collision will have to be set
as the initial velocity and then the simulation will continue from there, creating
a piece-wise smooth function.
An identical result that can be achieved using spline interpolation. Solve the
original equation (the first equation I started for calculating the current
position of an object) and see at which points that function is zero. Calculate
the first derivative of that equation with respect to time for the start point
and the end point, and then use a Hermite curve generate a spline curve between
those two points. Because the Hermite curve has to maintain C1 continuity as
well, the splines generated by this function need to be only computed across
continuous regions. While this method reduces the amount of calculations per
frame that need to be completed, since the series of spline curves only need
to be calculated once for the entire life of the particle, but that initial
calculation is fairly intense. It also is a fairly rigid method of calculating
motion which assumes no other forces will act upon the particle after generation.
Because of these drawbacks, my implementation opted for using numerical integration
in real time.
The only set of calculations this particle system needed to become a soft body
system would be the spring calculations that would needed to give the particle
system form.
Phase II: Collision Detection
The method in which I did collision detection for this project is fairly simple,
but very accurate. Since the time interval in which a vertex passes through
a polygon can and often times will be smaller than the time interval between
which the position of the particle will be updated, simply using the plane equation
to see if the particle exists within the plane established by the polygon won't
work. I instead need to use linear interpolation between the past position of
the particle in the previous frame and the current position of the particle
and the normal of the polygon I'm checking collisions with to see if a collision
occurred in the past.
Since the plane equation assumes an infinite plane, once a collision with the
plane generated by the polygon is confirmed, I need to check and see if that
collision occurred within the defined boundaries of that plane. (the edges of
the polygon) For that I simply compute the edge normal for each edge of the
polygon, compute a vector from the point of collision to the middle of the edge,
and take the dot product between the edge normal and the vector I just created.
If the angle between the vector I created and the edge normal is greater than
90, the point exists on the inside of the that edge. If not, then the point
exists on the outside of that edge and the collision didn't occur within the
polygon. I do this test for all edges of the polygon, stopping if the test fails
at any edge. If the point of intersection is found to be on the inside of all
edges, then it is inside the polygon and the collision did indeed occur within
the polygon.
When a collision is detected, that vertex is simply positioned outside the polygon.
While there are more complex algorithms that check how far the vertex penetrated
into the polygon and use that distance to calculate precisely where the vertex
would be had it collided with the plane in the first place, computers update
fast enough that the positional difference again, would be negliable.
Phase III: Cloth System
Building upon what was developed in phases I and II, phase three was fairly
straight forward. Making phase III fast was far more difficult. The only difference
between cloth and the systems I described earlier is the inclusion of springs
between each of the particles in the system to give the particle system a defined
shape. These springs can be simulated using Hooke's Law, which is F = Spring
Constant * (Current Length – Rest Length). A damping force can be added
for numerical consistency, which would simply be F = Damping Constant * (Velocity
1 – Velocity 2), with the velocity coming from the current velocity of
each particle. These two equations can be combined into a rather long equation,
but one that is simple to solve. Numerical integration isn't necessary for this
equation, since the components have been solved previously. One simply needs
to plug in the data to find the correct forces on each particle the spring connects.
The force on particle 2 is equal to the opposite of the force exerted on particle
1.
Care must be taken to ensure numerical stability or two things can occur. One
would be that the object enters a state of infinite oscillation and never returns
to rest. Another would be that the amplitude of the oscillation reaches infinity
and the system essentially breaks. By carefully adjusting the damping and spring
constants, one can avoid these problems easily, as long as the step size they're
using for the numerical integration isn't too large. Unfortunately though, these
limits have to be empirically derived, since there is no easy way to see how
the calculations will go before running through them.
Phase IV: External Effects and Cloth Draping
This phases is simply an exercise to make the cloth react appropriately regardless
of the situation it is placed in. Initially I attempted to model the draping
of the cloth using for lack of a better word, a drape equation, and this actually
worked quite well. However, it was a fairly expensive calculation, and ultimately
was unneeded. Since the method in which I calculated the cloth simulation was
fairly accurate physically, the cloth would naturally drape on its own, without
the need for any sort of position correction via the drape equation. Some issues
did appear however since with draping I can end up having collisions between
edges and faces instead of just vertices and planes, but by making the cloth
fairly dense these calculation errors are very difficult to see, especially
with the limits of z-buffer precision in computer hardware today.
Things such as wind and drag were easy to add to my simulation because of the
way in which I designed the way forces were calculated on the individual particles.
One just pushes forces onto an array every time it occurs and then the force
is thrown into the equations to calculate the velocity of the particle at that
point in time. Then the forces are cleared unless the force is being continually
applied.