An introduction to soft-body physics
While rope, cloth, and foam rubber can be simulated using many small rigid bodies, it is more convenient and efficient to treat them as a single body that can be deformed. To this end, Jolt JNI supports simulation of soft bodies in a manner roughly analogous to rigid bodies.
A comparison of soft bodies and rigid bodies
Unlike a rigid body, a soft body’s collision shape is only a placeholder. The body is composed of point masses (called vertices) whose locations are specified in system coordinates. The body’s position, shape, and mass distribution are all derived from its vertices.
-
To simulate rope, vertices can be connected in pairs (called edges).
-
To simulate cloth, vertices can be connected to form triangles (called faces).
-
To simulate foam rubber, vertices can be connected to form tetrahedra (called volumes).
The location of a soft body is defined as the center of its axis-aligned bounding box.
Soft bodies collide only with rigid bodies; they do not collide with other soft bodies.
Creation
To construct a soft body:
-
Instantiate and configure a
SoftBodySharedSettings
object. -
Add vertices to the shared-settings object using
addVertex()
. -
(Optional) Add faces to the shared settings using
addFace()
. -
Add edges and volumes to the shared settings using
addEdgeConstraint()
andaddVolumeConstraint()
.-
Alternatively, you may use
createConstraints()
to generate appropriate edges for the pre-existing faces.
-
-
Optimize the shared settings using
optimize()
. -
Instantiate and configure a
SoftBodyCreationSettings
object that incorporates the shared settings. -
Create the body as you would a rigid body, only using
createSoftBody()
instead ofcreateBody()
. -
Like rigid bodies, soft bodies aren’t simulated unless they’re added to a physics system.
Even though it’s possible to append individual vertices, edges, and faces to a shared-settings object, it’s often more convenient to generate a 3-D mesh with the desired shape and then append that mesh to the shared settings.
Meshes intended for graphics rendering often prove unsuitable for soft-body simulation. For instance, they may define multiple vertices at the same position or their edges/faces may be insufficiently subdivided. |
For a simple example of a soft body, see HelloSoftBody.
Solver iterations
During each simulation step, Jolt Physics applies an iterative solver to update the vertex locations in each soft body.
The number of iterations is stored in the body’s motion properties. You can improve accuracy by increasing the number of iterations:
SoftBodyMotionProperties sbmp = (SoftBodyMotionProperties)body.getMotionProperties();
sbmp.setNumIterations(10);
Compliance
Each edge acts like a spring with specific "rest length" and compliance. Compliance is the inverse of stiffness.
To simulate an object that flexes easily (such as cloth), create a soft
body with many faces and non-zero edge compliance.
A simple way to do this is to configure the vertex attributes
with the desired compliance and then
let createConstraints()
calculate the lengths:
SoftBodySharedSettings sbss = /* ... */;
VertexAttributes[] vertexAttributes = /* ... */;
float stiffness = /* ... */;
for (int i = 0; i < numVertices; ++i) {
vertexAttributes[i] = new VertexAttributes();
vertexAttributes[i].setShearCompliance(1f / stiffness);
}
sbss.createConstraints(vertexAttributes, EBendType.Distance);
For a simple example of cloth simulation, see HelloCloth.
Mass distribution
When a vertex is appended to a shared-settings object, it has mass=1 by default.
To alter the mass of an existing vertex, use its setInvMass()
method:
SoftBodySharedSettings sbss = /* ... */;
float mass = /* ... */;
Vertex vertex = sbss.getVertex(vertexIndex);
vertex.setInvMass(1f / mass);
If a soft-body vertex has invMass=0, it becomes pinned (immovable, like a static rigid body).
For a simple example of a pinned vertex, see HelloPin.java.
Simulating a rope
HelloSoftRope is a Sport-Jolt app that demonstrates one way to simulate rope using a soft body.