Due Wednesday, 4/9 at 23:59:59. You must work individually.
In this assignment, we’re going to simulate cloth with Position Based Dynamics.
For linear algebra, we’ll be using a C++ library called Eigen. You’ll have to install this
in the same way you installed glm in lab 0. You’ll
need to create a new environment variable called
EIGEN3_INCLUDE_DIR
that points to the Eigen directory. Once
you have it installed, you may want to look at this quick tutorial.
Please download the assignment and go over the code.
Your main task is to complete the function
Cloth::step()
. The skeleton code has a lot of
functionalities that deal with setting up the scene and rendering the
cloth. Study the code carefully to see what is happening.
main()
h
key runs one simulation step.Scene
Scene::load()
. You should start with a 2 by 2 cloth.Particle
x
and v
represent the
current position and velocity.p
is the previous position, used by the
PBD stepper.x0
and v0
represent the
initial position and velocity, which are used for resetting the
cloth.r
is the radius used for collisions. A
cloth particle has a small radius that is noticeably larger than the
thickness of the cloth.m
is the mass of the particle.d
is the damping coefficient.fixed
indicates whether this particle
is fixed or free.Spring
p0
and p1
are references
to the two particles.L
is the rest length of the
spring.alpha
is the compliance constant of the
spring.Cloth
Cloth::Cloth()
Cloth::updatePosNor()
Cloth::draw()
Cloth::step()
spheres
) is used for collisions and
can be ignored initially.First, add some code into Cloth::Cloth()
to set up the
particles and the springs. Start with rows = cols = 2
.
x00
, x01
, x10
, and
x11
. With rows = cols = 2
, there should be
four particles.mass
argument) divided by the number of particles.alpha
parameter for all spring types.Apply the XPBD stepping scheme:
for each particle i
fi = mi * g - di * vi
vi += (h / mi) * fi
pi = xi
xi += h * vi
end
for each constraint j between x0 and x1
Cj, ∇Cj] = springConstraint(x0, x1)
[j = -Cj / (w0 * |∇Cj0|^2 + w1 * |∇Cj1|^2 + αj/h^2)
λx0 += λj * w0 * ∇Cj0
x1 += λj * w1 * ∇Cj1
end
for each particle i
vi = (1 / h) * (xi - pi)
end
Keep in mind that some particles are marked as “fixed.” The positions and velocities of these particles should not be modified.
The spring constraint between particles \(\vec{x}_0\) and \(\vec{x}_1\) is \[ C = l - L, \] where \[ l = \| \Delta \vec{x} \|, \quad \Delta \vec{x} = \vec{x}_1 - \vec{x}_0. \] The gradients are \[ \nabla C_0 = -\frac{\Delta \vec{x}}{l}, \quad \nabla C_1 = \frac{\Delta \vec{x}}{l}. \] Note that for this constraint, \(\| \nabla C_i \| = 1\) because the norm of a unit vector is one. This means that the denominator for the calculation of \(\lambda\) can be simplified.
If you uncomment line 47 in Scene::load(...)
, you’ll see
a collision sphere moving, but it won’t be affecting the cloth yet. We
need to write some collision code first.
Implement collisions in Cloth::step()
, right before the
final update for the velocities. You must check all the cloth particles
against all the collision spheres (which are also particles) in the
scene, which in this case is just a single sphere. For each particle, if
there is a collision, snap the position of the cloth particle onto the
surface of the sphere.
Try changing the compliance, damping, and time step parameters. Write a short paragraph in the README discussing the following:
Total: 100 points
Failing to follow these points may decrease your “general execution” score.
If you’re using Mac/Linux, make sure that your code compiles and runs by typing:
> mkdir build
> cd build
> cmake ..
> make
> ./A5 <SHADER DIR>
If you’re using Windows, make sure your code builds using the steps described in Lab 0.
(*.~)
(*.o)
(.vs)
(.git)
UIN.zip
(e.g.,
12345678.zip
).UIN/
(e.g. 12345678/
).src/
,
CMakeLists.txt
, etc..zip
format (not .gz
,
.7z
, .rar
, etc.).