Graphical mesh lag behind collision shape in DebugDraw

Post Reply
User avatar
manmohan29
Posts: 15
Joined: Wed Aug 20, 2014 9:26 pm
Location: Chandigarh, India

Graphical mesh lag behind collision shape in DebugDraw

Post by manmohan29 » Mon May 08, 2017 1:26 pm

When we Debug draw the physics world, Is it normal that moving rigid bodies will have their graphical mesh lagging behind collision shape ?

I setup a simple physics world with one box as ground set at (0, 0, 0) and another as a 10Kg box which starts falling from (0, 300, 0)
What I am noticing is that when velocity for the falling box increases, the graphical mesh lags behind the collision shape drawn by btDebugDrawer.
falling.png
Falling box with graphical mesh lagging the collision shape.
falling.png (7.55 KiB) Viewed 931 times
Collision shape and Graphical mesh align perfectly when at rest (after colliding with ground box)
atrest.png
box at rest have collision shape and graphical mesh perfectly aligned.
atrest.png (7.85 KiB) Viewed 931 times
I am trying to debug draw my world by using this code:

Code: Select all

void
    CDebugDrawer::drawLine(const btVector3 &from, const btVector3 &to, const btVector3 &color) {
        drawLine(from, to, color, color);
    }

    void
    CDebugDrawer::drawLine(const btVector3 &from, const btVector3 &to, const btVector3 &fromColor,
                           const btVector3 &toColor) {
        this->m_Lines.push_back(LINE(glm::vec3(from.getX(), from.getY(), from.getZ()),
                                      glm::vec3(to.getX(), to.getY(), to.getZ())));
        this->m_Colors.push_back(COLOR(glm::vec3(fromColor.getX(), fromColor.getY(), fromColor.getZ()),
                                        glm::vec3(toColor.getX(), toColor.getY(), toColor.getZ())));
    }
...
...

void CDebugDrawer::doDrawing() {

        glBindBuffer(GL_ARRAY_BUFFER, pVBO);
        glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr) (this->m_Lines.size() * sizeof(LINE)),
                     &this->m_Lines[0], GL_STATIC_DRAW);
        glVertexAttribPointer(ATTRIB_POS, 3, GL_FLOAT, GL_FALSE, 0, 0);
        glEnableVertexAttribArray(ATTRIB_POS);

        glBindBuffer(GL_ARRAY_BUFFER, cVBO);
        glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr) (this->m_Colors.size() * sizeof(COLOR)),
                     &this->m_Colors[0], GL_STATIC_DRAW);
        glVertexAttribPointer(ATTRIB_COL, 3, GL_FLOAT, GL_FALSE, 0, 0);
        glEnableVertexAttribArray(ATTRIB_COL);

        glDrawArrays(GL_LINES, 0, this->m_Lines.size() * 2);

        glBindBuffer(GL_ARRAY_BUFFER, 0);

        // clean debug draw data for next frame
        cleanDrawing();

    }

    void CDebugDrawer::cleanDrawing() {
        this->m_Lines.clear();
        this->m_Colors.clear();
    }

Code: Select all

void CMyPhysicsWorld::StepPhysics() {
    // Step physics simulation @ 60 FPS
    // TODO: make this framerate independent, read http://bulletphysics.org/mediawiki-1.5.8/index.php/Stepping_the_World
    this->m_DynamicsWorld->stepSimulation(1 / 60.0f, 10);

    // get world transforms and update OpenGL scene object's matrices
    btTransform trans;
    this->m_RigidBodies.at("GroundMesh")->m_MotionState->getWorldTransform(trans);
    glm::mat4 ATTRIBUTE_ALIGNED16(gMR);
    trans.getOpenGLMatrix(glm::value_ptr(gMR));
    this->m_RigidBodies.at("GroundMesh")->GetGraphicalObject()->Model = gMR;

    this->m_RigidBodies.at("CubeMesh")->m_MotionState->getWorldTransform(trans);
    trans.getOpenGLMatrix(glm::value_ptr(gMR));
    this->m_RigidBodies.at("CubeMesh")->GetGraphicalObject()->Model = gMR;

    if (this->m_DebugDraw) {
        this->m_DynamicsWorld->debugDrawWorld();
    }
}

Code: Select all

void CScene_Main::Render() {
    this->m_PW->StepPhysics();

    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // black

    GLuint p = Renu3D::Engine::getInstance()->renderer.pO;
    glUseProgram(p);

    glm::mat4 MVP(1.0f);
    glm::mat4 V = glm::mat4(1.0f);
    glm::mat4 P = glm::mat4(1.0f);

    V = glm::lookAt(
            glm::vec3(25.0f, 200.0f, 90.0f),
            glm::vec3(0.0f, 30.0f, 0.0f),
            glm::vec3(0, 1, 0)
    );
    P = Renu3D::Engine::getInstance()->renderer.m_Projection;

    MVP = P * V * this->m_Objects.at("cube")->m_Meshes.at("CubeMesh")->Model;
    GLint mvp = Renu3D::Engine::getInstance()->renderer.mvp;

    glUniformMatrix4fv(mvp, 1, GL_FALSE, glm::value_ptr(MVP));

    // set data for graphical objects and draw

    glDrawElements(GL_TRIANGLES, (GLsizei) i.size(), GL_UNSIGNED_SHORT, (GLvoid *) 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    // Debug draw Physics world
    if (this->m_PW->m_DebugDraw) {
        MVP = P * V * glm::mat4(1.0f);
        glUniformMatrix4fv(mvp, 1, GL_FALSE, glm::value_ptr(MVP));
        this->m_PW->m_DebugDrawer.doDrawing();

    }

    glUseProgram(0);
}

erbisme4@yahoo.com
Posts: 41
Joined: Fri Apr 29, 2016 2:41 pm

Re: Graphical mesh lag behind collision shape in DebugDraw

Post by erbisme4@yahoo.com » Wed May 10, 2017 1:55 pm

This is natural as you update the graphical representation of the cube from the actual rigid body.

Post Reply