how can i make a Collisionshape of a Mesh.

zUuka12
Posts: 4
Joined: Fri Apr 05, 2013 1:23 pm

how can i make a Collisionshape of a Mesh.

Post by zUuka12 »

Hello Everyone,

i am new to the Forum.
I hope my Post is right here.
I am using Ogre as "Renderer" and so far managed to have a plane and a sphere ( both in Ogre and Bullet ) Bullet controlls the Ogre stuff.
I can yaw and pitch my plane and the sphere is rolling on the surface as it should.
Now i'd like to use my Ogre Mesh of an Labyrinth (done with Blender) as a Collision shape for Bullet.
I am NOT USING OgreBullet (a Bullet-Wrapper for Ogre)..just because i'd like to understand Bullet better.
And also i'd like to use the Ogre Mesh directly(i read that bullet understands Collada Meshes though).
I managed to get the Number of Vertices and Idices as well as Positions of each Vertex.
Which is the propper shape to use...i used btConvexHull and add Point....but i guess this will only make a box kind of shape for Collision, and not the actuall "Walls" of my Labyrinth Mesh?
Or will i be needing a striding mesh interface?! o.O

Any help is welcome :)

thanks in advance.
jazztickets
Posts: 21
Joined: Tue Dec 29, 2009 12:48 am

Re: how can i make a Collisionshape of a Mesh.

Post by jazztickets »

You want to use btBvhTriangleMeshShape.

Add your vertices and indices to btTriangleIndexVertexArray object, then add that to the shape.

Example: https://github.com/jazztickets/irrlamb/ ... lision.cpp
Jyavoc
Posts: 13
Joined: Fri Apr 05, 2013 6:19 am
Location: Pittsburgh, PA

Re: how can i make a Collisionshape of a Mesh.

Post by Jyavoc »

I'd suggest that, even though you aren't using OgreBullet, that you could look at their source code. Figure out what is going on where and how, get a better feel for how they're doing it and what they're doing. Just a suggestion, mind; but I see no reason to reinvent how to learn or use the wheel, even if you want to reinvent the wheel (please disregard the negative connotations with this phrase).
zUuka12
Posts: 4
Joined: Fri Apr 05, 2013 1:23 pm

Re: how can i make a Collisionshape of a Mesh.

Post by zUuka12 »

Hey Guys,

Thanks very much for the help.
I've tried using an triangle array but wasn't quite shure which shape i should choose (or how to pass to btBvhTriangleMeshShape, couldn't figure out format for vertexBase.it's a btScalar?! o.O ).
So i fell back to my start off try.
Which kind of works but i think its not good for my use...i can import any mesh from Ogre but as soon as it's a concave for example a box with on face opened , so a sphere could go in,
it just kind of interpolates between vertices so instead of a hollow cube i get something like an inverse pyramid where the sphere goes in.
So i gues only some "outer shape" is calculated and kind of interpolated, so it's not the actual shape of the mesh.
Well this is my code so far:

Code: Select all

int *createFromEntity(Ogre::Entity *ent, Ogre::Vector3 pos, Ogre::Quaternion ori, Ogre::Vector3 size)
{
        size_t vertex_count,index_count;
        Ogre::Vector3* vertices;
        unsigned long* indices;
        int triIndex;
        Ogre::MeshPtr meshptr;
        meshptr = ent->getMesh();
        if(meshptr.isNull())
            {
            printf("ERROR : MeshPointer is NULL\n");
            return 0;
            }

        getMeshInformation(meshptr.getPointer() , vertex_count,vertices, index_count, indices, pos, ori, size);

        printf("Vertices in mesh: %d \n",vertex_count);
        printf("Triangles in mesh: %d \n",index_count);

        btTriangleMesh* data = new btTriangleMesh();
       //btConvexHullShape *cShape = new btConvexHullShape();



            triIndex = 0;
            for(unsigned int i = 0;i < vertex_count; i+=3)
            {
                triIndex++;
                btVector3 A = btVector3((float)vertices[i].x,(float)vertices[i].y,(float)vertices[i].z);
                btVector3 B = btVector3((float)vertices[i+1].x,(float)vertices[i+1].y,-(float)vertices[i+1].z);
                btVector3 C = btVector3((float)vertices[i+2].x,(float)vertices[i+2].y,-(float)vertices[i+2].z);
                data->addTriangle(A,B,C,false); // false, don’t remove duplicate vertices
                data->addIndex(triIndex);
                printf("----------Triangle #%d-----------------\n",triIndex);
                printf("Point added at X %.0f | Y %.0f | Z%.0f \n",(float)A[0],(float)A[1],(float)A[2]);
                printf("Point added at X %.0f | Y %.0f | Z%.0f \n",(float)B[0],(float)B[1],(float)B[2]);
                printf("Point added at X %.0f | Y %.0f | Z%.0f \n",(float)C[0],(float)C[1],(float)C[2]);
                printf("----------------------------------------\n");
            }
        // true for using quantization; true for building the BVH
       btLabyrinthShape=new btBvhTriangleMeshShape(data,true,true);

       btTriangleInfoMap *TriangleInfoMap = new btTriangleInfoMap();
       btGenerateInternalEdgeInfo(btLabyrinthShape, TriangleInfoMap);

       delete[] vertices;
       delete[] indices;
       return 0;
}
So any idea what shape i should use?
I want to import from a Labyrinth mesh where the sphere should be able to roll inside and collide with the walls.
Or maybe the import of my vertices is wrong , couldn't find how Bulletshape requires the Vertice Order...like x,y,z per triangle?!

Thanks in advance!

:)
zUuka12
Posts: 4
Joined: Fri Apr 05, 2013 1:23 pm

Re: how can i make a Collisionshape of a Mesh.

Post by zUuka12 »

Hello,

so i finally managed to import a Ogre Mesh to a btTriangleMeshShape , main problem was the iteration through the Vertices adding them to TriangleMesh.

Code: Select all

        unsigned int numFaces = index_count / 3;

		mTriMesh = new btTriangleMesh(true,false);

        btVector3    vertexPos[3];
        for (unsigned int n = 0; n < numFaces; ++n)
        {
			{
				const Ogre::Vector3 &vec = vertices[*indices];
				vertexPos[0][0] = vec.x;
				vertexPos[0][1] = vec.y;
				vertexPos[0][2] = vec.z;
			}
			{
				const Ogre::Vector3 &vec = vertices[*(indices + 1)];
				vertexPos[1][0] = vec.x;
				vertexPos[1][1] = vec.y;
				vertexPos[1][2] = vec.z;
			}
			{
				const Ogre::Vector3 &vec = vertices[*(indices + 2)];
				vertexPos[2][0] = vec.x;
				vertexPos[2][1] = vec.y;
				vertexPos[2][2] = vec.z;
			}

			indices += 3;

            mTriMesh->addTriangle(vertexPos[0], vertexPos[1], vertexPos[2]);
        }
so this works really well.

With the previous Code you can get a Convex Shape, with this code you get a well working Concave Shape.


Thanks for your Help!

And sorry it took so long :)