Soft Bodies and Particles#

Some objects change shape: a rubber toy squashes, cloth folds, and water flows. ovphysx simulates them with soft bodies and particles. Both run only on an NVIDIA GPU. On the CPU, ovphysx logs an error and leaves them out of the simulation.

A soft green cube falls from 0.75 m, squashes to 0.363 m tall when it hits the ground, springs back, and settles at about 0.46 m tall. Next to it, the program's output lines appear one by one as simulated time passes.

Soft Bodies#

A soft body, also called a deformable body, is simulated on a mesh of points joined into small shapes. When the body hits something, its points move apart or together. Forces between them then pull the mesh back toward its rest shape, the shape it has when nothing pushes on it.

Kind

Simulation Mesh

Use It For

Volume

Tetrahedra, in a TetMesh

Solid objects that squash, such as rubber or flesh.

Surface

Triangles, in a Mesh

Thin objects that bend, such as cloth or paper.

A scene marks a soft body with these API schemas:

API Schema

Applied To

What It Does

OmniPhysicsDeformableBodyAPI

The body

Makes the prim a soft body.

OmniPhysicsVolumeDeformableSimAPI or OmniPhysicsSurfaceDeformableSimAPI

The simulation mesh

Marks the mesh to simulate and holds its rest shape.

PhysicsCollisionAPI

The collision mesh

Marks the mesh the body collides with. A soft body needs one.

OmniPhysicsDeformableMaterialAPI

A material bound to the body

Sets how soft the body is.

Assets often have no tetrahedral mesh. With PhysxAutoDeformableBodyAPI, ovphysx builds the simulation mesh from a render mesh when it attaches the stage.

A Soft Cube#

The following program, soft_bodies_cube.py, drops a soft cube with 0.5 m sides and prints its height. Its simulation mesh is also its collision mesh: the eight corners of the cube, joined into five tetrahedra. Real soft bodies use many more points.

import ovstage
from ovphysx.population import physics_population_descs
from ovphysx import PhysX
from ovphysx.samples.python_samples.schema_support import physx_schema_paths
from ovphysx.types import SimObjectType

# A soft cube with 0.5 m sides, whose bottom starts 0.75 m above the ground.
# Its simulation mesh joins the 8 corners into 5 tetrahedra.
SCENE = """#usda 1.0
(
    metersPerUnit = 1
    kilogramsPerUnit = 1
    upAxis = "Z"
)

def Xform "World"
{
    def PhysicsScene "PhysicsScene" {}

    def Plane "Ground" (
        prepend apiSchemas = ["PhysicsCollisionAPI"]
    )
    {
        uniform token axis = "Z"
    }

    def Material "SoftMaterial" (
        prepend apiSchemas = ["OmniPhysicsDeformableMaterialAPI",
                              "PhysxDeformableMaterialAPI"]
    )
    {
        float omniphysics:youngsModulus = 20000
        float omniphysics:poissonsRatio = 0.45
        float physxDeformableMaterial:elasticityDamping = 0.05
    }

    def TetMesh "SoftCube" (
        prepend apiSchemas = ["OmniPhysicsDeformableBodyAPI",
                              "OmniPhysicsVolumeDeformableSimAPI",
                              "PhysicsCollisionAPI", "MaterialBindingAPI"]
    )
    {
        point3f[] points = [
            (0, 0, 0.75), (0.5, 0, 0.75), (0.5, 0.5, 0.75), (0, 0.5, 0.75),
            (0, 0, 1.25), (0.5, 0, 1.25), (0.5, 0.5, 1.25), (0, 0.5, 1.25)
        ]
        int4[] tetVertexIndices = [
            (0, 1, 3, 4), (1, 2, 3, 6), (1, 4, 5, 6), (3, 4, 6, 7), (1, 3, 4, 6)
        ]
        int3[] surfaceFaceVertexIndices = [
            (0, 3, 1), (0, 1, 4), (0, 4, 3), (1, 3, 2), (1, 2, 6), (2, 3, 6),
            (1, 5, 4), (1, 6, 5), (4, 5, 6), (3, 4, 7), (3, 7, 6), (4, 6, 7)
        ]
        point3f[] omniphysics:restShapePoints = [
            (0, 0, 0.75), (0.5, 0, 0.75), (0.5, 0.5, 0.75), (0, 0.5, 0.75),
            (0, 0, 1.25), (0.5, 0, 1.25), (0.5, 0.5, 1.25), (0, 0.5, 1.25)
        ]
        int4[] omniphysics:restTetVtxIndices = [
            (0, 1, 3, 4), (1, 2, 3, 6), (1, 4, 5, 6), (3, 4, 6, 7), (1, 3, 4, 6)
        ]
        rel material:binding:physics = </World/SoftMaterial>
    }
}
"""


def cube_height(physx):
    """Read the points of every volume soft body and return the cube's height."""
    with physx.read_sync(SimObjectType.DEFORMABLE_VOLUME, ["points"]) as result:
        points = result.groups[0].tensors[0].numpy()  # this body: one row per point
        return float(points[:, 2].max() - points[:, 2].min())


ovstage.population.register_usd_schemas([str(p) for p in physx_schema_paths()])

stage = ovstage.Stage("soft_cube")
ovstage.population.open_usd_from_string_with_desc(
    stage, SCENE, ordinal=1, time=ovstage.population.DEFAULT_TIME, descs=physics_population_descs(rendering=True)
)
stage.advance_write_floor(ordinal=1).wait()

physx = PhysX()
physx.wait_task(physx.attach_ovstage(stage, read_ordinal=1))

print(f"t=0.00 s  height = {cube_height(physx):.3f} m")
for step in range(1, 91):
    physx.step_sync(1.0 / 60.0)
    if step % 15 == 0:
        print(f"t={step / 60:.2f} s  height = {cube_height(physx):.3f} m")

physx.wait_task(physx.detach_ovstage())
physx.destroy()
stage.destroy()

It prints:

t=0.00 s  height = 0.500 m
t=0.25 s  height = 0.500 m
t=0.50 s  height = 0.363 m
t=0.75 s  height = 0.504 m
t=1.00 s  height = 0.468 m
t=1.25 s  height = 0.453 m
t=1.50 s  height = 0.460 m

Notice the following:

  • The cube settles at about 0.46 m, pressed down by its own weight. A rigid box would print 0.500 m on every line.

  • The material sets the softness. youngsModulus is the stiffness in pascals, and a lower value is softer. poissonsRatio makes the cube bulge sideways when it is squashed, and elasticityDamping makes its wobbling die out.

  • Here, the points arrays are in GPU memory, and numpy() copies them to the CPU.

Particles#

Particles simulate liquids and granular material such as sand. Each particle is a small sphere, and many of them together flow, splash, and settle:

Side view of 750 liquid particles. A block of particles falls from where the dashed outline shows it started and splashes on the ground in a low mound that spreads outward, as water does.

A scene needs two parts:

Part

What It Does

A PhysxParticleSystem prim

Holds settings that its particles share, such as their size, which particleContactOffset sets.

A Points prim with PhysxParticleSetAPI

Makes each point a particle. Its physxParticle:particleSystem relationship names the particle system.

On the particle set, set physxParticle:fluid to 1 for a liquid or to 0 for a granular solid.

Reading Their State#

A read returns one array per object. Its points hold one row per mesh point or particle, relative to the prim that holds them.

Object Type

One Array Per

DEFORMABLE_VOLUME

Volume soft body

DEFORMABLE_SURFACE

Surface soft body

DEFORMABLE_SKIN_MESH

Render mesh that follows a soft body. Its points are read-only.

PARTICLE_SET

Particle set

More Detail#