"""Contact detection and force sensing — modern MuJoCo example."""
import numpy as np
import mujoco
import mujoco.viewer
XML = """
"""
# Simpler version without sensor complications
SIMPLE_XML = """
"""
def main():
model = mujoco.MjModel.from_xml_string(SIMPLE_XML)
data = mujoco.MjData(model)
print("Simulating box dropping onto table...")
print(f"{'Time':>6s} {'Height':>8s} {'Contacts':>10s} {'Force':>10s}")
print("-" * 40)
for step in range(2000):
mujoco.mj_step(model, data)
# Check contacts every 100 steps
if step % 100 == 0:
# Read contact information
n_contacts = data.ncon
total_force = 0.0
for i in range(n_contacts):
contact = data.contact[i]
# Get contact force
force = np.zeros(6)
mujoco.mj_contactForce(model, data, i, force)
total_force += np.linalg.norm(force[:3])
box_height = data.qpos[2]
print(f"{data.time:6.3f} {box_height:8.4f} {n_contacts:10d} {total_force:10.2f}")
print("\nDone! The box has settled on the table.")
print(f"Final box height: {data.qpos[2]:.4f}")
if __name__ == "__main__":
main()