The Physics Scene#

The PhysicsScene prim holds the settings that apply to the whole simulation, such as gravity. This page covers gravity, units, the time step, and the few scene settings worth knowing early.

Two 1 m boxes start with their centers 2 m above the ground, one with Earth gravity and one with Moon gravity. A height label follows each box. The Earth box lands before 0.75 s, and the Moon box lands between 1.25 s and 1.5 s. Next to them, the program prints both heights every 0.25 s.

Gravity#

Two attributes of the PhysicsScene set gravity:

Attribute

Meaning

Default

physics:gravityDirection

The direction gravity pulls in.

Down the up axis, such as (0, 0, -1) when Z is up.

physics:gravityMagnitude

The strength, in distance per second squared.

9.81 m/s², converted to the stage’s units.

A scene without any PhysicsScene gets the same defaults.

The following program, physics_scene_gravity.py, drops the same box twice: once with Earth gravity and once with Moon gravity, 1.62 m/s².

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 1 m box whose center starts 2 m above the ground.
# GRAVITY is replaced by a number before the scene is loaded.
SCENE = """#usda 1.0
(
    metersPerUnit = 1
    kilogramsPerUnit = 1
    upAxis = "Z"
)

def Xform "World"
{
    def PhysicsScene "PhysicsScene"
    {
        vector3f physics:gravityDirection = (0, 0, -1)
        float physics:gravityMagnitude = GRAVITY
    }

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

    def Cube "Box" (
        prepend apiSchemas = ["PhysicsRigidBodyAPI", "PhysicsCollisionAPI"]
    )
    {
        double size = 1
        double3 xformOp:translate = (0, 0, 2)
        uniform token[] xformOpOrder = ["xformOp:translate"]
    }
}
"""


def box_height(physx):
    """Read the position of every rigid body and return the box's z."""
    with physx.read_sync(SimObjectType.RIGID_BODY, ["position"]) as result:
        return float(result.groups[0].tensors[0].numpy()[0, 2])


def drop_box(gravity):
    """Simulate 1.5 s with this gravity and return the height every 0.25 s."""
    stage = ovstage.Stage("drop")
    scene = SCENE.replace("GRAVITY", str(gravity))
    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))

    heights = [box_height(physx)]
    for step in range(1, 91):
        physx.step_sync(1.0 / 60.0)
        if step % 15 == 0:
            heights.append(box_height(physx))

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


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

earth = drop_box(9.81)
moon = drop_box(1.62)
print("time    Earth    Moon")
for i, (earth_z, moon_z) in enumerate(zip(earth, moon)):
    print(f"{i * 0.25:.2f} s  {earth_z:.3f} m  {moon_z:.3f} m")

It prints:

time    Earth    Moon
0.00 s  2.000 m  2.000 m
0.25 s  1.692 m  1.949 m
0.50 s  0.771 m  1.797 m
0.75 s  0.500 m  1.544 m
1.00 s  0.500 m  1.189 m
1.25 s  0.500 m  0.733 m
1.50 s  0.500 m  0.500 m

Gravity belongs to the scene, so each drop loads its own scene.

Units and Up Axis#

Three settings at the top of the USD file, called layer metadata, set the units and the up direction:

Metadata

Meaning

These Guides Use

metersPerUnit

The length of one scene unit, in meters.

1, for meters

kilogramsPerUnit

The mass of one scene unit, in kilograms.

1, for kilograms

upAxis

The axis that points up, "Y" or "Z".

"Z"

ovphysx reads every length and mass in these units, scales its defaults to them, and returns results in them. For example, a stage in centimeters, with metersPerUnit = 0.01, gets a default gravity of 981 cm/s².

The Time Step#

Your program sets the time step, not the scene. Each step(dt) or step_sync(dt) call moves the simulation forward by dt seconds. These guides use 1/60 s. Stable Simulations shows when smaller steps help.

Settings You Can Change#

PhysX-specific scene settings live on PhysxSceneAPI. To use one, apply the API schema to the PhysicsScene prim and set the value there. For example, this complete scene file, cpu_scene.usda, runs on the CPU:

#usda 1.0
(
    metersPerUnit = 1
    kilogramsPerUnit = 1
    upAxis = "Z"
)

def Xform "World"
{
    def PhysicsScene "PhysicsScene" (
        prepend apiSchemas = ["PhysxSceneAPI"]
    )
    {
        bool physxScene:enableGPUDynamics = 0
        uniform token physxScene:broadphaseType = "MBP"
    }
}

These are the settings you are most likely to change:

Setting

Default

What It Does

physxScene:enableGPUDynamics

true

Simulates the scene on the GPU when one is available. To run the scene on the CPU, set it to false and set physxScene:broadphaseType to "MBP", the CPU broad phase (multi-box pruning).

physxScene:minPositionIterationCount and physxScene:maxPositionIterationCount

1 and 255

The fewest and most passes the solver makes over each body’s position in a step. The solver is the part of PhysX that works out how bodies move and push on each other. Refer to Stable Simulations.

physxScene:disableSleeping

false

Sleeping stops simulating a body at rest until another body touches it or your program changes it. Set it to true to keep every body awake.

To use the CPU for every scene in a process, refer to CPU and GPU.

More Detail#