Cannot detect collisions between capsule and Trimesh

Post Reply
sara144
Posts: 1
Joined: Sun Dec 10, 2017 1:24 pm

Cannot detect collisions between capsule and Trimesh

Post by sara144 »

Hello,

I am new to Bullet. I am trying to simulate a freely moving capsule inside a concave sphere shape (btBvhTriangleMeshShape) consisting of two hemispheres. So far I cannot detect any collisions between the mesh and the capsule.

However, if I replace the capsule with a sphere, everything works fine and the sphere rigid body stays inside the mesh.

Any idea what could be going wrong? I am using double precision as my sphere/capsule have radius 1e-8.

Here is a code for creating the mesh and the capsule:

Code: Select all

// Create mesh
  btTriangleIndexVertexArray* meshInterface = new btTriangleIndexVertexArray();
  std::vector<btVector3>* vertices1 = new std::vector<btVector3>(Trimesh::kNucleusVertices);
  for (int i = 0; i < Trimesh::kNucleusVertices; i++) {
    vertices1->at(i) = btVector3(btScalar(Trimesh::nuc1_vertices[3*i]),
                                btScalar(Trimesh::nuc1_vertices[3*i+1]),
                                btScalar(Trimesh::nuc1_vertices[3*i+2]));
  }

  btIndexedMesh top_mesh;
  top_mesh.m_numTriangles = Trimesh::kNucleusFaces;
  top_mesh.m_triangleIndexBase = reinterpret_cast<const unsigned char*>(Trimesh::nuc_indices);
  top_mesh.m_triangleIndexStride = sizeof (short)*3;
  top_mesh.m_numVertices = vertices1->size();
  top_mesh.m_vertexType = PHY_DOUBLE;
  top_mesh.m_vertexStride = sizeof(btVector3DoubleData);
  top_mesh.m_vertexBase = reinterpret_cast<const unsigned char*>(vertices1->data());
  meshInterface->addIndexedMesh(top_mesh, PHY_SHORT);

  std::vector<btVector3>* vertices2 = new std::vector<btVector3>(Trimesh::kNucleusVertices);
  for (int i = 0; i < Trimesh::kNucleusVertices; i++) {
    vertices2->at(i) = btVector3(btScalar(Trimesh::nuc2_vertices[3*i]),
                                 btScalar(Trimesh::nuc2_vertices[3*i+1]),
                                 btScalar(Trimesh::nuc2_vertices[3*i+2]));
  }

// same for the bottom hemisphere...
// ....
// ...

  // create free moving capsule
  double len = kChromLength-2*kChromRadiusMono;
  btCollisionShape* capsuleShape = new btCapsuleShapeZ(btScalar(kChromRadiusMono), btScalar(len));
  btScalar mass(kMass);
  btVector3 inertia(1., 1., 1.);
  capsuleShape->calculateLocalInertia(mass, inertia);

  btTransform startTransform;
  startTransform.setIdentity();
  startTransform.setOrigin(btVector3(0,0,0));
  btDefaultMotionState* motion_state = new btDefaultMotionState(startTransform);
  btRigidBody::btRigidBodyConstructionInfo body_ci(mass, motion_state, capsuleShape, inertia);
  btRigidBody* capsule_body = new btRigidBody(body_ci);
  capsule_body->setUserIndex2(1);
  capsule_body->applyCentralForce(btVector3(0,0.5e-6,1.5e-6));
  dynamicsWorld->addRigidBody(capsule_body);
Any idea what could be going wrong?
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Cannot detect collisions between capsule and Trimesh

Post by drleviathan »

Looking at the ctors for btRigidBody and btCollisionObject it appears that btCollisionObject::m_collisionFlags is initialized to btCollisionObject::CF_STATIC_OBJECT and is not set otherwise, even when you provide non-zero mass and inertia tensor. Why don't you try removing that flag:

Code: Select all

capsule_body->setCollisionFlags(capsule_body->getCollisionFlags() & ~btCollisionObject::CF_STATIC_OBJECT);
Post Reply