Stable Simulations#

A simulation is unstable when objects shake, sink into each other, fly apart, or pass through each other. The changes on this page fix most cases.

Two side views of the same stack of six boxes, in a scene that allows the solver one position iteration. On the left, with 1/60 s steps, the stack leans, buckles after about 3 s, and its top box ends on the ground 4.2 m away. On the right, with 1/240 s steps, the stack stands still and its top box moves 0.003 m.

What to Change#

You See

Change

Setting

Stacks lean and fall, joints stretch, objects shake

Take several smaller steps for each step your program needs, as in the figure. ovphysx never splits a step on its own.

The dt that you pass to step or step_sync

Stacks sag, objects sink into each other, joints drift apart

Give the solver more iterations, the passes it makes in each step to correct positions. More iterations cost time.

physxRigidBody:solverPositionIterationCount, 16 by default, or physxArticulation:solverPositionIterationCount for an articulation, 32 by default. The scene’s physxScene:maxPositionIterationCount caps both.

A heavy object sinks through light ones

Keep the masses of objects that touch close to each other.

physics:mass, on a body with PhysicsMassAPI

Objects jump apart at the first step

Place the shapes so that they do not touch at the start.

The positions in your scene

Joints jitter or shake, or a gripper pushes away what it holds

Use the lowest stiffness that reaches the target, enough damping that the joint does not swing past it, and a force limit. Drives explains them.

The drive’s stiffness, damping, and maxForce, such as drive:angular:physics:maxForce

A fast object passes through a wall or the ground

Turn on continuous collision detection (CCD), which looks for contacts between steps. Try speculative CCD first, because it is cheaper. If objects still pass through, also turn on sweep CCD.

Speculative: physxRigidBody:enableSpeculativeCCD. Sweep: physxRigidBody:enableCCD with physxScene:enableCCD.

A thin object, or a somewhat fast one, passes through another

Raise the contact offset of the moving shape, so that contacts start at that distance. Larger offsets cost time.

physxCollision:contactOffset

A setting whose name starts with physx, such as physxRigidBody:enableCCD, works only when the matching API schema, here PhysxRigidBodyAPI, is applied to the prim.

Tests with simple box scenes showed the following:

  • A 1,000 kg box sank straight through a stack of 1 kg boxes, even with 64 iterations. A 100 kg box sank about 1 cm.

  • A 10 cm box at 10 m/s passed through a 2 cm wall. A contact offset of 0.3 m stopped it. At 30 m/s, only CCD stopped it.

A Stack That Stands#

The figure comes from this program, stable_simulations_stack.py. Its scene allows the solver one position iteration, which makes the stack of six 0.5 m boxes fragile. It simulates 5 s with 1/60 s steps, then with 1/240 s steps.

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


def box(i):
    """A 0.5 m box, the i-th from the bottom of the stack."""
    return f"""
    def Cube "Box{i}" (
        prepend apiSchemas = ["PhysicsRigidBodyAPI", "PhysicsCollisionAPI"]
    )
    {{
        double size = 0.5
        double3 xformOp:translate = (0, 0, {0.25 + 0.5 * i})
        uniform token[] xformOpOrder = ["xformOp:translate"]
    }}
"""


# Six boxes stacked on the ground. The scene limits the solver to one
# position iteration, which makes the stack fragile.
SCENE = """#usda 1.0
(
    metersPerUnit = 1
    kilogramsPerUnit = 1
    upAxis = "Z"
)

def Xform "World"
{
    def PhysicsScene "PhysicsScene" (
        prepend apiSchemas = ["PhysxSceneAPI"]
    )
    {
        uniform uint physxScene:maxPositionIterationCount = 1
    }

    def Plane "Ground" (
        prepend apiSchemas = ["PhysicsCollisionAPI"]
    )
    {
        uniform token axis = "Z"
    }
""" + "".join(box(i) for i in range(6)) + "}\n"


def top_box(physx):
    """Return the x, y, z position of the top box."""
    with physx.read_sync(
        SimObjectType.RIGID_BODY, ["position"], pattern="/World/Box5"
    ) as result:
        x, y, z = result.groups[0].tensors[0].numpy()[0]
        return float(x), float(y), float(z)


def simulate(steps_per_second):
    """Simulate 5 s in steps of 1 / steps_per_second s.

    Report how far the top box moved.
    """
    stage = ovstage.Stage("stack")
    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))

    x0, y0, z0 = top_box(physx)
    for _ in range(5 * steps_per_second):
        physx.step_sync(1.0 / steps_per_second)
    x, y, z = top_box(physx)
    sideways = ((x - x0) ** 2 + (y - y0) ** 2) ** 0.5
    print(
        f"1/{steps_per_second} s steps: top box moved {sideways:.3f} m sideways, "
        f"height {z0:.3f} m -> {z:.3f} m"
    )

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


ovstage.population.register_usd_schemas([str(p) for p in physx_schema_paths()])
simulate(steps_per_second=60)
simulate(steps_per_second=240)

It prints:

1/60 s steps: top box moved 4.201 m sideways, height 2.750 m -> 0.250 m
1/240 s steps: top box moved 0.003 m sideways, height 2.750 m -> 2.746 m

With 1/240 s steps, four times as many, the same scene stands. Smaller steps often let a scene keep fewer iterations.

More Detail#