Search code examples
opengl-3glm-mathbulletphysics

Getting bullet physics transform matrix for Opengl


As of now I am using the below code to get the transform matrix from my rigid body in bullet and apply it to my instance. Now right now it seems to not be updating my rendered cube's transform, my first though is to believe that I'm losing data when creating a glm mat4. So my question is am I converting the data correctly to transform my matrix?

for (int i = 0; i < WoodenCrateInstances.size(); i++)
{
    btTransform t;
    WoodenCrateInstances.at(i).asset->body->getMotionState()->getWorldTransform(t);
    float mat[16];
    t.getOpenGLMatrix(mat);
    glm::vec3 vec = glm::make_vec3(mat);
    WoodenCrateInstances.at(i).transform = glm::translate(glm::mat4(), vec);
}

Solution

  • If you want the full transform, you should do:

    btTransform t;
    
    // Get the transform from Bullet and into 't'
    WoodenCrateInstances.at(i).asset->body->getMotionState()->getWorldTransform(t);
    
    // Convert the btTransform into the GLM matrix using 'glm::value_ptr'
    t.getOpenGLMatrix(glm::value_ptr(WoodenCrateInstances.at(i).transform));
    

    As you only create a translation matrix at the moment, you loose orientation/rotation, and eventually scale.

    Also note, that the matrices returned from Bullet is in world space, so if you have a scene hierachy/graph with the matrices stored as relative transforms to the node's parent node, you might also want to transform the matrix into local space, if needed.