Soft-bodies
For a high-level overview of the methods behind our soft-body implementation, see our blog-post.
Rigid-bodies can't deform in any way, which is precisely what makes them cheap and easy to control. Soft-bodies, aka. deformable bodies, are made for everything that must bend, stretch, or squash: ropes, cloth, jelly, balloons, the chassis of a car denting on impact, etc. They live in the same world as the rigid-bodies and the colliders, and interact with all the other features of the engine with little to no restriction:
- Any kind of impulse joint can be attached to a soft-body through its soft frames, which allows the definition of links between two soft-bodies, between a soft-body and a rigid-body, or between a soft-body and a multibody.
- Deformable colliders, as well as rigid ones, can be attached to a soft-body, sensors included.
- A detailed mesh can follow the deformations of a coarser simulated lattice (skinning).
- Parts of a soft-body can be controlled kinematically.
- A soft-body can deform permanently (plasticity), and tear apart (tearing).
Definition of a soft-body
A rigid-body is a single moving frame: the position and the velocity of any of its points follow from one translation and one rotation. This is precisely what a deformable body can't be, because its parts must be able to move (somewhat) independently of each other. Instead, a soft-body is made of particles, aka. mass points, i.e., points without any orientation, each having 2 degrees of freedom in 2D and 3 in 3D. The more particles a body is made of, the more detailed its deformations can be, and the more expensive it is to simulate.
The particles are kept together by a lattice made of up to three kinds of elements:
- The particles are the only mandatory element, i.e., the positions the
SoftBodyBuilderis built from. They carry the mass and the velocity of the body. - The edges (
edges) connect two particles. The structural edges resist the stretching (and the compression) of the body, whereas the bending edges (bend_edges) and the dihedral constraints (dihedrals, 3D) resist its bending. - The cells (
cells) connect three particles in 2D (triangles) and four in 3D (tetrahedra). They resist the deformation of the area (2D) or of the volume (3D) they cover.
Two more concepts describe how the body interacts with the rest of the world. The surface
(surface) is the boundary of the body (segments in 2D, triangles in 3D): this is
what the body collides with, and what the preservation of its volume integrates over. A body without any surface, e.g.,
a cloud of particles, collides with nothing at all, unless it is given wire segments
(wire), in which case it collides as a polyline, which is what a 3D rope does.
Finally, a skin (skin) is a mesh which vertices follow the cells without being
particles themselves, as detailed in the skinning section.
Every element can be given manually to the soft-body builder, but the constructors of the next section build the lattices of the most common shapes directly. Note that the elements a body is made of decide which of the cohesion models can hold it together.
Creation and insertion
A soft-body is described by a SoftBodyBuilder, which constructors build the lattice of the most common shapes:
| Constructor | Dimension | Lattice |
|---|---|---|
rope | 2D, 3D | Structural and bending edges between the particles of a line. |
cloth, cloth_anisotropic, cloth_tube | 3D | Structural, shear and bending edges, with a triangle surface. |
grid | 2D | Triangle cells filling a rectangle. |
cuboid | 3D | Tetrahedral cells filling a box. |
polygon, disk | 2D | A closed boundary preserving its area. |
sphere | 3D | A closed surface preserving its volume, with dihedral bending constraints. |
trimesh, polyline | 2D, 3D (trimesh), 2D (polyline) | The vertices and edges of a mesh, held by shape matching. |
volumetric | 2D, 3D | Cells filling a closed mesh. |
The volumetric constructor is the one to use for an arbitrary solid: it fills a closed mesh (segments in 2D, triangles
in 3D, oriented outward) with cells of about the requested size. The interior of the mesh is triangulated by Delaunay
refinement in 2D, whereas in 3D every cell of a lattice the mesh reaches is kept whole: the result contains the mesh
instead of following it exactly, and its boundary is as blocky as its cells. The meshing parameters are spelled
out by volumetric_with: the size of the cells, how much the cover is smoothed and subdivided around the boundary in
3D, i.e., how closely it follows the mesh, and whether the surface alone is covered, leaving the interior
empty.
- Example 2D
- Example 3D
// Fill a closed, counter-clockwise polyline with triangle cells of about 0.2 in size.
let vertices = vec![
Vector::new(-0.5, -0.25),
Vector::new(0.5, -0.25),
Vector::new(0.5, 0.25),
Vector::new(-0.5, 0.25),
];
let indices = vec![[0, 1], [1, 2], [2, 3], [3, 0]];
let block = SoftBodyBuilder::volumetric(&vertices, &indices, 0.2)
.expect("the polyline must be closed and enclose some area")
.translated(Vector::new(-3.0, 1.0));
let _block_handle = world.insert_soft_body(block);
// Fill a closed, outward-oriented triangle mesh with tetrahedral cells of about 0.2 in size.
let (vertices, indices) = Cuboid::new(Vector::new(0.5, 0.25, 0.25)).to_trimesh();
let block = SoftBodyBuilder::volumetric(&vertices, &indices, 0.2)
.expect("the mesh must be closed and enclose some volume")
.translated(Vector::new(-3.0, 1.0, 0.0));
let _block_handle = world.insert_soft_body(block);
The builder allows the definition of everything else that is specific to one soft-body: the particles held in place (the
pinned particles, pinned_particles), the softness of its constraints
(softness), the mass of its particles (one mass for every particle with
particle_mass, a total mass for the whole body with
mass, or one mass per particle with
masses), their radius
(particle_radius), the collider its surface is made of, and whether that
surface is allowed to collide with itself (self_contacts). The particles
can also be given a linear damping (linear_damping), a gravity scale
(gravity_scale), a dominance group
(SoftBodyParticleSettings::dominance_group), and be allowed to sleep or
not (can_sleep), exactly like a rigid-body. Inserting the
soft-body into the world (PhysicsWorld::insert_soft_body) will
automatically create the rigid-body standing for it (its root body), as well as the
colliders covering its surface:
- Example 2D
- Example 3D
// A world with a ground.
let mut world = PhysicsWorld::new();
world.insert_collider(ColliderBuilder::cuboid(10.0, 0.1), None);
// Builder for a rope of 20 particles between two points.
let _ = SoftBodyBuilder::rope(Vector::new(0.0, 3.0), Vector::new(2.0, 3.0), 20);
// Builder for a grid of `nx` by `ny` particles filled with triangle cells.
let _ = SoftBodyBuilder::grid(Vector::new(3.0, 1.0), Vector::new(1.0, 1.0), 6, 6);
// Builder for a disk: a ring of particles holding its area (a pressurized blob).
let _ = SoftBodyBuilder::disk(Vector::new(0.0, 3.0), 0.8, 24);
// Builder for a closed polygon of particles holding its area.
let _ = SoftBodyBuilder::polygon(vec![
Vector::new(5.0, 4.0),
Vector::new(7.0, 4.0),
Vector::new(7.0, 6.0),
Vector::new(5.0, 6.0),
]);
let n = 20;
let sheet = SoftBodyBuilder::grid(Vector::new(-3.0, 3.0), Vector::new(1.0, 1.0), n, n)
// Particles held in place.
.pinned_particles([0, (n - 1) as u32])
// A uniform softness (natural frequency in Hz, damping ratio) for every constraint.
.softness(SpringCoefficients::new(30.0, 1.0))
// The mass of each particle.
// Default: 1.0
.particle_mass(0.05)
// The thickness of the particles, for collisions.
// Default: 0.01
.particle_radius(0.05)
// The template of the body's colliders: its shape is replaced by the deformable surface.
.surface_collider(ColliderBuilder::ball(0.05).friction(0.8))
// Whether the body may fall asleep.
// Default: true
.can_sleep(true);
// Insert the soft body: this creates its hidden root rigid body and its colliders.
let sheet_handle = world.insert_soft_body(sheet);
// A world with a ground.
let mut world = PhysicsWorld::new();
world.insert_collider(ColliderBuilder::cuboid(10.0, 0.1, 10.0), None);
// Builder for a rope of 20 particles between two points.
let _ = SoftBodyBuilder::rope(Vector::new(0.0, 3.0, 0.0), Vector::new(2.0, 3.0, 0.0), 20);
// Builder for a cloth: `nu` by `nv` particles, particle `(i, j)` at `origin + i * du + j * dv`.
let _ = SoftBodyBuilder::cloth(
Vector::new(-1.0, 2.0, -1.0),
Vector::new(0.1, 0.0, 0.0),
Vector::new(0.0, 0.0, 0.1),
20,
20,
);
// Builder for a box of `nx * ny * nz` particles filled with tetrahedral cells.
let _ = SoftBodyBuilder::cuboid(Vector::new(3.0, 1.0, 0.0), Vector::splat(0.5), 4, 4, 4);
// Builder for a hollow sphere holding its volume (a balloon).
let _ = SoftBodyBuilder::sphere(Vector::new(0.0, 3.0, 3.0), 0.8, 2);
// Builder over raw particle positions; the elements are added by the setters.
let n = 20;
let cloth = SoftBodyBuilder::cloth(
Vector::new(-1.0, 2.0, -1.0),
Vector::new(0.1, 0.0, 0.0),
Vector::new(0.0, 0.0, 0.1),
n,
n,
)
// Particles held in place.
.pinned_particles([0, (n - 1) as u32, (n * (n - 1)) as u32, (n * n - 1) as u32])
// A uniform softness (natural frequency in Hz, damping ratio) for every constraint.
.softness(SpringCoefficients::new(30.0, 1.0))
// The mass of each particle.
// Default: 1.0
.particle_mass(0.05)
// The thickness of the particles, for collisions.
// Default: 0.01
.particle_radius(0.02)
// The template of the body's colliders: its shape is replaced by the deformable surface.
.surface_collider(ColliderBuilder::ball(0.05).friction(0.8))
// Whether the surface collides with itself.
// Default: false
.self_contacts(true)
// Whether the body may fall asleep.
// Default: true
.can_sleep(true);
// Insert the soft body: this creates its hidden root rigid body and its colliders.
let cloth_handle = world.insert_soft_body(cloth);
The collider given to SoftBodyBuilder::surface_collider is
only a template: its shape is replaced by the deformable surface of the soft-body, and its density is ignored, whereas
all its other properties are kept. Therefore this is where the friction, the collision
groups, or the active events of
the soft-body must be set. A body that should only collide through colliders of your own can be built without any
default one (no_surface_collider).
Two builders can be merged into a single soft-body with append, and their pieces
sewn together with additional edges (add_edges), which rest length is the distance
their particles have when they are added. This is, e.g., how the sleeves of a shirt are attached to its body.
Soft-body set
Like the rigid-bodies and the colliders, the soft-bodies of a simulation are stored inside of a set: the SoftBodySet.
The examples of this page use the PhysicsWorld, a façade owning all the sets of one simulation, but the sets can also
be used directly. Note that the insertion of a soft-body (SoftBodySet::insert) needs the rigid-body set and the
collider set as well, because a soft-body owns the rigid-body standing for it and the colliders of its surface:
- Example 2D
- Example 3D
// The sets can also be used directly, without the `PhysicsWorld` façade.
let mut soft_body_set = SoftBodySet::new();
let mut rigid_body_set = RigidBodySet::new();
let mut collider_set = ColliderSet::new();
let rope = SoftBodyBuilder::rope(Vector::new(0.0, 3.0), Vector::new(2.0, 3.0), 20);
let rope_handle = soft_body_set.insert(rope, &mut rigid_body_set, &mut collider_set);
let soft_body = &soft_body_set[rope_handle];
assert_eq!(soft_body.num_particles(), 20);
// The sets can also be used directly, without the `PhysicsWorld` façade.
let mut soft_body_set = SoftBodySet::new();
let mut rigid_body_set = RigidBodySet::new();
let mut collider_set = ColliderSet::new();
let rope = SoftBodyBuilder::rope(Vector::new(0.0, 3.0, 0.0), Vector::new(2.0, 3.0, 0.0), 20);
let rope_handle = soft_body_set.insert(rope, &mut rigid_body_set, &mut collider_set);
let soft_body = &soft_body_set[rope_handle];
assert_eq!(soft_body.num_particles(), 20);
Keeping the body together
Rapier supports three ways of holding the particles of a soft-body together: shape matching, constraints, and the Finite Elements Method (FEM). They can be combined, e.g., shape matching on top of edge constraints. Note that shape matching works on the particles alone, whereas the constraints need edges or cells, and the FEM solver needs cells.
Shape matching
Shape matching doesn't need any element. At each timestep, the rest shape of the body is placed where it best fits its current shape, i.e., both shapes are given the same center of mass, and the rest shape is given the rotation bringing its particles the closest to the current ones. Each particle is then pulled toward its twin in that matched rest shape by a spring:
This is cheap, and a body always recovers its original shape whatever the deformation it went through. On the other
hand, the particles don't interact with their neighbors: pushing on one particle doesn't pull the ones around it, which
makes the deformations feel very local. Shape matching is enabled by
SoftBodyBuilder::shape_matching, and the strength of its
springs is the shape matching softness (shape_matching_softness)
of the material:
- Example 2D
- Example 3D
// A cloud of particles without any element: shape matching alone pulls them back toward
// their rest shape, placed where it best fits the current one.
let points: Vec<Vector> = (0..9)
.map(|i| Vector::new((i % 3) as f32, (i / 3) as f32 + 4.0) * 0.3)
.collect();
let blob = SoftBodyBuilder::new(points)
.shape_matching(true)
.material(SoftBodyMaterial {
// How fast the particles are pulled back toward their rest shape.
shape_matching_softness: SpringCoefficients::new(5.0, 1.0),
..Default::default()
})
.particle_radius(0.1);
let _blob_handle = world.insert_soft_body(blob);
// A cloud of particles without any element: shape matching alone pulls them back toward
// their rest shape, placed where it best fits the current one.
let points: Vec<Vector> = (0..27)
.map(|i| Vector::new((i % 3) as f32, (i / 3 % 3) as f32 + 4.0, (i / 9) as f32) * 0.3)
.collect();
let blob = SoftBodyBuilder::new(points)
.shape_matching(true)
.material(SoftBodyMaterial {
// How fast the particles are pulled back toward their rest shape.
shape_matching_softness: SpringCoefficients::new(5.0, 1.0),
..Default::default()
})
.particle_radius(0.1);
let _blob_handle = world.insert_soft_body(blob);
Use shape matching for low-detail deformations, or whenever computing a topology (edges, cells) isn't desired. It is
enabled by default by the trimesh and polyline constructors. Note however that it performs very poorly for ropes,
cloth, or any open shape. Also note that combining it with edges makes the deformations spread to the neighbors to look
more realistic.
Constraints
The constraints-based soft-body solver is the default solver (SoftBodySolver::Constraints), and the most versatile one:
every edge and cell element of the deformation lattice becomes a constraint, solved together with the contacts and the joints of the scene.
An edge is a spring-damper pulling its two particles back toward its rest length. A cell is either a volume constraint
keeping its area or its volume, the shape itself being held by the edges, or an elastic element resisting any
deformation. This is selected by the cell model of the body
(cell_model):
Volume: one constraint per cell keeping its area (2D) or its volume (3D) at its rest value. This is the cheapest model, and combined with the edges it is often enough to obtain a convincing jelly.Corotational: linear elasticity expressed in the rotation-free frame of the cell. It is stable at any stiffness and recovers from inverted cells.NeoHookean: stable Neo-Hookean hyperelasticity. It feels stiffer than linear elasticity on compression, but softer on tension.
The stiffness of every element is configured by the SoftBodyMaterial of the body, which can be given to the builder or
set at any time with SoftBody::set_material. The edges and the volume constraints are given a softness, i.e., a
natural frequency (in Hz) and a damping ratio instead of a stiffness, so it doesn't depend on the masses of the
particles:
- The edge softness (
edge_softness) for the structural edges; - The bend softness (
bend_softness) for the bending edges and the dihedral constraints; - The volume softness (
volume_softness) for the volume constraints.
The elastic cells are given a Young modulus (young_modulus, in force per unit
area in 3D, per unit length in 2D), a Poisson ratio (poisson_ratio), and a
damping ratio (elastic_damping_ratio) instead. Their natural frequency
is derived from these, therefore a body meshed more finely doesn't become stiffer, whereas it becomes more expensive to
simulate.
Finally, a body with a closed surface can preserve the area (2D) or the volume (3D) it encloses
(volume_preservation), which target can be scaled by a
volume_factor
greater than 1 in order to inflate the body, e.g., to simulate a pressurized blob:
- Example 2D
- Example 3D
// Elastic cells: a jelly square with corotational linear elasticity.
let jelly = SoftBodyBuilder::grid(Vector::new(3.0, 1.2), Vector::splat(1.0), 6, 6)
// The constitutive model of the cells: `Volume` (per-cell area constraints,
// the shape is held by the edges), `Corotational` or `NeoHookean`.
.cell_model(SoftBodyCellModel::Corotational)
.material(SoftBodyMaterial {
// Stiffness of the elastic cells.
young_modulus: 3.0e3,
poisson_ratio: 0.35,
elastic_damping_ratio: 0.5,
// Plasticity: the rest shape flows past 5% strain, at 20 per second.
plastic_yield: 0.05,
plastic_creep: 20.0,
// Tearing: an element past 40% strain tears.
tear_strain: Some(0.4),
..Default::default()
})
.particle_mass(0.2);
let jelly_handle = world.insert_soft_body(jelly);
// A pressurized blob: a ring of particles inflated by area preservation.
let blob = SoftBodyBuilder::disk(Vector::new(0.0, 3.0), 0.8, 24)
.softness(SpringCoefficients::new(20.0, 1.0))
// Target area multiplier (`> 1` inflates the body); enables area preservation.
.volume_factor(1.1)
.self_contacts(true);
let blob_handle = world.insert_soft_body(blob);
// Elastic cells: a jelly cube with corotational linear elasticity.
let jelly = SoftBodyBuilder::cuboid(Vector::new(3.0, 1.0, 0.0), Vector::splat(0.5), 4, 4, 4)
// The constitutive model of the cells: `Volume` (per-cell volume constraints,
// the shape is held by the edges), `Corotational` or `NeoHookean`.
.cell_model(SoftBodyCellModel::Corotational)
.material(SoftBodyMaterial {
// Stiffness of the elastic cells.
young_modulus: 2.0e3,
poisson_ratio: 0.35,
elastic_damping_ratio: 0.5,
// Plasticity: the rest shape flows past 5% strain, at 20 per second.
plastic_yield: 0.05,
plastic_creep: 20.0,
// Tearing: an element past 40% strain tears.
tear_strain: Some(0.4),
..Default::default()
})
.particle_mass(0.2);
let jelly_handle = world.insert_soft_body(jelly);
// A material shared by the edges, bending constraints and volume constraints.
let material = SoftBodyMaterial {
// Softness of the bending constraints, on top of a uniform 30 Hz softness.
bend_softness: SpringCoefficients::new(3.0, 1.0),
..SoftBodyMaterial::uniform(SpringCoefficients::new(30.0, 1.0))
};
world.soft_bodies[cloth_handle].set_material(material);
The stiffness effectively simulated by the constraints solver depends on its convergence: with too few iterations, a stiff
body looks softer than its material says. This is why soft-bodies are configured with 3 additional internal PGS solver iterations by default, which can
be modified with
additional_pgs_iterations. The whole island a body belongs to
can also be given additional substeps with
additional_solver_iterations, just like
rigid-bodies.
The following table gathers the settings to look at for the most common problems:
| Problem | What to change |
|---|---|
| The body is too soft, or stretches too much. | Raise the natural frequency of the material's edge_softness, or its young_modulus for the elastic cells. Give it more additional_pgs_iterations, or switch it to the FEM solver with solver. |
| A cloth stretches, but should still fold easily. | Keep a stiff edge_softness, and give it a soft bend_softness. |
| A rope compresses like a spring. | Make its edges resist stretching only with tension_only. |
| The body keeps wobbling after an impact. | Raise the damping_ratio of the material's softnesses and its elastic_damping_ratio, or its deformation_damping (which damps the deformations but not the motion of the body as a whole). |
| A closed body collapses, or must be inflated. | Enable volume_preservation, and give it a volume_factor greater than 1. |
| The deformations are too local. | Combine shape_matching with edges, or rely on edges and cells alone. |
FEM solver
The FEM solver (SoftBodySolver::Fem, behind the fem cargo feature) resolves the elasticity of the whole body at once and semi-implicitly:
the forces and the stiffness of every cell are assembled into a single linear system, solved at each substep. Therefore
the stiffness of the body no longer depends on the number of solver iterations, which makes it capable of simulating
very stiff materials, as well as more realistic plastic deformations and failures:
This comes at a price: the system is factorized at each timestep, and every constraint touching the body (contacts,
joints) needs a solve against it. Note that the FEM solver requires cells, so it only applies to the bodies built with
cells, e.g., with the grid, cuboid, or volumetric constructors. The configuration of its linear solves is shared
by every body using it, and lives in the integration parameters:
- Example 2D
- Example 3D
// A stiff beam simulated by the FEM solver (requires the `fem` cargo feature): its stiffness
// doesn't depend on the number of solver iterations.
let beam = SoftBodyBuilder::grid(Vector::new(0.0, 2.0), Vector::new(1.0, 0.1), 21, 3)
.solver(SoftBodySolver::Fem)
.cell_model(SoftBodyCellModel::NeoHookean)
.material(SoftBodyMaterial {
young_modulus: 1.0e5,
poisson_ratio: 0.3,
..Default::default()
})
// The particles of the side at `x = -1` are the first 3 ones.
.pinned_particles(0..3);
let _beam_handle = world.insert_soft_body(beam);
// The tuning of the linear solves of the FEM solver, shared by every body using it.
let fem = &mut world.integration_parameters.soft_bodies.fem;
fem.linear_tolerance = 1.0e-5;
fem.max_linear_iterations = 20;
// A stiff beam simulated by the FEM solver (requires the `fem` cargo feature): its stiffness
// doesn't depend on the number of solver iterations.
let beam = SoftBodyBuilder::cuboid(Vector::new(0.0, 2.0, -3.0), Vector::new(1.0, 0.1, 0.1), 11, 3, 3)
.solver(SoftBodySolver::Fem)
.cell_model(SoftBodyCellModel::NeoHookean)
.material(SoftBodyMaterial {
young_modulus: 1.0e5,
poisson_ratio: 0.3,
..Default::default()
})
// The particles of the face at `x = -1` are the first 3 × 3 ones.
.pinned_particles(0..9);
let _beam_handle = world.insert_soft_body(beam);
// The tuning of the linear solves of the FEM solver, shared by every body using it.
let fem = &mut world.integration_parameters.soft_bodies.fem;
fem.linear_tolerance = 1.0e-5;
fem.max_linear_iterations = 20;
The linear solves stop at the relative residual
linear_tolerance, or after
max_linear_iterations conjugate-gradient iterations,
whatever the residual. The bodies with at most
max_dense_dofs degrees of freedom (600 by default) are
factorized directly, whereas the larger ones rely on the iterative conjugate gradient.
Use the FEM solver for stiff materials which simulated stiffness must not depend on the iteration count, e.g., the chassis of a car or a metal beam. This also results in more realistic plasticity and tearing.
Contacts and self-intersections
A soft-body collides through the collider covering its surface, which is a deformable triangle-mesh (a polyline in 2D) built from the template given to the builder.
Thickness
The radius of the particles
(particle_radius) is
the thickness of the soft-body wrt. collision-detection: it is the contact skin of its surface collider, i.e., the
distance kept between the surface and the objects touching it, as well as the distance the solver bounds the motion of
one particle by at each substep. A radius that is too small relative to the distance between two neighboring particles
may let thin objects pass through the surface, whereas a radius larger than that distance will make the body collide
with itself even when it is at rest. Therefore it is recommended to keep it well below the distance between two
neighboring particles. Note that every constructor picks a sensible default, i.e., about half the length of its edges
or of its cells.
Oriented surfaces and shells
A closed surface (a balloon, a jelly cube, a filled polygon) is oriented: its contacts are generated on its outward
side only, like for an oriented triangle-mesh, so nothing is held
inside it. An open surface (a rope, a cloth) is two-sided, because a body arriving from either side must be stopped.
This is what the builder does by default, and it is what a solid body wants. A shell, i.e., a closed surface which
inner side must hold the bodies contained in it, is obtained by asking for a surface that is not oriented
(oriented):
- Example 2D
- Example 3D
// A shell: a closed surface that is not oriented, so its inner side holds the bodies put
// inside it (a bowl, a box, a container). A closed surface is oriented by default.
let bowl = SoftBodyBuilder::disk(Vector::new(-3.0, 2.0), 0.8, 24)
.oriented(false)
.softness(SpringCoefficients::new(60.0, 1.0));
let _bowl_handle = world.insert_soft_body(bowl);
// A shell: a closed surface that is not oriented, so its inner side holds the bodies put
// inside it (a bowl, a box, a container). A closed surface is oriented by default.
let bowl = SoftBodyBuilder::sphere(Vector::new(-3.0, 2.0, 0.0), 0.8, 2)
.oriented(false)
.softness(SpringCoefficients::new(60.0, 1.0));
let _bowl_handle = world.insert_soft_body(bowl);
After the insertion, the shape of the Collider is
the authority: the flag is changed there, like for any other collider.
Self-contacts
A soft-body doesn't collide with itself by default. Self-contacts are enabled by
SoftBodyBuilder::self_contacts,
which makes the vertices and the edges of the surface collide with the surface of their own body: this is what keeps
a cloth folding onto itself, or a jelly squashed against itself, from passing through itself. Note that they are more
expensive, since the whole surface must be tested against itself.
Penetrations and tangles
The contacts between two meshes are computed triangle by triangle (segment by segment in 2D), without any notion of their interiors. As long as the two surfaces don't penetrate, this works well. But as soon as they do, some of these local contacts start pointing the wrong way, and actively keep the two surfaces in their penetrating state instead of separating them. Deformations make it worse, since a single surface can also cross itself and end up tangled:
Rapier handles these configurations by measuring the volume of the overlap between the two surfaces. The gradient of that volume gives a good approximation of the direction separating the two bodies, aka. the volume normal, which is used both to push the overlapping regions apart, and to correct the direction of the local contacts inside them:
This is enabled by default for the closed surfaces, against other soft-bodies as well as against rigid colliders.
It is configured, along with the detection and the recovery of the tangled configurations, by the recovery settings of
the global settings
(IntegrationParameters::soft_bodies.recovery).
Every mechanism can be switched off individually, and the table below lists the ones to look at for the most common
problems:
| Problem | What to change |
|---|---|
| Thin or fast objects pass through a surface. | Raise the particle radius (particle_radius). Let the body request more substeps while it is hit fast (max_extra_substeps). |
| Two bodies crossing corner-first don't collide. | Enable edge_speculation (note that it can leave pressed 3D piles crossed). |
| A body stays tangled with itself. | Keep self_stand_down enabled (the default), which lets the elasticity untangle it, or push the crossed features apart with crossing_repulsion. |
| The recovery from a penetration is too slow, or too violent. | Change the recovery_pace, i.e., the corrective speed allowed to the recovery (in length units per second). |
| Soft contacts feel too spongy. | Raise the contact_stiffening of the soft-body contacts relative to the rigid ones. |
| The overlap of two bodies isn't resolved at all. | Make sure both surfaces are closed: the intersection-volume constraints (overlap_constraints) only apply to them. |
Soft frames: joints and rigid colliders
Joints and rigid colliders both need a frame to be attached to, i.e., a translation and a rotation, which a soft-body
doesn't have. This is what soft frames are for: a soft frame is a rigid-body of type
RigidBodyType::SoftFrame which pose is computed at each timestep
from a set of particles, by shape-matching. Since it is an ordinary rigid-body, every API working with rigid-bodies
works with it too: impulse joints of any kind (fixed, revolute, prismatic, generic, etc.) can be
attached to it, as well as rigid colliders (sensors included), and its position can be read at any time. Therefore a
soft-body is linked to another soft-body, to a rigid-body, or to a multibody
exactly the same way two rigid-bodies are.
The root body
Every soft-body is created with one soft frame covering all of its particles: its root body. It is the rigid-body
given by SoftBody::root_body. A
joint attached to it acts on the soft-body as a whole, and so does a force or an impulse applied to it. It also stands
for the soft-body in the islands, and it is the parent of the colliders the engine built for the body's surface (a
deformable collider bound to another cluster has the proxy of that cluster as its parent
instead). The soft-body a collider belongs to is given by Collider::deformable_mesh_ref, which is how a collider reported by a
scene query or by a collision
event is traced back to the body it
covers.
- Example 2D
- Example 3D
// The rigid body the engine created for the whole soft body, read back after its insertion.
let root: RigidBodyHandle = world.soft_bodies[jelly_handle].root_body();
assert!(world.bodies[root].is_soft_frame());
// A rigid collider attached to it follows the frame of the whole body: here a sensor
// detecting what comes close to the jelly.
let _sensor = world.insert_collider(ColliderBuilder::ball(1.6).sensor(true), Some(root));
// A joint attached to it acts on the soft body as a whole: this one hangs the jelly under a
// fixed anchor by a spring.
let anchor = world.insert_body(RigidBodyBuilder::fixed().translation(Vector::new(3.0, 5.0)));
world.insert_impulse_joint(anchor, root, SpringJointBuilder::new(2.0, 60.0, 2.0));
// The rigid body the engine created for the whole soft body, read back after its insertion.
let root: RigidBodyHandle = world.soft_bodies[jelly_handle].root_body();
assert!(world.bodies[root].is_soft_frame());
// A rigid collider attached to it follows the frame of the whole body: here a sensor
// detecting what comes close to the jelly.
let _sensor = world.insert_collider(ColliderBuilder::ball(1.0).sensor(true), Some(root));
// A joint attached to it acts on the soft body as a whole: this one hangs the jelly under a
// fixed anchor by a spring.
let anchor =
world.insert_body(RigidBodyBuilder::fixed().translation(Vector::new(3.0, 4.0, 0.0)));
world.insert_impulse_joint(anchor, root, SpringJointBuilder::new(2.5, 60.0, 2.0));
The pose of the root body is recomputed from the particles at each timestep, therefore moving it has no effect. Removing it is not a no-op though: like any cluster proxy, it takes its cluster with it, i.e., the whole soft-body, unless another cluster covers some of its particles (see removal).
Clusters
A single frame for the whole body is often not expressive enough: several joints attached to the root body all act on
the body as a whole, and their effect isn't concentrated where they are attached. This is why a soft-body can also be
given clusters (PhysicsWorld::add_soft_body_cluster), i.e.,
soft frames over any subset of its particles, each with its own pose computed by shape-matching over that subset only.
Joints attached to different clusters then act on different parts of the body, each with its own orientation:
Similarly, rigid colliders attached to the proxies of different clusters move and rotate independently, which is what allows the definition of rigid parts on a deformable body: the handle of a deformable hammer, the bones of a soft character, or the plate a jelly is carried on.
- Example 2D
- Example 3D
// A cluster over the top particles of the jelly: a rigid proxy that joints and
// colliders can attach to.
let top: Vec<u32> = {
let jelly = &world.soft_bodies[jelly_handle];
(0..jelly.num_particles() as u32)
.filter(|&i| jelly.particle_position(i as usize).y > 2.0)
.collect()
};
let cluster = world
.add_soft_body_cluster(jelly_handle, &top)
.expect("at least one valid particle");
let proxy: RigidBodyHandle = world.soft_bodies[jelly_handle]
.cluster_proxy(cluster)
.unwrap();
// A rigid plate welded onto the cluster.
let (plate, _) = world.insert(
RigidBodyBuilder::dynamic().translation(Vector::new(3.0, 2.4)),
ColliderBuilder::cuboid(1.2, 0.05).density(0.4),
);
world.insert_impulse_joint(
plate,
proxy,
FixedJointBuilder::new().local_anchor1(Vector::new(0.0, -0.1)),
);
// A cluster can be pinned, driven or tuned as a whole.
let jelly = &mut world.soft_bodies[jelly_handle];
jelly.set_cluster_stiffness_scale(cluster, 2.0);
jelly.enable_cluster_shape_matching(cluster, true);
// A cluster over the top particles of the jelly: a rigid proxy that joints and
// colliders can attach to.
let top: Vec<u32> = {
let jelly = &world.soft_bodies[jelly_handle];
(0..jelly.num_particles() as u32)
.filter(|&i| jelly.particle_position(i as usize).y > 1.3)
.collect()
};
let cluster = world
.add_soft_body_cluster(jelly_handle, &top)
.expect("at least one valid particle");
let proxy: RigidBodyHandle = world.soft_bodies[jelly_handle]
.cluster_proxy(cluster)
.unwrap();
// A rigid plate welded onto the cluster.
let (plate, _) = world.insert(
RigidBodyBuilder::dynamic().translation(Vector::new(3.0, 1.9, 0.0)),
ColliderBuilder::cuboid(0.7, 0.05, 0.7).density(0.4),
);
world.insert_impulse_joint(
plate,
proxy,
FixedJointBuilder::new().local_anchor1(Vector::new(0.0, -0.1, 0.0)),
);
// A cluster can be pinned, driven or tuned as a whole.
let jelly = &mut world.soft_bodies[jelly_handle];
jelly.set_cluster_stiffness_scale(cluster, 2.0);
jelly.enable_cluster_shape_matching(cluster, true);
A cluster also defines a few settings for the elements it covers, which gives regional materials without needing separate bodies:
- The stiffness scale
(
set_cluster_stiffness_scale) multiplies the Young modulus of every cell entirely contained in the cluster (the cells straddling its boundary are left unchanged). - The edge softness
(
set_cluster_edge_softness) overrides the softness of every edge entirely contained in the cluster, e.g., a stiffer collar on a shirt. - The tear resistance
(
set_cluster_tear_resistance) multiplies the tear thresholds of every element entirely contained in the cluster, e.g., a tough region, or a perforation line. - Shape-matching (
enable_cluster_shape_matching) pulls the particles of the cluster toward the frame of its proxy (or toward the target pose given bySoftBodyCluster::set_shape_matching_target), so that part of the body tends to keep the shape it was created with.
The rotation of a cluster is deduced from its particles, which isn't possible for a cluster made of a single particle (or, in 3D, of collinear particles). Such a cluster has no angular response, therefore the angular parts of the joints attached to its proxy are disabled.
Attaching a rigid-body to a particle
A particle can also be attached directly to a rigid-body (SoftBody::attach_particle), which is
the simplest way of combining the two kinds of bodies: a rope tied to a swinging ball, a flag attached to its pole,
etc. Unlike pinning, an attachment is a two-way point-to-point constraint: the rigid-body holds the particle, and the
particle pulls the rigid-body back. The anchor is the position the particle has when the attachment is created,
expressed in the local frame of the rigid-body. Note that a particle attached twice keeps both of its attachments, and
that an attachment is undone with
SoftBody::detach_particle:
- Example 2D
- Example 3D
// Attach the last particle of a rope to a rigid box, at the particle's position.
let rope = SoftBodyBuilder::rope(Vector::new(8.0, 9.0), Vector::new(12.0, 9.0), 25)
.pinned_particles([0])
.softness(SpringCoefficients::new(40.0, 1.0));
let rope_handle = world.insert_soft_body(rope);
let last_position = world.soft_bodies[rope_handle].particle_position(24);
let (weight, _) = world.insert(
RigidBodyBuilder::dynamic().translation(last_position - Vector::new(0.0, 0.4)),
ColliderBuilder::cuboid(0.3, 0.3).density(2.0),
);
world.soft_bodies[rope_handle].attach_particle(24, weight, &world.bodies);
// Attach the last particle of a rope to a rigid ball, at the particle's position.
let rope = SoftBodyBuilder::rope(Vector::new(-0.5, 5.0, 3.0), Vector::new(2.5, 5.0, 3.0), 30)
.pinned_particles([0])
.softness(SpringCoefficients::new(40.0, 1.0));
let rope_handle = world.insert_soft_body(rope);
let last_position = world.soft_bodies[rope_handle].particle_position(29);
let (ball, _) = world.insert(
RigidBodyBuilder::dynamic().translation(last_position - Vector::new(0.0, 0.3, 0.0)),
ColliderBuilder::ball(0.25).density(2.0),
);
world.soft_bodies[rope_handle].attach_particle(29, ball, &world.bodies);
Particles and kinematic control
The state of a soft-body is the state of its particles, which are identified by their index in the body. Their positions
and their velocities can be read (particle_position, particle_positions, particle_velocity,
particle_velocities) and modified (set_particle_position,
set_particle_velocity) at any time, one by one or all at
once. The elements built from them (edges, cells, and boundary) can be read as well, e.g., in order to render the body with your own mesh.
A particle can also be pinned (set_particle_pinned). A pinned particle is kinematic: it is no longer affected by the forces
nor by the contacts, and it will simply hold its position, or follow the kinematic target
(set_particle_kinematic_target) or the velocity it is given. This is, e.g., how a piece of cloth
is hung on a wall, or how a rope is dragged by the player. Releasing the particle gives it back its nominal mass and
lets it keep its current velocity:
- Example 2D
- Example 3D
let soft_body = &mut world.soft_bodies[sheet_handle];
// Read the particles.
let position = soft_body.particle_position(0);
let velocity = soft_body.particle_velocity(0);
let positions: Vec<Vector> = soft_body.particle_positions().collect();
assert_eq!(positions.len(), soft_body.num_particles());
// Move a particle.
soft_body.set_particle_position(1, position + Vector::new(0.0, 0.1));
soft_body.set_particle_velocity(1, velocity);
// Pin (or release) a particle; a pinned particle can be driven like a kinematic body.
soft_body.set_particle_pinned(2, true);
soft_body.set_particle_kinematic_target(2, Vector::new(-3.5, 3.5));
// The elements: edges, cells and the boundary segments.
let num_edges = soft_body.edges().len();
let num_cells = soft_body.cells().len();
let boundary: &[[u32; 2]] = soft_body.boundary();
assert!(num_edges > 0 && num_cells > 0 && !boundary.is_empty());
let soft_body = &mut world.soft_bodies[cloth_handle];
// Read the particles.
let position = soft_body.particle_position(0);
let velocity = soft_body.particle_velocity(0);
let positions: Vec<Vector> = soft_body.particle_positions().collect();
assert_eq!(positions.len(), soft_body.num_particles());
// Move a particle.
soft_body.set_particle_position(1, position + Vector::new(0.0, 0.1, 0.0));
soft_body.set_particle_velocity(1, velocity);
// Pin (or release) a particle; a pinned particle can be driven like a kinematic body.
soft_body.set_particle_pinned(2, true);
soft_body.set_particle_kinematic_target(2, Vector::new(-1.0, 2.5, -0.8));
// The elements: edges, cells and the boundary triangles.
let num_edges = soft_body.edges().len();
let num_cells = soft_body.cells().len();
let boundary: &[[u32; 3]] = soft_body.boundary();
assert!(num_edges > 0 && num_cells == 0 && !boundary.is_empty());
Setting the position of a particle explicitly teleports it: no contact is taken into account along the way, so a particle can be moved inside of another object this way. Whenever the motion must be seen by the contacts and by the friction (to drag a piece of cloth, for example), it is recommended to pin the particle and to give it a kinematic target instead.
Controlling a region kinematically
A whole region of the body is controlled at once through a cluster covering it. Pinning
the cluster (set_cluster_pinned) pins all of its particles, and its kinematic target
(set_cluster_kinematic_target) moves them
rigidly: each pinned particle is sent where the rest shape of the cluster places it at the target pose, with the
matching velocity. The rest of the body is then simulated as usual, and drags behind the controlled region, e.g., the
hand of a soft character carrying something:
- Example 2D
- Example 3D
// Pin every particle of the cluster, then move it along a path: the cluster behaves like a
// kinematic rigid part dragging the rest of the body.
let jelly = &mut world.soft_bodies[jelly_handle];
jelly.set_cluster_pinned(cluster, true);
jelly.set_cluster_kinematic_target(cluster, Pose::from_translation(Vector::new(3.0, 2.5)));
// Release it: the cluster is simulated again.
jelly.set_cluster_pinned(cluster, false);
// Pin every particle of the cluster, then move it along a path: the cluster behaves like a
// kinematic rigid part dragging the rest of the body.
let jelly = &mut world.soft_bodies[jelly_handle];
jelly.set_cluster_pinned(cluster, true);
jelly.set_cluster_kinematic_target(
cluster,
Pose::from_translation(Vector::new(3.0, 2.0, 0.0)),
);
// Release it: the cluster is simulated again.
jelly.set_cluster_pinned(cluster, false);
Deformable colliders and skinning
Games don't need the simulated shape of a body to be as detailed as its visual shape: a coarse and well-shaped lattice is faster and more stable to simulate than one cell per visual triangle. This is why Rapier supports cage simulation and skinning. The detailed mesh is embedded in a coarse volumetric lattice, aka. its cage, which is the only part being simulated. The vertices of the mesh are then interpolated from the deformed cells holding them, aka. skinning:
Skinned soft-bodies
The
SoftBodyBuilder::volumetric_skinned
constructor computes the cage of a closed mesh automatically, and keeps the
mesh as the skin of the body. That automatic cage is built for performance rather than geometric fidelity: in 3D, it
encloses the whole mesh with the tetrahedra of a lattice, without snapping them to the mesh:

By default, the body still collides through the boundary of its cage, which is as coarse as its cells. Its skin can
become its actual collision mesh instead with
skin_collision.
- Example 2D
- Example 3D
// A detailed outline held by a coarse cage of cells: only the cells are simulated, and the
// outline (the skin) follows their deformation.
let num = 48;
let vertices: Vec<Vector> = (0..num)
.map(|i| {
let angle = i as f32 / num as f32 * std::f32::consts::TAU;
Vector::new(angle.cos(), angle.sin()) * 0.5
})
.collect();
let indices: Vec<[u32; 2]> = (0..num as u32).map(|i| [i, (i + 1) % num as u32]).collect();
let skinned = SoftBodyBuilder::volumetric_skinned(&vertices, &indices, 0.25)
.expect("the polyline must be closed and enclose some area")
// Collide through the skin instead of the boundary of the cage.
.skin_collision(true)
.translated(Vector::new(0.0, 4.0));
let skinned_handle = world.insert_soft_body(skinned);
// The skin is the body's collision mesh: read its vertices back to render it.
let body = &world.soft_bodies[skinned_handle];
let skin = body.collision_mesh().expect("the skin collides");
let skin_vertices: Vec<Vector> = skin.vertex_positions(body).collect();
assert_eq!(skin_vertices.len(), vertices.len());
// A detailed mesh held by a coarse cage of cells: only the cells are simulated, and the mesh
// (the skin) follows their deformation.
let (vertices, indices) = Ball::new(0.5).to_trimesh(24, 24);
let skinned = SoftBodyBuilder::volumetric_skinned(&vertices, &indices, 0.25)
.expect("the mesh must be closed and enclose some volume")
// Collide through the skin instead of the boundary of the cage.
.skin_collision(true)
.translated(Vector::new(0.0, 4.0, 3.0));
let skinned_handle = world.insert_soft_body(skinned);
// The skin is the body's collision mesh: read its vertices back to render it.
let body = &world.soft_bodies[skinned_handle];
let skin = body.collision_mesh().expect("the skin collides");
let skin_vertices: Vec<Vector> = skin.vertex_positions(body).collect();
assert_eq!(skin_vertices.len(), vertices.len());
A skin doesn't need a computed cage: any mesh can be given as the skin of a body built with cells, with
SoftBodyBuilder::skin. Each of its
vertices is bound to the cell closest to it, in the pose the cells are built in.
Deformable colliders
The colliders built from the surface or the skin of a soft-body are generated by the engine itself, but it is also
possible to give a body a collider of your own which vertices follow its particles: a deformable collider
(ColliderSet::insert_deformable). This is a polyline in
2D, or a triangle mesh in 3D, flagged as deformable, and attached to the proxy of one of the
clusters of the body (the root body, a cluster itself, can be used too). Its vertices are
given in the frame of that proxy, and they can be read back at any time in order to render the mesh where the simulation moved
it. A deformable collider can be a sensor as well, e.g., to detect what enters a deformable volume.
How the vertices follow the particles is given by the binding
(SoftMeshBinding):
skinned: each vertex is embedded in the cell of the cluster holding it, i.e., the collider is a skin of the cage.direct: the vertexifollows the particle given for it, which must belong to the cluster. Its alternative that binds every vertex to the closest particle within a given distance (direct_by_position) is useful when the mesh is the one the particles were built from.
- Example 2D
- Example 3D
// A deformable polyline bound to the blob: each vertex follows one particle (`direct`),
// or is embedded in the cell holding it (`skinned`). The polyline is given in the frame
// of the proxy it is attached to.
let root = world.soft_bodies[blob_handle].root_body();
let root_pose = *world.bodies[root].position();
let blob = &world.soft_bodies[blob_handle];
let num = blob.num_particles();
let vertices: Vec<Vector> = blob
.particle_positions()
.map(|p| root_pose.inverse() * p)
.collect();
let indices: Vec<[u32; 2]> = (0..num as u32).map(|i| [i, (i + 1) % num as u32]).collect();
let particles: Vec<u32> = (0..num as u32).collect();
let outline =
ColliderBuilder::polyline_with_flags(vertices, Some(indices), PolylineFlags::DEFORMABLE)
.sensor(true);
let outline_handle = world
.insert_deformable(outline, SoftMeshBinding::direct(particles), root)
.expect("a deformable polyline bound to a cluster proxy");
// The polyline follows the particles: read its current vertices back.
let blob = &world.soft_bodies[blob_handle];
let mesh = blob.mesh_of(outline_handle).unwrap();
let outline_vertices: Vec<Vector> = mesh.vertex_positions(blob).collect();
assert_eq!(outline_vertices.len(), num);
// A deformable triangle mesh bound to the jelly: each vertex is embedded in the cell
// holding it (`skinned`), or follows one particle (`direct`). The mesh is given in the
// frame of the proxy it is attached to.
let root = world.soft_bodies[jelly_handle].root_body();
let root_pose = *world.bodies[root].position();
let center = world.soft_bodies[jelly_handle].center_of_mass();
let r = 1.0;
let vertices: Vec<Vector> = [
Vector::new(r, 0.0, 0.0),
Vector::new(-r, 0.0, 0.0),
Vector::new(0.0, r, 0.0),
Vector::new(0.0, -r, 0.0),
Vector::new(0.0, 0.0, r),
Vector::new(0.0, 0.0, -r),
]
.iter()
.map(|v| root_pose.inverse() * (center + *v))
.collect();
let indices = vec![
[0, 2, 4],
[2, 1, 4],
[1, 3, 4],
[3, 0, 4],
[2, 0, 5],
[1, 2, 5],
[3, 1, 5],
[0, 3, 5],
];
let skin = ColliderBuilder::trimesh_with_flags(vertices, indices, TriMeshFlags::DEFORMABLE)
.unwrap()
.sensor(true);
let skin_handle = world
.insert_deformable(skin, SoftMeshBinding::skinned(), root)
.expect("a deformable mesh bound to a cluster proxy");
// The mesh follows the particles: read its current vertices back.
let jelly = &world.soft_bodies[jelly_handle];
let mesh = jelly.mesh_of(skin_handle).unwrap();
let skin_vertices: Vec<Vector> = mesh.vertex_positions(jelly).collect();
assert_eq!(skin_vertices.len(), 6);
A deformable collider has no mass: its density is ignored, and it is the particles which hold the mass of the soft-body. Note that a collider given no contact skin explicitly gets the particle radius of the soft-body as its skin, so its thickness matches the thickness of the surface of the body.
Plasticity and tearing
A soft-body can deform permanently in two ways:
- Plasticity changes the rest shape of the body, without any change of its topology: a metal sheet folding on impact, a piece of clay being modeled, the chassis of a car denting.
- Tearing changes its topology: pieces of the body physically disconnect from each other, e.g., a piece of fabric torn in two, or a jelly sliced by a blade.
Both are supported by the constraints solver and by the FEM solver. Note that with the constraints solver, the quality of the plastic deformations follows the convergence of the solver: more iterations result in more convincing permanent deformations.
Plasticity
Plasticity is configured by the material of the body, separately for its cells and for its edges:
- A cell strained past its plastic yield
(
plastic_yield) absorbs the strain in excess into its rest shape, at the rate of its plastic creep (plastic_creep, per second), up to a total permanent deformation of its plastic max (plastic_max). This flow preserves the volume of the cell, and an inverted cell never flows. Note that this only applies to the elastic cells (theCorotationalandNeoHookeanmodels): theVolumecells never flow. - An edge strained past its edge plastic yield
(
edge_plastic_yieldcompared to|length / rest_length - 1|) sees its rest length flow toward its current length at the rate of its edge plastic creep (edge_plastic_creep), up to a total permanent set of its edge plastic max (edge_plastic_max, as a fraction of its initial length). Its edge plastic flow (edge_plastic_flow, aSoftEdgePlasticFlow) selects whether that happens when it is squeezed, when it is stretched, or both.
A plastic deformation can be undone at any time
(SoftBody::reset_plasticity),
the particles springing back elastically from there. Note that the tear thresholds of the edges are always measured on
their initial length, not on their plastic one:
- Example 2D
- Example 3D
// The jelly has elastic (corotational) cells: the plasticity of `Volume` cells has no effect.
let material = world.soft_bodies[jelly_handle].material_mut();
// Cells: the rest shape flows toward the current one past 5% strain, at a rate of 20 per
// second, up to a total permanent deformation of 50%.
material.plastic_yield = 0.05;
material.plastic_creep = 20.0;
material.plastic_max = 0.5;
// Edges: the rest length flows past 10% strain, up to half the initial length, but only
// when squeezed (a dent stays, a stretch springs back).
material.edge_plastic_yield = 0.1;
material.edge_plastic_creep = 10.0;
material.edge_plastic_max = 0.5;
material.edge_plastic_flow = SoftEdgePlasticFlow::Compression;
// Every permanent deformation can be undone at once.
world.soft_bodies[jelly_handle].reset_plasticity();
// The jelly has elastic (corotational) cells: the plasticity of `Volume` cells has no effect.
let material = world.soft_bodies[jelly_handle].material_mut();
// Cells: the rest shape flows toward the current one past 5% strain, at a rate of 20 per
// second, up to a total permanent deformation of 50%.
material.plastic_yield = 0.05;
material.plastic_creep = 20.0;
material.plastic_max = 0.5;
// Edges: the rest length flows past 10% strain, up to half the initial length, but only
// when squeezed (a dent stays, a stretch springs back).
material.edge_plastic_yield = 0.1;
material.edge_plastic_creep = 10.0;
material.edge_plastic_max = 0.5;
material.edge_plastic_flow = SoftEdgePlasticFlow::Compression;
// Every permanent deformation can be undone at once.
world.soft_bodies[jelly_handle].reset_plasticity();
Tearing
Tearing is configured by the material of the body as well. An element tears at the end of the timestep during which its load goes beyond one of the two thresholds of the material:
- The tear strain (
tear_strain) applies to the edges (a fraction of their initial rest length) and to the elastic cells (their largest tensile strain). Note that volume cells never tear. - The tear force (
tear_force) applies to the edges only: an edge tears if its force along its direction exceeds it.
The other settings of the material shape how a tear propagates:
- The tear smoothing (
tear_smoothing) is the time constant (in seconds) over which the load of an element is smoothed before being tested, so that a single impact spike doesn't tear. - The interior strength (
interior_strength) makes the undamaged interior elements (without any particle on the surface or on an earlier tear) that many times tougher, so that tears start from the surface or from an existing damage, and run inward. - The max tears per step (
max_tears_per_step) bounds how many edges may tear during one step, the most loaded going first, which paces the cracks of a taut sheet (an edge loaded past twice its threshold always tears). - The min piece (
min_piece) is the smallest piece (in elements) a tear may split off, any tear leaving a smaller piece waiting until it doesn't.
Individual edges can be made tougher (or weaker, e.g., a perforation line) with their tear resistance, given to the
builder
(edge_tear_resistance)
or by cluster:
- Example 2D
- Example 3D
let material = world.soft_bodies[sheet_handle].material_mut();
// An edge tears past 40% of stretch, or past a force of 50 along its direction.
material.tear_strain = Some(0.4);
material.tear_force = Some(50.0);
// The load is smoothed over 0.1 second, so a single impact spike doesn't tear.
material.tear_smoothing = 0.1;
// Undamaged interior elements are twice as tough: tears start from the surface.
material.interior_strength = 2.0;
// A tear never splits off a piece smaller than 10 elements.
material.min_piece = Some(10);
let material = world.soft_bodies[cloth_handle].material_mut();
// An edge tears past 40% of stretch, or past a force of 50 along its direction.
material.tear_strain = Some(0.4);
material.tear_force = Some(50.0);
// The load is smoothed over 0.1 second, so a single impact spike doesn't tear.
material.tear_smoothing = 0.1;
// Undamaged interior elements are twice as tough: tears start from the surface.
material.interior_strength = 2.0;
// A tear never splits off a piece smaller than 10 elements.
material.min_piece = Some(10);
A tear can also be requested explicitly, either edge by edge
(SoftBody::tear_edge, tear_cell),
or all at once along a set of edges and through a set of cells
(PhysicsWorld::tear_soft_body).
Finally, a body can be cut
(PhysicsWorld::cut_soft_body)
along a blade, i.e., a segment in 2D or a triangle in 3D, which is the most convenient way of slicing a body with the
weapon of a player. Note that the cuts ignore the min piece threshold.
Tearing and cutting lose no material: the particles are duplicated along the tear instead of being removed, so the area (2D) or the volume (3D) of the body is preserved. The pieces a tear disconnects become soft-bodies of their own, which keep the material and the settings of the body they come from, the deformable meshes and the joints following the pieces they were attached to. Therefore the particles of the torn body are renumbered, and the returned event tells where each of them went:
- Example 2D
- Example 3D
// Elements tear on their own past the material's thresholds; a tear can also be requested.
world.soft_bodies[sheet_handle].tear_edge(10); // Applied at the end of the next step.
// Tear at once along edges and through cells; pieces the tear disconnects become soft
// bodies of their own.
let event = world.tear_soft_body(sheet_handle, &[11, 12], &[]);
if let Some(event) = event {
println!("{} edges torn", event.torn_edges.len());
}
// Cut along a blade (a segment in 2D), without removing material.
let blade = [Vector::new(-3.0, -10.0), Vector::new(-3.0, 10.0)];
if let Some(event) = world.cut_soft_body(sheet_handle, &blade) {
for piece in &event.pieces {
println!(
"piece {:?} has {} particles",
piece.soft_body,
piece.particles.len()
);
}
// Where a particle of the torn body went.
if let Some((body, index)) = event.particle_destination(n as u32 * n as u32 - 1) {
println!(
"particle {} is now particle {} of {:?}",
n * n - 1,
index,
body
);
}
}
// Elements tear on their own past the material's thresholds; a tear can also be requested.
world.soft_bodies[cloth_handle].tear_edge(10); // Applied at the end of the next step.
// Tear at once along edges and through cells; pieces the tear disconnects become soft
// bodies of their own.
let event = world.tear_soft_body(cloth_handle, &[11, 12], &[]);
if let Some(event) = event {
println!("{} edges torn", event.torn_edges.len());
}
// Cut along a blade (a triangle in 3D), without removing material.
let blade = [
Vector::new(-0.1, -10.0, -10.0),
Vector::new(-0.1, 10.0, 0.0),
Vector::new(-0.1, -10.0, 10.0),
];
if let Some(event) = world.cut_soft_body(cloth_handle, &blade) {
for piece in &event.pieces {
println!(
"piece {:?} has {} particles",
piece.soft_body,
piece.particles.len()
);
}
// Where a particle of the torn body went.
if let Some((body, index)) = event.particle_destination(n as u32 * n as u32 - 1) {
println!(
"particle {} is now particle {} of {:?}",
n * n - 1,
index,
body
);
}
}
Tearing one edge with
SoftBody::tear_edge
only marks it: the tear is applied at the end of the next step, together with the tears the simulation generates itself.
The methods of the
PhysicsWorld
tear and cut immediately, which is why they are the ones giving back an event.
Volume cells never tear. Therefore a body which cells use the Volume model
will only tear along its edges, and a material with a tear strain should be combined with the
Corotational or the NeoHookean
cell model if you expect it to be torn apart.
Tear events
The tears applied during a step, whether they were generated by the simulation itself or requested with
SoftBody::tear_edge,
are reported the same way as the
collision events: by giving
an event handler (EventHandler::handle_soft_body_tear_event) to the step.
Each event (a SoftBodyTearEvent) identifies the soft-body that tore and gives the
pieces it was split into, so the rendering of the scene can be updated accordingly:
- Example 2D
- Example 3D
// Tears applied during a step are reported through the event handler.
let (collision_send, _collision_recv) = std::sync::mpsc::channel();
let (contact_force_send, _contact_force_recv) = std::sync::mpsc::channel();
let (soft_body_tear_send, soft_body_tear_recv) = std::sync::mpsc::channel();
let event_handler =
ChannelEventCollector::new(collision_send, contact_force_send, soft_body_tear_send);
world.step_with_events(&(), &event_handler);
while let Ok(tear_event) = soft_body_tear_recv.try_recv() {
println!("Soft body {:?} tore", tear_event.soft_body);
}
// Tears applied during a step are reported through the event handler.
let (collision_send, _collision_recv) = std::sync::mpsc::channel();
let (contact_force_send, _contact_force_recv) = std::sync::mpsc::channel();
let (soft_body_tear_send, soft_body_tear_recv) = std::sync::mpsc::channel();
let event_handler =
ChannelEventCollector::new(collision_send, contact_force_send, soft_body_tear_send);
world.step_with_events(&(), &event_handler);
while let Ok(tear_event) = soft_body_tear_recv.try_recv() {
println!("Soft body {:?} tore", tear_event.soft_body);
}
Forces and impulses
Forces and impulses can be applied to a soft-body as a whole
(add_force, apply_impulse),
or to one particular particle
(add_particle_force, apply_particle_impulse).
A force added to the whole body is added to each of its free particles and is persistent, i.e., it keeps being applied
at each step until the forces are reset (reset_forces), exactly like the forces of
a rigid-body. An impulse applied to the whole body is a velocity change
applied to each of its free particles, so the body is kicked as a whole without being deformed. Note that the pinned
particles ignore both.
Two additional methods are provided for the effects which magnitude depends on the distance to a point: an impulse
applied within a radius
(apply_impulse_at_point), and a
radial blast pushing the particles away from its center
(apply_radial_impulse). In both
cases the impulse is scaled linearly down to zero at the given radius. Like for the rigid-bodies,
the last boolean argument of all these methods ensures the soft-body is
awake before the force or the impulse is applied:
- Example 2D
- Example 3D
let soft_body = &mut world.soft_bodies[sheet_handle];
// The `true` argument makes sure the soft body is awake.
soft_body.reset_forces(true); // Reset the forces to zero.
soft_body.add_force(Vector::new(0.0, 1.0), true); // Spread over the particles by mass.
soft_body.add_particle_force(3, Vector::new(0.0, 1.0), true);
soft_body.apply_impulse(Vector::new(0.0, 0.1), true);
soft_body.apply_particle_impulse(3, Vector::new(0.0, 0.1), true);
// An impulse on the particles within 0.5 of a point, scaled down with the distance.
soft_body.apply_impulse_at_point(Vector::new(0.0, 0.1), Vector::new(-3.0, 3.0), 0.5, true);
// A blast pushing the particles away from a center.
soft_body.apply_radial_impulse(Vector::new(-3.0, 3.0), 0.1, 1.0, true);
let soft_body = &mut world.soft_bodies[cloth_handle];
// The `true` argument makes sure the soft body is awake.
soft_body.reset_forces(true); // Reset the forces to zero.
soft_body.add_force(Vector::new(0.0, 1.0, 0.0), true); // Spread over the particles by mass.
soft_body.add_particle_force(3, Vector::new(0.0, 1.0, 0.0), true);
soft_body.apply_impulse(Vector::new(0.0, 0.1, 0.0), true);
soft_body.apply_particle_impulse(3, Vector::new(0.0, 0.1, 0.0), true);
// An impulse on the particles within 0.5 of a point, scaled down with the distance.
soft_body.apply_impulse_at_point(
Vector::new(0.0, 0.1, 0.0),
Vector::new(0.0, 2.0, 0.0),
0.5,
true,
);
// A blast pushing the particles away from a center.
soft_body.apply_radial_impulse(Vector::new(0.0, 2.0, 0.0), 0.1, 1.0, true);
Global settings
A few settings are shared by every soft-body of the world. They are part of the integration parameters, so they can be changed between two steps:
- The re-sweep strain
(
resweep_strain) is the strain beyond which a constraint is solved once more after the contacts of every substep. This is what keeps a light body buried under heavier ones from being torn apart by them. - The maximum number of extra substeps
(
max_extra_substeps) is the number of substeps a soft-body is allowed to ask for while it is hit fast. Those substeps bound the motion of its particles, and the motion of the rigid-bodies approaching them, by about one particle radius per substep. Giving zero disables them. - The contact stiffening
(
contact_stiffening) multiplies the natural frequencies of the contacts involving a soft-body. Those contacts run stiffer than the rigid ones because the mass behind one contact is the mass of a few particles, and not the mass of the whole body.
In addition, the detection of the penetrations and of the tangles, as well as the way the bodies recover from them, are
configured by the recovery settings (recovery, a SoftRecoverySettings), where every mechanism can be switched
off individually (see the contacts section for the ones to look at
first). The tuning of the linear solves of the FEM solver lives there as well (fem, a
SoftFemParameters):
- Example 2D
- Example 3D
// Settings shared by every soft body of the world.
let settings = &mut world.integration_parameters.soft_bodies;
// Strain beyond which a constraint is re-solved after the contacts of every substep.
// Default: 0.75
settings.resweep_strain = 0.75;
// Extra substeps a soft body requests while it is hit fast; 0 disables them.
// Default: 4
settings.max_extra_substeps = 4;
// Stiffening of the soft-body contacts relative to the rigid ones.
// Default: 4.0
settings.contact_stiffening = 4.0;
// The tangle detection and recovery stack can be switched off mechanism by mechanism.
settings.recovery.crossing_repulsion = true;
// Settings shared by every soft body of the world.
let settings = &mut world.integration_parameters.soft_bodies;
// Strain beyond which a constraint is re-solved after the contacts of every substep.
// Default: 0.75
settings.resweep_strain = 0.75;
// Extra substeps a soft body requests while it is hit fast; 0 disables them.
// Default: 4
settings.max_extra_substeps = 4;
// Stiffening of the soft-body contacts relative to the rigid ones.
// Default: 4.0
settings.contact_stiffening = 4.0;
// The tangle detection and recovery stack can be switched off mechanism by mechanism.
settings.recovery.crossing_repulsion = true;
Removal
Removing a soft-body (PhysicsWorld::remove_soft_body) removes everything the engine created for it: its root
body, the proxies of its clusters, the colliders of its surface and of its deformable meshes, as well as the joints
attached to any of them. One cluster can also be removed on its own
(PhysicsWorld::remove_soft_body_cluster):
- Example 2D
- Example 3D
// Removing a soft body removes its root body, its proxies, its colliders and the joints
// attached to them.
world.remove_soft_body(rope_handle);
// A cluster can be removed on its own.
world.remove_soft_body_cluster(jelly_handle, cluster);
// Removing a soft body removes its root body, its proxies, its colliders and the joints
// attached to them.
world.remove_soft_body(rope_handle);
// A cluster can be removed on its own.
world.remove_soft_body_cluster(jelly_handle, cluster);
Removing a cluster also removes the particles that only this cluster covered, with their elements and their attachments. Therefore removing the last cluster of a soft-body removes the soft-body itself. Note that removing the proxy of a cluster from the rigid-body set is equivalent to removing the cluster.