-
Hi everyone! I passed here to ask you if you know how to create a custom bullet collision shape. I am trying to convert a raylib mesh into a bullet physics shape and I am not having that much luck with it. For some strange reason, the tree is falling through the floor, instead of colliding with it. I have tried to change the shape to a sphere and, in that case, the collision works perfectly, which means my issue is probably with my attempt of converting a mesh into a shape. As I don't want a sphere as my collider, only a custom shape, I came here to ask you for help. here is my code for reference: InitWindow(800, 600, "Raylib + Bullet Physics Example");
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase();
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver();
btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);
dynamicsWorld->setGravity(btVector3(0, -9.81, 0));
Model treeModel = LoadModel("assets/models/tree.obj");
btTriangleMesh* treeTriMesh = new btTriangleMesh();
for (int m = 0; m < treeModel.meshCount; m++) {
Mesh mesh = treeModel.meshes[m];
float* meshVertices = (float*)mesh.vertices;
for (int v = 0; v < mesh.vertexCount; v += 9) {
Vector3 v1 = { meshVertices[v], meshVertices[v + 1], meshVertices[v + 2] };
Vector3 v2 = { meshVertices[v + 3], meshVertices[v + 4], meshVertices[v + 5] };
Vector3 v3 = { meshVertices[v + 6], meshVertices[v + 7], meshVertices[v + 8] };
std::cout << v1.x << ", " << v1.y << ", " << v1.z << std::endl;
std::cout << v2.x << ", " << v2.y << ", " << v2.z << std::endl;
std::cout << v3.x << ", " << v3.y << ", " << v3.z << std::endl;
std::cout << std::endl;
treeTriMesh->addTriangle(
btVector3(v1.x, v1.y, v1.z),
btVector3(v2.x, v2.y, v2.z),
btVector3(v3.x, v3.y, v3.z)
);
}
}
btBvhTriangleMeshShape* treeShape = new btBvhTriangleMeshShape(treeTriMesh, true);
treeShape->buildOptimizedBvh();
btTransform treeTransform;
treeTransform.setIdentity();
treeTransform.setOrigin(btVector3(0, 10, 0));
btDefaultMotionState* treeMotionState = new btDefaultMotionState(treeTransform);
btScalar treeMass = 1.0f;
btVector3 treeInertia(0, 0, 0);
treeShape->calculateLocalInertia(treeMass, treeInertia);
btRigidBody::btRigidBodyConstructionInfo treeRigidBodyCI(treeMass, treeMotionState, treeShape, treeInertia);
btRigidBody* treeRigidBody = new btRigidBody(treeRigidBodyCI);
dynamicsWorld->addRigidBody(treeRigidBody); issue.with.custom.shapes.mp4 |
Beta Was this translation helpful? Give feedback.
Answered by
luis605
Oct 14, 2023
Replies: 1 comment
Answer selected by
luis605
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gamedev.stackexchange.com/a/100562/168794