Running Mujoco in C++

Discussion in 'Simulation' started by Amin, Nov 3, 2017.

  1. Hi

    I used the sample code from the documentation page for simulating and rendering humanoid example. But when I run it, it's running in slow motion mode. Am I doing something wrong? (I'm running the code in release mode).

    Here's the code:
    Code:
    #include "mujoco.h"
    #include "glfw3.h"
    #include "stdio.h"
    #include "string.h"
    
    char error[1000];
    mjModel* m;
    mjData* d;
    
    mjvCamera cam;                      // abstract camera
    mjvPerturb pert;                    // perturbation object
    mjvOption opt;                      // visualization options
    mjvScene scn;                       // abstract scene
    mjrContext con;                     // custom GPU context
    
    int main(void)
    {
        // activate MuJoCo Pro
        mj_activate("mjkey.txt");
    
        // load model from file and check for errors
        m = mj_loadXML("humanoid.xml", NULL, error, 1000);
        if( !m )
        {
            printf("%s\n", error);
            return 1;
        }
    
        // make data corresponding to model
        d = mj_makeData(m);
    
        glfwInit();
        GLFWwindow* window = glfwCreateWindow(800, 600, "Demo", NULL, NULL);
        glfwMakeContextCurrent(window);
        glfwSwapInterval(1);
    
        // initialize visualization data structures
        mjv_defaultCamera(&cam);
        mjv_defaultPerturb(&pert);
        mjv_defaultOption(&opt);
        mjr_defaultContext(&con);
        mjv_makeScene(&scn, 1000);                     // space for 1000 objects
        mjr_makeContext(m, &con, mjFONTSCALE_100);     // model-specific context
    
    
        // get framebuffer viewport
        mjrRect viewport = {0, 0, 0, 0};
        glfwGetFramebufferSize(window, &viewport.width, &viewport.height);
    
        // run simulation for 10 seconds
        while(d->time< 5)
        {
            mj_step(m, d);
    
            // update scene and render
            mjv_updateScene(m, d, &opt, &pert, &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();
        }
    
        // close GLFW, free visualization storage
        glfwTerminate();
        mjv_freeScene(&scn);
        mjr_freeContext(&con);
    
        // free model and data, deactivate
        mj_deleteData(d);
        mj_deleteModel(m);
        mj_deactivate();
    
        return 0;
    }
     
  2. Emo Todorov

    Emo Todorov Administrator Staff Member

    When you call glfwSwapBuffers, the video driver makes you wait for 1/60 sec. The simulation time step is smaller than that, so yes, your code is simulating in slow motion. To get around this, you need to call mj_step repeatedly, and make sure the simulation time advances about as much as 1/FPS. The code sample basic.cpp illustrates this.