Contacts#

Contacts tell your program when objects touch and how hard they push on each other, such as a robot’s foot on the floor or a gripper’s fingers on a part. ovphysx reports them through the same read calls as positions.

A 2 kg box falls onto a static plate. Next to it, a graph shows the contact force on the box. The force stays at zero during the fall and jumps to 488 N for one step when the box lands. Then it settles on a dashed line at the box's weight, 2 kg times 9.81 m/s², which is 19.62 N.

Contact Sensors#

A contact sensor is a body that reports its contacts. The program in Read the Contact Force uses three scene settings:

Scene Setting

Why

PhysxContactReportAPI on the body, or on the prim that holds an articulation

Makes the body, or every link of the articulation, a contact sensor. A body without it has no contact data.

physxContactReport:threshold = 0 on the same prim

Reports even small forces. By default, contact forces below 1 N are not reported, in a scene in meters.

physxScene:disableSleeping = 1 on the PhysicsScene, with PhysxSceneAPI

Keeps bodies awake. A body that lies still falls asleep after a moment, and then its contacts with sleeping or static objects are no longer reported.

Read the Contact Force#

This program, contacts_box_on_plate.py, drops a 2 kg box onto a static plate and prints the contact force on the box every 5 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

# A 2 kg box, 0.5 m wide, falls onto a static plate. The box is a contact sensor.
SCENE = """#usda 1.0
(
    metersPerUnit = 1
    kilogramsPerUnit = 1
    upAxis = "Z"
)

def Xform "World"
{
    def PhysicsScene "PhysicsScene" (
        prepend apiSchemas = ["PhysxSceneAPI"]
    )
    {
        bool physxScene:disableSleeping = 1
    }

    def Cube "Plate" (
        prepend apiSchemas = ["PhysicsCollisionAPI"]
    )
    {
        double size = 1
        double3 xformOp:translate = (0, 0, -0.05)
        float3 xformOp:scale = (2, 2, 0.1)
        uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:scale"]
    }

    def Cube "Box" (
        prepend apiSchemas = ["PhysicsRigidBodyAPI", "PhysicsCollisionAPI",
                              "PhysicsMassAPI", "PhysxContactReportAPI"]
    )
    {
        double size = 0.5
        float physics:mass = 2
        float physxContactReport:threshold = 0
        double3 xformOp:translate = (0, 0, 1)
        uniform token[] xformOpOrder = ["xformOp:translate"]
    }
}
"""


def contact_force(physx):
    """Read the net contact force on every sensor.

    Return the box's z, in newtons.
    """
    with physx.read_sync(
        SimObjectType.CONTACT_SENSOR, ["netNormalContactForce"]
    ) as result:
        forces = result.groups[0].tensors[0].numpy()  # one row per sensor
        return float(forces[0, 2])


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

stage = ovstage.Stage("contacts")
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  contact force z = {contact_force(physx):7.2f} N")
for step in range(1, 61):
    physx.step_sync(1.0 / 60.0)
    if step % 5 == 0:
        force = contact_force(physx)
        print(f"t={step / 60:.2f} s  contact force z = {force:7.2f} N")

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

It prints:

t=0.00 s  contact force z =    0.00 N
t=0.08 s  contact force z =    0.00 N
t=0.17 s  contact force z =    0.00 N
t=0.25 s  contact force z =    0.00 N
t=0.33 s  contact force z =    0.00 N
t=0.42 s  contact force z =  488.11 N
t=0.50 s  contact force z =   19.62 N
t=0.58 s  contact force z =   19.62 N
t=0.67 s  contact force z =   19.62 N
t=0.75 s  contact force z =   19.62 N
t=0.83 s  contact force z =   19.62 N
t=0.92 s  contact force z =   19.62 N
t=1.00 s  contact force z =   19.62 N

Notice the following:

  • A read of SimObjectType.CONTACT_SENSOR returns one row per sensor. netNormalContactForce adds up the forces that press on the sensor where it touches other objects, as an x, y, z vector in newtons, in world coordinates.

  • The force is the one that the sensor receives. The plate pushes the box up, so z is positive.

  • Each value describes the last step, so a read returns zero before the first step.

  • Friction forces, along the touching surfaces, come separately as netFrictionContactForce. Add the two vectors for the total contact force.

Forces From One Object#

The net force adds up the forces from every object that touches the sensor. To see what one object contributes, name it as a filter: pass contacts=[ContactSelection("/World/Box", ["/World/Plate"])] to a read_sync of SimObjectType.CONTACT_SENSOR, and read normalContactForceMatrix. It holds an x, y, z force for each filter object, in the order of the list. ContactSelection comes from ovphysx.types.

In the following run, a 1 kg box drops onto the 2 kg box, and the read uses two filters, the plate and the top box:

The 2 kg box rests on the plate while a 1 kg box drops onto it. Three bars show the read result. The net force on the 2 kg box stays near 19.62 N, while the force from the plate rises to 29.43 N. The force from the top box settles at -9.81 N, after a spike of about 200 N each way at the impact.

At the impact, the plate and the top box each push about 200 N, in opposite directions, yet the net force barely changes. Only the filters show that.

More Detail#

Next: Showing the Simulation