Rigid Bodies#

A rigid body is an object that moves but never bends, such as a box or a wheel. Objects that keep their shape come in three kinds, and each kind is moved by something different.

Side view. A blue kinematic platform floats 1 m above the static ground and slides from x = 1 m to x = 2 m and back. A green dynamic box rides on it. A band at the bottom shows the write your program makes before every step, and the program prints the x position of the platform and the box every second.

Three Kinds of Objects#

Kind

In USD

What Moves It

Static collider

PhysicsCollisionAPI only

Nothing. It never moves.

Dynamic body

PhysicsRigidBodyAPI and PhysicsCollisionAPI

Physics: gravity, contacts, joints, and forces.

Kinematic body

The same API schemas, plus physics:kinematicEnabled = true

Your program. Gravity and contacts do not move it, but it moves the dynamic bodies it touches.

A rigid body moves together with every prim below it in the scene. Its colliders give it a shape: a body without a collider falls through everything. Colliders and Materials covers shapes.

Mass#

If the scene sets no mass, ovphysx computes a dynamic body’s mass from the volume of its colliders and a density. It uses the density of the body’s material if the material sets one, or else 1,000 kg/m³, the density of water. To choose the mass yourself, apply PhysicsMassAPI to the body and set physics:density or physics:mass:

You Set

ovphysx Uses

Mass of a 0.5 m Cube

Nothing, and no material density

Volume times 1,000 kg/m³

125 kg

physics:density

Volume times your density

62.5 kg for a density of 500

physics:mass

Your mass

2 kg for a mass of 2

If both are set, ovphysx uses physics:mass. A dynamic body with no collider and no mass set gets a mass of 1 in the scene’s mass unit, 1 kg in these guides, and ovphysx logs a warning.

Starting Velocity#

To start a body moving, set physics:velocity, in distance per second, and physics:angularVelocity, in degrees per second. Both use world coordinates. Reading Values lists the units that reads return.

Moving a Kinematic Body#

To move a kinematic body, write a kinematic target before each step: the position the body must reach at the end of that step. ovphysx moves the body there during the step, at the speed that takes, so the body carries what rests on it. The following program, rigid_bodies_kinematic.py, slides a platform back and forth with a box on it:

The imports and the scene, in USD
import math

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 static ground, a kinematic platform 1 m above it,
# and a dynamic box on the platform.
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 Cube "Platform" (
        prepend apiSchemas = ["PhysicsRigidBodyAPI", "PhysicsCollisionAPI"]
    )
    {
        bool physics:kinematicEnabled = true
        double size = 1
        double3 xformOp:translate = (1, 0, 1)
        double3 xformOp:scale = (2, 1, 0.2)
        uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:scale"]
    }

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


def print_x(physx, t):
    """Print the x position of the platform and of the box."""
    x = {}
    with physx.read_sync(SimObjectType.RIGID_BODY, ["position"]) as result:
        for group in result.groups:
            names = result.dictionary.get_path_strings(group.prim_list)
            for name, position in zip(names, group.tensors[0].numpy()):
                x[name] = float(position[0])
    platform_x, box_x = x["/World/Platform"], x["/World/Box"]
    print(f"t={t:.0f} s  platform x = {platform_x:.2f} m  box x = {box_x:.2f} m")


def move_platform(physx, x):
    """Write where the platform must be at the end of the next step."""
    with physx.write_sync(
        SimObjectType.RIGID_BODY,
        ["kinematicTargetPosition"],
        pattern="/World/Platform",
    ) as session:
        for group in session.groups:
            group.tensors[0].assign([[x, 0.0, 1.0]])
            session.commit_sync(group)


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

stage = ovstage.Stage("platform")
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))

# Slide the platform from x = 1 m to 2 m and back in 4 s, one target per step.
print_x(physx, 0)
for step in range(1, 241):
    t = step / 60.0
    move_platform(physx, 1.5 - 0.5 * math.cos(math.pi * t / 2.0))
    physx.step_sync(1.0 / 60.0)
    if step % 60 == 0:
        print_x(physx, t)

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

It prints:

t=0 s  platform x = 1.00 m  box x = 1.00 m
t=1 s  platform x = 1.50 m  box x = 1.50 m
t=2 s  platform x = 2.00 m  box x = 2.00 m
t=3 s  platform x = 1.50 m  box x = 1.50 m
t=4 s  platform x = 1.00 m  box x = 1.00 m

Notice the following:

  • The write selects the platform alone with pattern. Writing Values explains writes.

  • The box moves with the platform because friction carries it.

  • Without a new target, the platform stops where it is. kinematicTargetOrientation turns a kinematic body in the same way.

Do not move a kinematic body by writing position. The body then jumps to the new place, so nothing rides along:

Two copies of the moving platform. On the left, the program writes kinematicTargetPosition and the box rides along. On the right, the program writes position, so the platform jumps to each new place without carrying the box, which stays where it was.

Kinematic targets work in the default mode and on the CPU, but not with DirectGPU. Refer to CPU and GPU.

More Detail#