Exploring Robotic Dynamics with MuJoCo: 2-DOF Modeling
In this post, we’re going to dive into a simple simulation of a 2-DOF robot using MuJoCo.
Before we get into complex robot manipulation, it’s essential to understand the fundamentals of how to get a robot to a desired location. We will focus on a straightforward simulation: moving the robotic arm from its current state to a specific target coordinate.
If we know the target coordinates and the robot’s current position, we can calculate exactly how the arm needs to move to reach that point. This method is called Inverse Kinematics (IK).
Conversely, if we already know the specific angles for each joint and want to calculate where the robot’s end-effector will end up, that process is called Forward Kinematics (FK).
In this first scenario, we will explore how to use Inverse Kinematics to calculate the required joint angles within a 2D XY coordinate system. Fortunately, in this planar setup, we can determine the angle for each joint simply by applying the Law of Cosines.
First, let’s establish our known variables: the robot’s base position, the link lengths, and the target coordinates.
For this simulation, we will define the robot’s base at the origin (0,0) and set the length of each link (joint) to 20cm. The target destination is set to x = 10cm and y = 30cm.
- Calculate the distance to the target ($r$)
$$r^2 = x^2+y^2$$
$$ r^2=10^2+30^2=100+900=1000$$
- Solve for Joint 2 (\(\theta_2\)) using the Law of Cosines
$$r^2 = L_1^2 + L_2^2 - 2L_1L_2\cos(180^\circ - \theta_2)$$
$$1000 = 20^2 + 20^2 - 2(20)(20)\cos(\pi - \theta_2)$$
$$1000 = 400 + 400 - 800\cos(\pi - \theta_2)$$
$$200 = -800\cos(\pi - \theta_2)$$
$$\cos(\pi - \theta_2)=-cos(\theta_2) = -0.25$$
$$\theta_2 = \arccos(0.25) \approx \mathbf{1.318 \text{ rad}}$$
- Solve for Joint 1 (\(\theta_1\))
$$\phi = \arctan(\frac{30}{10}) \approx 1.249 \text{ rad}$$
Although we could calculate this angle using arccos(x/r), the cosine function cannot differentiate between “up” (+y) and “down” (-y). It would return the same positive angle for both (10,30) and (10,-30). By using arctan, we preserve the sign information, allowing the robot to navigate the full \(360^\circ\) range.
$$\alpha = \frac{\theta_2}{2} \approx 0.659 \text{ rad}$$
$$\theta_1 = \phi - \alpha$$
$$ \theta_1 = 1.249 - 0.659 \approx \mathbf{0.590 \text{ rad}}$$

The figure above visualizes the calculation process to aid your understanding.

The video above shows a simple simulation of the scenario using MuJoCo. In this demo, I simply hardcoded the calculated values. You can press the Spacebar to start the simulation.
Summary
In this post, we explored the basics of Inverse Kinematics and calculation methods using a simple 2-DOF robot, followed by a simulation in MuJoCo.