Bullet Physics - 从Mesh创建ShapeHull

时间:2014-06-04 12:53:13

标签: c++ opengl game-physics physics-engine bulletphysics

我正在尝试将用于使用OpenGL绘制的网格数据加载到子弹引擎中。

问题是我不相信网格数据实际上是从指针中读取的。因此,物理世界不匹配,“角色控制器”正在落地。

渲染的世界很好,所以我知道OpenGL数据很好。

这是我用来将数据传输到OpenGL的控制器类的功能。

我在这里缺少什么?

谢谢 - 这是我正在使用的代码:

void PhysicsController::AddStaticBasicMesh(PhysicsObject* NewObject, bool ReducePolygonCount = true)
{
   btTriangleMesh* OriginalTriangleMesh = new btTriangleMesh();
   btIndexedMesh* NewMesh = new btIndexedMesh();
   NewMesh->m_triangleIndexBase = (unsigned char *)NewObject->Indices;
   NewMesh->m_triangleIndexStride = 3 * sizeof(unsigned int);
   NewMesh->m_vertexBase = (unsigned char *)NewObject->Vertices;
   NewMesh->m_vertexStride = 3 * sizeof(float);
   NewMesh->m_numVertices = NewObject->NumberOfVertices;
   NewMesh->m_numTriangles = NewObject->NumberOfTriangles;
   OriginalTriangleMesh->addIndexedMesh((*NewMesh));
   btConvexShape* NewStaticMesh = new btConvexTriangleMeshShape(OriginalTriangleMesh);
   btConvexHullShape* ReducedPolygonStaticMesh;

   if (ReducePolygonCount == true) {
      btShapeHull* HullOfOriginalShape = new btShapeHull(NewStaticMesh);
      btScalar CurrentMargin = NewStaticMesh->getMargin();
      HullOfOriginalShape->buildHull(CurrentMargin);
      ReducedPolygonStaticMesh = new btConvexHullShape();

      for (int i = 0; i < HullOfOriginalShape->numVertices(); i++) {
         ReducedPolygonStaticMesh->addPoint(HullOfOriginalShape->getVertexPointer()[i], false);
      }

      ReducedPolygonStaticMesh->recalcLocalAabb();
      /*

      Find out what this line does.

      ReducedPolygonStaticMesh->initializePolyhedralFeatures();

      */

      StaticShapes.push_back(ReducedPolygonStaticMesh);
      btDefaultMotionState* StaticMotionState = new btDefaultMotionState((*NewObject->PositionAndOrientation));
      btRigidBody::btRigidBodyConstructionInfo StaticMeshRigidBodyInfo(0, StaticMotionState, ReducedPolygonStaticMesh, btVector3(0.0f, 0.0f, 0.0f));
      btRigidBody* StaticRigidMesh = new btRigidBody(StaticMeshRigidBodyInfo);
      NewObject->Body = StaticRigidMesh;
      StaticRigidMesh->setCollisionFlags(StaticRigidMesh->getCollisionFlags() | btCollisionObject::CF_STATIC_OBJECT);
      MainPhysicsWorld->addRigidBody(StaticRigidMesh, btBroadphaseProxy::StaticFilter, btBroadphaseProxy::CharacterFilter | btBroadphaseProxy::DefaultFilter);
      AllRigidBodies.push_back(NewObject);
      StaticRigidMesh->setUserPointer(AllRigidBodies[AllRigidBodies.size() - 1]);
      delete HullOfOriginalShape;
      delete NewStaticMesh;
   } else {
      StaticShapes.push_back(NewStaticMesh);;
      btDefaultMotionState* StaticMotionState = new btDefaultMotionState((*NewObject->PositionAndOrientation));
      btRigidBody::btRigidBodyConstructionInfo StaticMeshRigidBodyInfo(0, StaticMotionState, NewStaticMesh, btVector3(0.0f, 0.0f, 0.0f));
      btRigidBody* StaticRigidMesh = new btRigidBody(StaticMeshRigidBodyInfo);
      NewObject->Body = StaticRigidMesh;
      StaticRigidMesh->setCollisionFlags(StaticRigidMesh->getCollisionFlags() | btCollisionObject::CF_STATIC_OBJECT);
      MainPhysicsWorld->addRigidBody(StaticRigidMesh, btBroadphaseProxy::StaticFilter, btBroadphaseProxy::CharacterFilter | btBroadphaseProxy::DefaultFilter);
      AllRigidBodies.push_back(NewObject);
      StaticRigidMesh->setUserPointer(AllRigidBodies[AllRigidBodies.size() - 1]);
   }
}

1 个答案:

答案 0 :(得分:4)

也许,不是您正在寻找的答案,但可能您可以使用btConvexHullShape,如Bullet文档中所建议的那样?

http://bulletphysics.org/Bullet/BulletFull/classbtConvexTriangleMeshShape.html

  

尽管如此,大多数用户应该使用更好的表现   btConvexHullShape而不是。

使用addPoint逐个添加顶点,文档中也提到了这一点:

http://bulletphysics.org/Bullet/BulletFull/classbtConvexHullShape.html

  

更容易不传递构造函数中的任何点,只需添加即可   使用addPoint

一次一个点

我知道回答&#34;只是使用别的&#34; 而不是回答原始问题是不好的。

这样你就可以复制你的顶点了。

但是,如果您只是开始学习一些新技术,您可能希望先使一切工作,然后逐步返回原始设计可能更容易。

另一个好主意是使用调试抽屉并实际看到您的网格物体的样子。

我已经为OpenGL ES 2.0创建了一个,也许它会有所帮助: https://github.com/kmuzykov/custom-opengl-es-game-engine/blob/master/Engine/Physics/KMPhysicsDebugDrawer.cpp