Page 1 of 1

btConvexHullShape problems

Posted: Sat Sep 10, 2016 5:44 pm
by Tonyx97
Hello guys! I implemented bullet physics in my engine some days ago and I organized very well the code for my entities etc but I stopped at Convex / Mesh collisions. My goal is to get a dynamic collision created in runtime by the model vertices. I've read a lot about this type of collision but still I have problems with it. First of all I want to clear that the vertex array I pass to the collision is in the same order I save the vertices to render my model so I doubt the problem it's on the array. I've tested meshes like cubes and they work fine if I put the margin to 0, however, trying some "complex" meshes like this one https://gyazo.com/e2a5b80c40c2990075d523cfe7f9afed will make the collision very very weird, I debug the vertices points returned by btConvexHullShape and they all are perfectly positioned in each vertex of the model and this is turning me crazy because I don't know where is the problem... for example look at this picture, I create the model, I freeze it and put a cube over it and this happens https://gyazo.com/7d1f6861d4b3087b273fc87c5fa6be8c . I drop the cube over the model and the collision is not properly set up... Here's how I do it.

Code: Select all

void CPhysicEntity::AddConvexCollider(std::vector<glm::vec3> _vertices, glm::vec3 _position, glm::vec4 _rotation, float _mass, float _friction, glm::vec3 _inertia)
{
	if (!Physics->World) return;

	Mass = _mass;
	Inertia = btVector3(_inertia.x, _inertia.y, _inertia.z);

	MeshShape = new btConvexHullShape();

	for (auto i = 0; i < _vertices.size(); i++)
		MeshShape->addPoint(btVector3(_vertices[i].x, _vertices[i].y, _vertices[i].z));

	MeshShape->CalculateInertia(_mass, Inertia);
	ColShape = MeshShape; //ColShape = btCollisionShape

	MotionState = new btDefaultMotionState(btTransform(btQuaternion(_rotation.x, _rotation.y, _rotation.z, _rotation.w), btVector3(_position.x, _position.y, _position.z)));
	btRigidBody::btRigidBodyConstructionInfo RigidBodyInfo(_mass, MotionState, ColShape, Inertia);
	RigidBody = new btRigidBody(RigidBodyInfo);
	RigidBody->setFriction(glm::clamp(_friction, 0.f, 1.f));

	Position = (btVector3*)&RigidBody->getCenterOfMassPosition();
	isFrozen = (_mass == 0.f);

	Physics->AddRigidBodyToQueue(RigidBody);
}
I really appreciate any help. Thanks in advance.

Re: btConvexHullShape problems

Posted: Sun Sep 11, 2016 2:07 pm
by benelot
If I understood correctly, you set the collision margin to zero. That is never a good idea. Try a small value like 0.05, but never 0. It usually causes the strange collisions.

Re: btConvexHullShape problems

Posted: Sun Sep 11, 2016 4:41 pm
by Tonyx97
benelot wrote:If I understood correctly, you set the collision margin to zero. That is never a good idea. Try a small value like 0.05, but never 0. It usually causes the strange collisions.
At the first try I didn't change the margin, it was to 0.04 by default and it's the same problem.

Re: btConvexHullShape problems

Posted: Mon Sep 12, 2016 6:58 pm
by drleviathan
Perhaps your are confused about what a convex hull really is? The example mesh you provided is not only concave but also non-manifold. For a complex terrain mesh like that you should probably use btBvhTriangleMeshShape instead of btConvexHullShape.

If you want a concave mesh to collide as a dynamic RigidBody then you could use a convexification algorithm like V-HACD to make a collection of convex hulls and then put them all inside a btCompoundShape.

Re: btConvexHullShape problems

Posted: Mon Sep 12, 2016 8:41 pm
by Tonyx97
drleviathan wrote:Perhaps your are confused about what a convex hull really is? The example mesh you provided is not only concave but also non-manifold. For a complex terrain mesh like that you should probably use btBvhTriangleMeshShape instead of btConvexHullShape.

If you want a concave mesh to collide as a dynamic RigidBody then you could use a convexification algorithm like V-HACD to make a collection of convex hulls and then put them all inside a use btCompoundShape.
Nice now it's working good but I don't know why my btBvhTriangleMeshShape is not colliding with another btBvhTriangleMeshShape collision type. It's working at the point that btBvhTriangleMeshShape is working nice with inertia and against other native collisions but it's not colliding with another btBvhTriangleMeshShape. Thanks you for your answer! :)

Re: btConvexHullShape problems

Posted: Mon Sep 12, 2016 11:42 pm
by drleviathan
RigidBodies that use btBvhTraiangleMeshShape can only be STATIC (not DYNAMIC or KINEMATIC), which means no two btBvhTriangleMeshShape objects will ever collide with each other. In fact, that is why the don't collide: there is no "algorithm" implemented in Bullet that handles two such shapes.

In order to make a dynamic object with a concave shape you need to use a btCompoundShape with convex subshapes, or I hear you can use a btGImpactShape (but I don't have any personal experience with that).

Re: btConvexHullShape problems

Posted: Tue Sep 13, 2016 9:41 am
by Tonyx97
drleviathan wrote:RigidBodies that use btBvhTraiangleMeshShape can only be STATIC (not DYNAMIC or KINEMATIC), which means no two btBvhTriangleMeshShape objects will ever collide with each other. In fact, that is why the don't collide: there is no "algorithm" implemented in Bullet that handles two such shapes.

In order to make a dynamic object with a concave shape you need to use a btCompoundShape with convex subshapes, or I hear you can use a btGImpactShape (but I don't have any personal experience with that).
Thanks you very much that's everything I wanted to know!