btBvhTriangleMeshShape

Petrefax
Posts: 4
Joined: Tue Mar 27, 2012 7:38 pm

btBvhTriangleMeshShape

Post by Petrefax »

Hello, Bullet newbie here. I'm trying to figure out how to create a btBvhTriangleMeshShape. Should be a simple business of adding triangles to a btTriangleMesh and then creating a btBvhTriangleMeshShape from the mesh, no?

In my naive fantasies, the following would create a pyramid-shaped rigid body:

Code: Select all

		
btTriangleMesh tetraMesh;
tetraMesh.addTriangle(btVector3(-1, 0, -1), btVector3(-1, 0, 1), btVector3( 1, 0, -1), false);
tetraMesh.addTriangle(btVector3( 1, 0, -1), btVector3(-1, 0, 1), btVector3( 1, 0,  1), false);
tetraMesh.addTriangle(btVector3(-1, 0, -1), btVector3(0, 1, 0), btVector3(-1, 0, 1), false);
tetraMesh.addTriangle(btVector3(-1, 0, -1), btVector3(0, 1, 0), btVector3( 1, 0,-1), false);
tetraMesh.addTriangle(btVector3( 1, 0, -1), btVector3(0, 1, 0), btVector3( 1, 0, 1), false);
tetraMesh.addTriangle(btVector3( 1, 0,  1), btVector3(0, 1, 0), btVector3(-1, 0, 1), false);
tetraShape = new btBvhTriangleMeshShape(&tetraMesh, false);
		
btVector3 localInertia(0, 0, 0);
tetraShape->calculateLocalInertia(1, localInertia);
btTransform trans;
trans.setIdentity();		
trans.setOrigin(btVector3(0, 7, 0));		
btDefaultMotionState* motionState = new btDefaultMotionState(trans);
btRigidBody* body = new btRigidBody(1, motionState, tetraShape, localInertia);		
body->setContactProcessingThreshold(BT_LARGE_FLOAT);
body->setCcdMotionThreshold(.5);
body->setCcdSweptSphereRadius(0);
m_dynamicsWorld->addRigidBody(body);
In reality, it crashes somewhere deep in the bowels of stepSimulation(). Help appreciated.
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: btBvhTriangleMeshShape

Post by xexuxjy »

I suspect your btTriangleMesh tetraMesh is out of scope by stepSimulation. Have you tried creating it on the heap?
Petrefax
Posts: 4
Joined: Tue Mar 27, 2012 7:38 pm

Re: btBvhTriangleMeshShape

Post by Petrefax »

Thanks xexuxjy, you're right of course. Silly me
Petrefax
Posts: 4
Joined: Tue Mar 27, 2012 7:38 pm

Re: btBvhTriangleMeshShape

Post by Petrefax »

Another probably silly question. When I add btBoxShapes, btCylinderShape, etc, they bounce against each other and behave like they should. But these pyramid shapes don't spin on collision, and don't bounce off each other (only against other bodies). Am I missing something?
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: btBvhTriangleMeshShape

Post by Flix »

Petrefax wrote:Another probably silly question. When I add btBoxShapes, btCylinderShape, etc, they bounce against each other and behave like they should. But these pyramid shapes don't spin on collision, and don't bounce off each other (only against other bodies). Am I missing something?
Yes, btBvhTriangleMeshShape can be used for static/kinematic objects only. You can use btGImpactMeshShapes (or btCompoundShapes plus HACD) for concave dynamic rigidbodies, or you can just throw away the indices and create a btConvexHullShape with the vertices of your mesh, but this would result in your shape becoming convex.
Petrefax
Posts: 4
Joined: Tue Mar 27, 2012 7:38 pm

Re: btBvhTriangleMeshShape

Post by Petrefax »

Thanks Flix. btConvexHullShape did the trick.
Bonaducci
Posts: 2
Joined: Thu Aug 02, 2012 5:53 pm

Re: btBvhTriangleMeshShape

Post by Bonaducci »

I have problem with this class too, so I won't create another topic.

Mainly the problem is here:

Code: Select all

    btVector3 *bVert = new btVector3[MB->MBVcount]; //vertex count
    ushort *bIndi = new ushort[MB->MBTcount * 3]; //triangles count

    MB->recalcTriangles();

    MBVertex *v_tmp = MB->MBV;
    for(int i = 0; i < MB->MBVcount; i++)
    {
        bVert[i] = *v_tmp;

        foreach(MBTriangle *t_tmp, v_tmp->Triangle)
        {
            int tmp = t_tmp->index * 3;
            if(t_tmp->v1 == v_tmp)
            {
                bIndi[tmp] = i;
            }
            else if(t_tmp->v2 == v_tmp)
            {
                bIndi[tmp + 1] = i;
            }
            else
            {
                bIndi[tmp + 2] = i;
            }
        }
        v_tmp = v_tmp->next;
    }
    btTriangleIndexVertexArray *btArray = new btTriangleIndexVertexArray((int)MB->MBTcount, (int*)bIndi, 3 * (int)sizeof(ushort), (int)MB->MBVcount, (btScalar*) &bVert[0].x(), (int)sizeof(btVector3));
    btMesh = new btBvhTriangleMeshShape(btArray, true);
I could look not optimal, but I've removed 2/3 of this code regarding to graphics. Graphic engine uses the same format (list of vertices and triangle->vertex indices) and it works properly (I see what I want). Unfortunately program is crashing on the last line - btBvhTriangleMeshShape constructor. Can anyone tell me why?

I'll add, that this method works just fine:

Code: Select all

    btTriangleMesh *mesh = new btTriangleMesh();
    MBTriangle *triangle = MB->MBT;

    for (short i = 0; i < MB->MBTcount; i++)
    {
        mesh->addTriangle(*triangle->v1, *triangle->v2, *triangle->v3, false);
        triangle = triangle->next;
    }
    btMesh = new btBvhTriangleMeshShape(mesh, false);