Unexpectect ContactPairTest with btGImpactMeshShape

Post Reply
tnassour
Posts: 3
Joined: Mon Oct 31, 2016 1:24 pm

Unexpectect ContactPairTest with btGImpactMeshShape

Post by tnassour »

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 :

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);
Function : btGImpactMeshShapeCollisionShapeFromOSG()

Code: Select all

		btGImpactMeshShape* btGImpactMeshShapeCollisionShapeFromOSG(nodes::PartMeshGeode* geode)
		{
			if (!geode) return nullptr;
			btGImpactMeshShape* meshShape = new btGImpactMeshShape(geode->getbtTriangleMesh());
			return meshShape;
		}
Function getbtTriangleMesh():

Code: Select all

btTriangleMesh* getbtTriangleMesh() const { return mbtTriangleMesh.get(); }
How mbtTriangleMesh is computed :

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);
		}
How do i compute collision :

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);
			}
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 ?
Attachments
Bounding box intersects and no collision : OK
Bounding box intersects and no collision : OK
3.png (43 KiB) Viewed 3543 times
Collision Detected
Collision Detected
2.png (102.2 KiB) Viewed 3543 times
No Collision
No Collision
1.png (83.05 KiB) Viewed 3543 times
Post Reply