Recursive AI

Simulate a URDF robot in C#

OnePhysics is a free, open-source physics library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

arm.urdf in the download defines two links joined at an elbow.

Terminal
dotnet add package RecursiveAI.OnePhysics.Interchange

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OnePhysics.Interchange;
using RecursiveAI.OnePhysics.Physics3D;
 
var text = File.ReadAllText("arm.urdf");
var robot = UrdfImporter.Import(text);
var world = new PhysicsWorld3D();
var arm = world.AddArticulation(
    robot.Definition, at: default); // World origin
var link = robot.FindLink("forearm");
Console.WriteLine($"Forearm link: {link}");
foreach (var warning in robot.Warnings)
    Console.WriteLine(warning.Code);
arm.SetJointVelocity(link, 1f);
for (int frame = 0; frame < 30; frame++)
    world.Step(1f / 60f);
Console.WriteLine(FormattableString.Invariant(
    $"Elbow angle after 30 steps: {arm.GetJointPosition(link):F2} rad"));

Unzip the example, then dotnet run.

The snippet spins the elbow, steps the simulation and prints the resulting angle:

Text
Forearm link: 1
mesh-unresolved
Elbow angle after 30 steps: 0.50 rad

arm.urdf is in the download:

XML
<robot name="bench_arm">
  <link name="base"/>
  <link name="forearm">
    <collision>
      <origin xyz="0.25 0 0"/>
      <geometry><box size="0.5 0.1 0.1"/></geometry>
    </collision>
    <collision>
      <geometry><mesh filename="guard.stl"/></geometry>
    </collision>
  </link>
  <joint name="elbow" type="revolute">
    <parent link="base"/><child link="forearm"/>
    <origin xyz="0 0 0.4"/><axis xyz="0 1 0"/>
    <limit lower="-1.5" upper="1.5" effort="10" velocity="2"/>
  </joint>
</robot>

The box in the sample is the forearm’s collider. The second collision element refers to guard.stl, deliberately not included: the importer reports it and omits that shape.

A file with a broken link tree, such as a joint naming a nonexistent parent, throws InterchangeException; a lookup for an absent link on a successfully imported robot returns -1.

Variations

Let the robot’s base move. Replace the import line with this version when the robot’s base should not be fixed to the world:

C#
var robot = UrdfImporter.Import(text,
    new UrdfOptions { FixedBase = false });

Console.WriteLine($"Fixed base: {robot.Definition.FixedBase}"); then prints:

Text
Fixed base: False

The short answer

Read the .urdf file as text and call UrdfImporter.Import, then pass robot.Definition to PhysicsWorld3D.AddArticulation. That adds the linked bodies and their joints. Use FindLink with the child link name (here forearm, not the joint name elbow) when reading or setting the elbow’s movement. Inspect the import warnings even when the robot loads.

What is URDF in robotics?

The Unified Robot Description Format is XML for describing the parts of a robot and how they connect. A link is a rigid part of the model. A joint attaches a child link to its parent; the joint type and axis say what motion is allowed. OnePhysics reads lengths in meters, masses in kilograms and angles in radians. It supports fixed, revolute, continuous and prismatic joints. For example, the sample elbow turns about its local y axis, bounded between −1.5 and 1.5 radians. A continuous joint can turn without angle limits.

The imported joint limits aren’t a position controller. Effort and velocity limits carry over, but the importer does not set stiffness or damping on the position drive. The full example sets an initial joint velocity to demonstrate stepping without configuring a drive to hold a target. To command a target angle, set up a drive separately.

What’s the difference between visual and collision geometry in URDF?

Visual geometry describes what to draw. Collision geometry defines what the simulation can hit. OnePhysics reads <collision> elements and ignores <visual> elements; a model that looks complete in a viewer might still lack contact shapes in the physics world. Collision boxes, spheres and cylinders can be imported directly. For a collision mesh, supply it through UrdfOptions.Meshes: your code resolves the filename and returns vertices and triangle indices, and OnePhysics uses the convex hull after applying the mesh scale. Without a resolved mesh, the importer leaves it out and adds mesh-unresolved. The URDF importer specification lists the other mappings and warnings, including mimic joints and planar joints.

All onephysics recipes