Unexpectect ContactPairTest with btGImpactMeshShape
Posted: Thu Mar 09, 2017 12:54 pm
Hello,
I have unexpected contact with two btGImpactMeshShape.
I am using osg library to display objects.
The two objects are : btGImpactMeshShape:
Creation of the Shapes :
Function : btGImpactMeshShapeCollisionShapeFromOSG()
Function getbtTriangleMesh():
How mbtTriangleMesh is computed :
How do i compute collision :
And i am using mCollidingPairs to change color.
My Problem is only here : i am detecting a collision : under the ship in the photo.
The red wire is the bondingbox.
Can some help me ?
I have unexpected contact with two btGImpactMeshShape.
I am using osg library to display objects.
The two objects are : btGImpactMeshShape:
Creation of the Shapes :
Code: Select all
CustomizableNodeVisitor<> cnv;
cnv.setGeodeApplyFunc([this](CustomizableNodeVisitor<>& cnv, osg::Geode& geode) {
// Try casting to a PartMeshGeode
osg::ref_ptr<nodes::PartMeshGeode> pmg = dynamic_cast<nodes::PartMeshGeode*>(&geode);
if (!pmg || pmg->getName() != utils::PART_MESH_GEODE) return;
// Create Bullet Shape
std::unique_ptr<btGImpactMeshShape> btShape(bullet::btGImpactMeshShapeCollisionShapeFromOSG(pmg.get()));
if (btShape)
{
//Get the matrix of the actual geometry
osg::Matrix mat = cnv.getMatrixStack().empty() ? osg::Matrix::identity() : cnv.getMatrixStack().back();
// Extract translation on x,y and rotation on z and use them as transformation for trajectories
osg::Vec3d translation;
osg::Quat rotation;
osg::Vec3d scale;
osg::Quat so;
mat.decompose(translation, rotation, scale, so);
// Set the scale to the shape
btShape->setLocalScaling(osgbCollision::asBtVector3(scale));
btShape->updateBound();
btShape->setMargin(0.0001f); // Set Detection Margin
// Set Matrix for btObject without the scale because the scale is on the shape
btTransform transform;
transform.setIdentity();
transform.setOrigin(osgbCollision::asBtVector3(translation));
transform.setRotation(bullet::asbtQuaternion(rotation));
mCompoundObject->addChildShape(transform, btShape.get());
mCollisionObjects.emplace(cnv.getNodePath(), std::move(btShape));
}
});
accept(cnv);
Code: Select all
btGImpactMeshShape* btGImpactMeshShapeCollisionShapeFromOSG(nodes::PartMeshGeode* geode)
{
if (!geode) return nullptr;
btGImpactMeshShape* meshShape = new btGImpactMeshShape(geode->getbtTriangleMesh());
return meshShape;
}
Code: Select all
btTriangleMesh* getbtTriangleMesh() const { return mbtTriangleMesh.get(); }
Code: Select all
void PartMeshGeode::createPhysics()
{
osgbCollision::ComputeTriMeshVisitor visitor;
accept(visitor);
osg::ref_ptr<osg::Vec3Array> vertices = visitor.getTriMesh();
if (vertices->size() < 3) return;
std::unique_ptr<btTriangleMesh> mesh(new btTriangleMesh);
mesh->preallocateVertices(vertices->size());
mesh->preallocateIndices(vertices->size());
for (size_t i = 0; i + 2 < vertices->size(); i += 3)
{
osg::Vec3& p1 = (*vertices)[i];
osg::Vec3& p2 = (*vertices)[i + 1];
osg::Vec3& p3 = (*vertices)[i + 2];
mesh->addTriangle(osgbCollision::asBtVector3(p1), osgbCollision::asBtVector3(p2), osgbCollision::asBtVector3(p3));
}
mbtTriangleMesh = std::move(mesh);
}
Code: Select all
for (const auto& tuple : pairsToCollide)
{
ConnectedResultCallback result;
auto collisionObj0 = static_cast<btCollisionObject*>(tuple.m_pProxy0->m_clientObject);
auto collisionObj1 = static_cast<btCollisionObject*>(tuple.m_pProxy1->m_clientObject);
// Find collision between objects -> this calls algorithm if attached
contactPairTest(collisionObj0, collisionObj1, result);
if (result.mConnected)
{
// There is contact
mCollidingPairs.emplace(tuple);
}
else
{
// No contact found : Remove the pair if it was colliding
auto it = mCollidingPairs.find(tuple);
if (it != mCollidingPairs.end())
mCollidingPairs.erase(it);
}
// Ray casting on each collision object
// Used to detect if the collision object is inside another Object
determineIfPartInstanceObjectIsInsideAnObject(collisionObj0);
determineIfPartInstanceObjectIsInsideAnObject(collisionObj1);
}
My Problem is only here : i am detecting a collision : under the ship in the photo.
The red wire is the bondingbox.
Can some help me ?