This blog will show you how to install MuJoCo in Windows10 and use VS2019 for development. Besides, a tutorial is given to show how to install HAPTIX.
MuJoCo stands for Multi-Joint dynamics with Contact. It is a physics engine aiming to facilitate research and development in robotics, biomechanics, graphics and animation, machine learning, and other areas where fast and accurate simulation of complex dynamical systems is needed.
For most cases, people use MuJoCo in Ubuntu via MuJoCo-Py. But sometimes, people would like to use that via visual studio. Although one may find some documents about the installation process, only a few provide a step-by-step tutorial. Here, I would like to give you that.
1.Install MuJoCo
a. Go to this address to submit a license request first.
b. You will receive the first email with the following contents:
Thank you for your interest in MuJoCo Pro. Your account is now active. The account number is xxxxxxxxxxxxxxxxxxxxxxxx. You can use it to register your Computer id on our website. An activation key needed to run MuJoCo Pro will be emailed to you automatically after a successful registration.
c. Go back here again to register your computer. Copy your account number from the first email. Download the software to check your computer ID. Then, click the register computer button.
d. You will receive a second email with the following contents:
You have successfully registered your Computer id. The activation key is attached to this email. If you have not already done so, please download and unzip the software, copy the attached file ‘mjkey.txt’ to the ‘bin’ subdirectory, and then you can run the software.
e. Download MuJoCo200 here. Then, unzip the package to C:\Users\your_name\Downloads\mujoco200_win64, for example. Then, create a folder C:\Users\your_name\.mujoco\mujoco200. It is recommended not to use other paths. Copy all contents in C:\Users\your_name\Downloads\mujoco200_win64\mujoco200_win64 (including bin, doc, …) to C:\Users\your_name\.mujoco\mujoco200. Next, copy your ‘mjkey.txt’ to the ‘bin’ subdirectory. Finally, append C:\Users\your_name\.mujoco\mujoco200\bin to your PATH environment variable and reboot the computer.
f. Execute C:\Users\your_name\.mujoco\mujoco200\bin\simulate.exe to see whether the installation is successful.
2.Install VS2019
a. Go to the official address to download the VS2019 Community Version.
b. Install VS2019 with C++ development tools selected.
3.Simple Project
a. Create an empty C++ project.
b. Add C:\Users\your_name\.mujoco\mujoco200\include to the IncludePath.
c. Add C:\Users\zengz\.mujoco\mujoco200\bin to the LibraryPath.
d. Add mujoco200.lib;mujoco200nogl.lib;glfw3.lib; to AdditionalDependencies.
e. Copy C:\Users\zengz\.mujoco\mujoco200\model\humanoid100.xml to the project folder.
f. Copy C:\Users\zengz\.mujoco\mujoco200\bin\mjkey.txt to the project folder. (IMPORTANT!)
g. Create hello.cpp in the project and copy the following code into the file:
/* Copyright © 2018, Roboti LLC
This file is licensed under the MuJoCo Resource License (the “License”).
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.roboti.us/resourcelicense.txt
*/
#include “mujoco.h”
#include “glfw3.h”
#include “stdio.h”
#include “stdlib.h”
#include “string.h”
// MuJoCo data structures
mjModel* m = NULL; // MuJoCo model
mjData* d = NULL; // MuJoCo data
mjvCamera cam; // abstract camera
mjvOption opt; // visualization options
mjvScene scn; // abstract scene
mjrContext con; // custom GPU context
// mouse interaction
bool button_left = false;
bool button_middle = false;
bool button_right = false;
double lastx = 0;
double lasty = 0;
// keyboard callback
void keyboard(GLFWwindow* window, int key, int scancode, int act, int mods)
{
// backspace: reset simulation
if( act==GLFW_PRESS && key==GLFW_KEY_BACKSPACE )
{
mj_resetData(m, d);
mj_forward(m, d);
}
}
// mouse button callback
void mouse_button(GLFWwindow* window, int button, int act, int mods)
{
// update button state
button_left = (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT)==GLFW_PRESS);
button_middle = (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE)==GLFW_PRESS);
button_right = (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT)==GLFW_PRESS);
// update mouse position
glfwGetCursorPos(window, &lastx, &lasty);
}
// mouse move callback
void mouse_move(GLFWwindow* window, double xpos, double ypos)
{
// no buttons down: nothing to do
if( !button_left && !button_middle && !button_right )
return;
// compute mouse displacement, save
double dx = xpos — lastx;
double dy = ypos — lasty;
lastx = xpos;
lasty = ypos;
// get current window size
int width, height;
glfwGetWindowSize(window, &width, &height);
// get shift key state
bool mod_shift = (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT)==GLFW_PRESS ||
glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT)==GLFW_PRESS);
// determine action based on mouse button
mjtMouse action;
if( button_right )
action = mod_shift ? mjMOUSE_MOVE_H : mjMOUSE_MOVE_V;
else if( button_left )
action = mod_shift ? mjMOUSE_ROTATE_H : mjMOUSE_ROTATE_V;
else
action = mjMOUSE_ZOOM;
// move camera
mjv_moveCamera(m, action, dx/height, dy/height, &scn, &cam);
}
// scroll callback
void scroll(GLFWwindow* window, double xoffset, double yoffset)
{
// emulate vertical mouse motion = 5% of window height
mjv_moveCamera(m, mjMOUSE_ZOOM, 0, -0.05*yoffset, &scn, &cam);
}
// main function
char error[1000];
int main(void)
{
// activate MuJoCo
mj_activate(“mjkey.txt”);
// load model from file and check for errors
m = mj_loadXML(“humanoid100.xml”, NULL, error, 1000);
if (!m)
{
printf(“%s\n”, error);
return 1;
}
// make data
d = mj_makeData(m);
// init GLFW
if( !glfwInit() )
mju_error(“Could not initialize GLFW”);
// create window, make OpenGL context current, request v-sync
GLFWwindow* window = glfwCreateWindow(1200, 900, “Demo”, NULL, NULL);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
// initialize visualization data structures
mjv_defaultCamera(&cam);
mjv_defaultOption(&opt);
mjv_defaultScene(&scn);
mjr_defaultContext(&con);
// create scene and context
mjv_makeScene(m, &scn, 2000);
mjr_makeContext(m, &con, mjFONTSCALE_150);
// install GLFW mouse and keyboard callbacks
glfwSetKeyCallback(window, keyboard);
glfwSetCursorPosCallback(window, mouse_move);
glfwSetMouseButtonCallback(window, mouse_button);
glfwSetScrollCallback(window, scroll);
// run main loop, target real-time simulation and 60 fps rendering
while( !glfwWindowShouldClose(window) )
{
// advance interactive simulation for 1/60 sec
// Assuming MuJoCo can simulate faster than real-time, which it usually can,
// this loop will finish on time for the next frame to be rendered at 60 fps.
// Otherwise add a cpu timer and exit this loop when it is time to render.
mjtNum simstart = d->time;
while( d->time — simstart < 1.0/60.0 )
mj_step(m, d);
// get framebuffer viewport
mjrRect viewport = {0, 0, 0, 0};
glfwGetFramebufferSize(window, &viewport.width, &viewport.height);
// update scene and render
mjv_updateScene(m, d, &opt, NULL, &cam, mjCAT_ALL, &scn);
mjr_render(viewport, &scn, &con);
// swap OpenGL buffers (blocking call due to v-sync)
glfwSwapBuffers(window);
// process pending GUI events, call GLFW callbacks
glfwPollEvents();
}
//free visualization storage
mjv_freeScene(&scn);
mjr_freeContext(&con);
// free MuJoCo model and data, deactivate
mj_deleteData(d);
mj_deleteModel(m);
mj_deactivate();
// terminate GLFW (crashes with Linux NVidia drivers)
#if defined(__APPLE__) || defined(_WIN32)
glfwTerminate();
#endif
return 1;
}
h. Choose the Debug x64 option.
i. Build the project before running.
j. Run the program.
3. Install HAPTIX
a. Download HAPTIX150 here.
b. Unzip it anywhere.
c. Run mjhaptix150\program\mjhaptix.exe and enjoy it.