opensim-org / opensim-core

SimTK OpenSim C++ libraries and command-line applications, and Java/Python wrapping.
https://opensim.stanford.edu
Apache License 2.0
758 stars 308 forks source link

MOTION ANIMATION INVERSE KINEMATICS #3734

Closed Giopestri01 closed 3 months ago

Giopestri01 commented 3 months ago

Good morning, Can anyone help me about seeing the Inverse Kinematics motion from a .mot file in a cpp project? I've perfomed the IK: void performIK(Model& model, const string& setupFilePath) { InverseKinematicsTool ikTool(setupFilePath); ikTool.run(); } and then I've tried to visualize the model moving: void visualizeAndAnimateModel(const string& modelFilePath, const string& motionFilePath) { try { Model model(modelFilePath); model.setUseVisualizer(true);

    // Inizializza il modello e il visualizzatore
    SimTK::State& state = model.initSystem();

    // Carica il file .mot come TimeSeriesTable
    auto motionData = TimeSeriesTable(motionFilePath);

    // Converti TimeSeriesTable in una StatesTrajectory
    auto statesTrajectory = StatesTrajectory::createFromStatesTable(model, motionData);

    // Ottieni il visualizzatore
    auto& viz = model.updVisualizer().updSimbodyVisualizer();

    // Ciclo per animare il modello attraverso la traiettoria di stati
    for (const auto& st : statesTrajectory) {
        model.realizePosition(st); // Aggiorna la posizione del modello
        viz.drawFrameNow(st);      // Disegna il frame attuale
        std::this_thread::sleep_for(std::chrono::milliseconds(33)); // Regola la velocità dell'animazione
    }

    cout << "Animazione completata. Premi Invio per uscire..." << endl;
    cin.get();
}
catch (const std::exception& ex) {
    cerr << "Errore durante la visualizzazione del modello: " << ex.what() << endl;
}

} but I'm not able to see it. If anyone could help me, please let me know :)

nickbianco commented 3 months ago

Hi @Giopestri01,

Could you try VisualizerUtilities::showMotion as an alternative?

Unless there is a bug in OpenSim to report here (that is not related to the Simbody visualizer), please reserve these types of posts for the OpenSim forum.