I have implemented the Bullet Physics engine into my android program with the NDK (I am using Vuforia's imagetarget example for android), and it is set up and working correctly, however I would like to render/draw my collision boxes/planes to see my rigid bodies (btRigidBody)/collision shapes (btCollisionShape), I'm positive this is possible but I can't find any tutorials on how to do it!
我已经实现了子弹物理引擎到我的Android程序与NDK(我使用的是Vuforia的Android的图像目标示例),它是设置和工作正常,但我想渲染/绘制我的碰撞框/平面来看到我的刚体(BtRigidBody)/碰撞形状(BtCollisionShape),我肯定这是可能的,但我找不到任何如何做到这一点的教程!
I have taken the hello world Bullet physics tutorial on their wiki page and modified it to apply the transformations from the falling physics body to a 3d object I have in opengl es 2.0 to view the collision bodies, here is the code I am using to render to object:
我已经在他们的维基页面上使用了Hello World子弹物理教程,并对其进行了修改,以应用从坠落的物理体到我在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);
}
EDIT: looking at the code for btBoxShape i noticed you can grab the box vertices and normals:
编辑:查看btBoxShape的代码,我注意到您可以获取长方体的顶点和法线:
btVector3** vertices= wallShape->getVertices();
btVector3**normals = wallShape->getNormals();
but you can't grab a list of indices to draw the vertex points in a certain order!
但是你不能用一系列的索引来按一定的顺序画顶点!
更多回答
优秀答案推荐
If I recall correctly, this is not the proper way to draw debug shapes in Bullet. Did you read the user manual (PDF), page 16?
如果我没记错的话,这不是在项目符号中绘制调试形状的正确方法。您是否阅读了第16页的用户手册(PDF)?
You are supposed to implement your own debug drawer class which implements btIDebugDraw
, and in this class you implement the drawLine
method.
您应该实现您自己的调试抽屉类,该类实现btIDebugDraw,并且在这个类中实现DrawLine方法。
You pass this debug drawer to bullet with setDebugDrawer
, and then enable it with world->getDebugDrawer->setDebugMode(debugMode);
您可以使用setDebugDrawer将此调试抽屉传递给项目符号,然后使用world->getDebugDrawer->setDebugMode(debugMode);启用它
To draw the world, call world->debugDrawWorld()
;
要绘制世界,请调用world->debugDrawWorld();
This then calls drawLine
on your custom function numerous times until a wireframe model of the physics world has been drawn.
然后,在您的自定义函数上多次调用DrawLine,直到绘制出物理世界的线框模型。
更多回答
This looks like it's what I need, thanks for the pointer! Unfortunately my C++ is rusty (being an android developer I've been primarily programming in java) I've been getting by with code snippets for the NDK stuff but looks like i'm going to have to brush up to write proper C++ classes
这看起来就是我需要的,谢谢你的指点!不幸的是,我的C++已经生疏了(作为一名Android开发人员,我主要是用Java编程),我一直在勉强应付NDK的代码片段,但看起来我必须温习一下才能写出合适的C++类
我是一名优秀的程序员,十分优秀!