Recursive AI

Control an articulated robot arm in C#

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

Steer a simulated robot arm toward a tool-point target.

Terminal
dotnet add package RecursiveAI.OnePhysics

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

C#
using RecursiveAI.OneMath;
using RecursiveAI.OnePhysics.Physics3D;
 
var world = new PhysicsWorld3D();
var arm = world.AddArticulation(
    ArticulationDef.RobotArm(0.9f, 7, 8f));
var tool = new Vector3F(0, 0.1286f, 0);
arm.SetTaskTarget(arm.LinkCount - 1, tool,
    new Vector3D(0.3, 0.5, 0.3));
for (int i = 0; i < 300; i++)
    world.Step(1f / 60f);
var tip = arm.GetPointPosition(7, tool);
Console.WriteLine($"{tip.X:F3}, " +
    $"{tip.Y:F3}, {tip.Z:F3}");
Text
0.300, 0.500, 0.300

Unzip the example, then dotnet run.

The full program

The snippet at the top creates a fixed-base, seven-joint arm and steers its tool point. The full program adds a static floor and reports the distance from the target after 300 steps. All geometry is generated in code; no robot asset or renderer is required.

tool is a point in the last link’s local coordinates, while goal is in world coordinates. The 0.02 m tolerance is this example’s acceptance threshold, not a library-wide accuracy guarantee. The preset includes a base link, so seven joints produce eight links.

C#
using RecursiveAI.OneMath;
using RecursiveAI.OnePhysics;
using RecursiveAI.OnePhysics.Physics3D;
 
var world = new PhysicsWorld3D();
world.AddBox(new Vector3F(20, 1, 20),
    at: new Vector3D(0, -0.5, 0), type: BodyType.Static);
var arm = world.AddArticulation(
    ArticulationDef.RobotArm(0.9f, 7, 8f), new Vector3D(0, 0, 0));
var tool = new Vector3F(0, 0.1286f, 0);
var goal = new Vector3D(0.3, 0.5, 0.3);
arm.SetTaskTarget(arm.LinkCount - 1, tool, goal);
for (int i = 0; i < 300; i++)
    world.Step(1f / 60f);
var tip = arm.GetPointPosition(arm.LinkCount - 1, tool);
double error = (tip - goal).Length();
Console.WriteLine($"Links: {arm.LinkCount}");
Console.WriteLine($"Tool: {tip.X:F4}, {tip.Y:F4}, {tip.Z:F4}");
Console.WriteLine($"Error: {error:F4} m");
Console.WriteLine($"Within 0.02 m: {error <= 0.02}");

dotnet run prints:

Text
Links: 8
Tool: 0.3000, 0.5000, 0.3000
Error: 0.0000 m
Within 0.02 m: True

An unreachable or obstructed task target may not converge: SetTaskTarget is a local solver, so check the measured error and route the tool through reachable intermediate targets.

Variations

After measuring the tool point in the full program, request the joint torques that would hold its present pose against gravity:

C#
var holding = new double[arm.VelocityCount];
arm.ComputeGravityCompensation(holding);
Console.WriteLine($"Joint torque entries: {holding.Length}");

This appends:

Text
Joint torque entries: 7

Connect bodies with joints · Snapshot and rollback · 3D physics


The short answer

Create an arm with ArticulationDef.RobotArm, add it to PhysicsWorld3D, and call SetTaskTarget for a point on its last link. Step the world before reading GetPointPosition. This steers a physically simulated articulation, not just a rendered pose. The articulation guide specifies how targets, drives, contacts, and saved state work.

The sample reaches the printed target after 300 steps of 1/60 s. It also reports the position error against an explicit 0.02 m tolerance. Keep an error check in application control logic rather than assuming every target will be reachable because this one is.

How do I simulate an articulated robot arm in C#?

An articulation holds its links in joint coordinates. The links form a tree; the library’s guide describes this representation as preventing the joints from coming apart. Use it for mechanisms such as robot manipulators or long linked machines where independent joint constraints are not the representation you want.

The preset creates the example’s arm without a model file. For a custom definition, add links with parents first, then specify each link’s mass properties, joint kind and axis, joint origin, limits, drive, and collision shapes. The definition maps those concepts to the same members used by the importers.

Each link is also an ordinary kinematic body in the world. Its shapes participate in collisions, queries, events, and debugging. Do not reposition a link body directly to steer the arm. Change joint coordinates, forces, or targets so the articulation stays responsible for the link transforms.

The ground in the full program is a static box. It is not part of the articulation, and adding it does not turn this console sample into a rendered robot viewer. Read the computed coordinates or connect the resulting transforms to your own display.

How does inverse kinematics move a robotic arm?

A task-space target asks for a point on a link to reach a world position. SetTaskTarget moves the targets of joints that have position drives. The local tool offset matters: targeting the link’s origin is not the same as targeting the end of a tool extending from it.

Contacts still matter while those joints move. The guide says the tool must approach from a reachable direction; this is a local solver, not a global route planner. For a target behind an obstacle, first choose an intermediate target above or around it, then approach the destination. Raising the frame count alone does not provide a path around every obstacle.

Drives use stiffness, damping, and a maximum force. The guide describes them as implicitly integrated PD controllers. A weak drive cannot push an arbitrarily heavy object just because the target lies beyond it. Task-space steering therefore belongs inside a measured feedback loop with a position tolerance, not a one-time assumption that the requested point has been reached.

For joint-space control, SetJointTarget(link, position) changes a drive’s target. ComputeGravityCompensation returns the joint torques that hold the present pose; the variation allocates one entry per velocity coordinate. ComputeMassMatrix is also available for control code that needs the joint-space mass matrix. These are dynamics inputs, not a second tool-position API.

The library includes articulations in snapshots, hashes, forks, replays, regions, and origin shifts. Save the world state rather than only copying link transforms when you need to resume the physical mechanism. The guide also states what this representation does not model: closed loops, mimic joints, and sleeping. A mechanism requiring one of those features needs a different supported representation rather than treating the tree preset as a general-purpose robot importer.

All onephysics recipes