如何绘制/渲染子弹物理碰撞体/形状?

问题描述 投票:0回答:2

我已经使用 NDK 将 Bullet 物理引擎实现到我的 android 程序中(我正在使用 Vuforia's imagetarget for android 示例),并且它已设置并正常工作,但是我想渲染/绘制我的碰撞框/平面要查看我的刚体 (btRigidBody)/碰撞形状 (btCollisionShape),我确信这是可能的,但我找不到任何有关如何执行此操作的教程!

我已经在他们的wiki页面上获取了hello world Bullet物理教程并对其进行了修改,以将下落物理体的转换应用到我在opengl es 2.0中查看碰撞体的3d对象,这是我的代码用于渲染对象:

void drawRigidBody(btRigidBody* body,QCAR::Matrix44F modelViewMatrix, unsigned int textureID)
{
btTransform trans;
body->getMotionState()->getWorldTransform(trans);
    LOG("sphere pos: (x %f , y %f, z %f)",trans.getOrigin().getX(),trans.getOrigin().getY(),trans.getOrigin().getZ());


    float physicsMatrix[16];
    trans.getOpenGLMatrix(physicsMatrix);

    SampleUtils::scalePoseMatrix(kObjectScale, kObjectScale, kObjectScale,
            &modelViewMatrix.data[0]);

    QCAR::Matrix44F modelViewProjection, objectMatrix;
    SampleUtils::multiplyMatrix(&modelViewMatrix.data[0], physicsMatrix, &objectMatrix.data[0]);
    SampleUtils::multiplyMatrix(&projectionMatrix.data[0], &objectMatrix.data[0], &modelViewProjection.data[0]);



    glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0,
            (const GLvoid*) &signVerts[0]);
    glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0,
            (const GLvoid*) &signNormals[0]);
    glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,
            (const GLvoid*) &signTexCoords[0]);

    glEnableVertexAttribArray(vertexHandle);
    glEnableVertexAttribArray(normalHandle);
    glEnableVertexAttribArray(textureCoordHandle);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, textureID);
    glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
            (GLfloat*)&modelViewProjection.data[0] );
    glDrawArrays(GL_TRIANGLES, 0, signNumVerts);
}

编辑:查看 btBoxShape 的代码,我注意到你可以抓住盒子的顶点和法线:

btVector3** vertices= wallShape->getVertices();
btVector3**normals = wallShape->getNormals();

但是你无法获取索引列表来按特定顺序绘制顶点!

android c++ android-ndk opengl-es-2.0 bulletphysics
2个回答
11
投票

如果我没记错的话,这不是在 Bullet 中绘制调试形状的正确方法。您是否阅读了用户手册 (PDF),第 16 页?

您应该实现自己的调试抽屉类,该类实现

btIDebugDraw
,并在该类中实现
drawLine
方法。

您可以使用

setDebugDrawer
将此调试抽屉传递给项目符号,然后使用
world->getDebugDrawer->setDebugMode(debugMode);

启用它

要绘制世界,请调用

world->debugDrawWorld()
;

然后,这会在您的自定义函数上多次调用

drawLine
,直到绘制出物理世界的线框模型。


0
投票

有人知道为什么这不会调用我的画线函数吗?

我继承了该类并创建了一个实例等,之后在我的应用程序循环中调用了绘制世界。

PhysDebug debugger;
            
    dynamicsWorld->setDebugDrawer(&debugger);
    dynamicsWorld->getDebugDrawer()->setDebugMode(btIDebugDraw::DBG_DrawWireframe);
    //Create buffers.

我没有列出我的drawLine函数,因为无论如何它都没有被调用,但这是标题,

  void drawLine(const btVector3& from, const btVector3& to, const btVector3& color);
      ```
© www.soinside.com 2019 - 2024. All rights reserved.