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.