Create bullet shape from .obj mesh

Post Reply
User avatar
Kotik
Posts: 7
Joined: Tue Oct 04, 2016 7:12 pm

Create bullet shape from .obj mesh

Post by Kotik »

Hi there!

I have a simple mesh model of a plane made in Blender, consisting of only triangles.

I'm trying to create the corresponding btConvexTriangleMeshShape the following way (the whole .cpp file). First I read the .obj file and make up a list of triangles:

Code: Select all

std::string line;
while (std::getline(inputFile, line)) {
    std::istringstream iss(line);

    char c;
    iss >> c;

    if (line[0] == 'v' && line[1] == ' ') {
        btScalar x, y, z;
        iss >> x >> y >> z;
        btVector3 point(x, y, z);
        vertices.push_back(point);
    } else if (line[0] == 'f' && line[1] == ' ') {
        std::string s1, s2, s3;
        iss >> s1 >> s2 >> s3;

        s1 = s1.substr(0, s1.find('/'));
        s2 = s2.substr(0, s2.find('/'));
        s3 = s3.substr(0, s3.find('/'));

        btScalar t1, t2, t3;
        t1 = std::stof(s1) - 1;
        t2 = std::stof(s2) - 1;
        t3 = std::stof(s3) - 1;
        btVector3 triangle(t1, t2, t3);

        triangles.push_back(triangle);
    }
}
and then I create a btTriangleMesh:

Code: Select all

btTriangleMesh *triangleMesh = new btTriangleMesh();
for (const btVector3 &triangle : triangles)
    triangleMesh->addTriangle(vertices[triangle.x()], vertices[triangle.y()], vertices[triangle.z()]);
and finally put the btTriangleMesh into the shape (the second .cpp file):

Code: Select all

shape = new btConvexTriangleMeshShape(objMesh.getTriangleMesh());
Then I create a node in Irrlicht with the same model, but they do not match when it collides with other rigid bodies.

Just looking at the code and the description can you spot some potential mistakes I've made?

Thanks for your answers in advance :3
User avatar
Kotik
Posts: 7
Joined: Tue Oct 04, 2016 7:12 pm

Re: Create bullet shape from .obj mesh

Post by Kotik »

I've finally found the problem out. It was that the body's rotation didn't get updated in the graphics engine.
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Create bullet shape from .obj mesh

Post by benelot »

Hello,

I am sorry nobody responded to you and I am happy you came back to post what the problem was! If yoh have any other questions, come back and ask away!
User avatar
Kotik
Posts: 7
Joined: Tue Oct 04, 2016 7:12 pm

Re: Create bullet shape from .obj mesh

Post by Kotik »

benelot wrote:Hello,

I am sorry nobody responded to you and I am happy you came back to post what the problem was! If yoh have any other questions, come back and ask away!
Thank you :)
usamarafiq93
Posts: 5
Joined: Mon Jun 10, 2019 3:46 pm

Re: Create bullet shape from .obj mesh

Post by usamarafiq93 »

Kotik wrote: Wed Oct 05, 2016 12:02 am I've finally found the problem out. It was that the body's rotation didn't get updated in the graphics engine.
I am getting my vertices as an object. I want to create triangle mesh from those vertices instead of loading those vertices from a file.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Create bullet shape from .obj mesh

Post by Erwin Coumans »

PyBullet has some examples that create a mesh (convex or concave) from an .obj file or from vertices in memory.

See https://github.com/bulletphysics/bullet ... ateMesh.py
https://github.com/bulletphysics/bullet ... alShape.py
and a few other examples in https://github.com/bulletphysics/bullet ... t/examples
Post Reply