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.

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 |
|---|---|
|
Makes the body, or every link of the articulation, a contact sensor. A body without it has no contact data. |
|
Reports even small forces. By default, contact forces below 1 N are not reported, in a scene in meters. |
|
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_SENSORreturns one row per sensor.netNormalContactForceadds 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:

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#
Contact Sensors lists every contact attribute, including contact points and contact events.
Contact Queries explains filters in full.
Contact Sensor Attributes gives the layout of each attribute.
PhysxContactReportAPI describes the schema and its settings.
Next: Showing the Simulation