Irrlicht terrain and bullet

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Granyte
Posts: 850
Joined: Tue Jan 25, 2011 11:07 pm
Contact:

Irrlicht terrain and bullet

Post by Granyte »

i'm trying to pass the terrain LOD 0 mesh to bullet in order to have colision with the terrain
and once again i'm butting my head against a wall

Code: Select all

 
terrain->setMaterialFlag(video::EMF_FOG_ENABLE, true);
        irr::scene::CDynamicMeshBuffer mb(irr::video::EVT_STANDARD,irr::video::EIT_16BIT);
        terrain->getMeshBufferForLOD(mb,0);
        btTriangleMesh* pMesh = mb;
        btCollisionShape* pShape = new btBvhTriangleMeshShape((btStridingMeshInterface*)pMesh, true);
here is where i'm so far but there is no conversion from the mesh buffer to bttriangle mesh and i can't seem to figure one by my self

EDIT while we are at it bullet vector don't seem to accept the camera position as coordinate any idea?
Insomniacp
Posts: 288
Joined: Wed Apr 16, 2008 1:45 am
Contact:

Re: Irrlicht terrain and bullet

Post by Insomniacp »

you need to convert form irrlicht to bullet. There is no direct conversions so you need to do it manually. For example

Code: Select all

btVector(irrVec.X,irrVec.Y,irrVec.Z);
So to convert a mesh you would need to copy the data into the bullet format. I don't recall exactly what it wants but you may want to take a look at one of the bullet wrappers some people have created in the projects thread. These basically do the conversions for you and let you handle things at a higher level.
HerrAlmanack
Posts: 52
Joined: Mon Jun 13, 2011 3:50 pm

Re: Irrlicht terrain and bullet

Post by HerrAlmanack »

could we please see the code in context?
Insomniacp
Posts: 288
Joined: Wed Apr 16, 2008 1:45 am
Contact:

Re: Irrlicht terrain and bullet

Post by Insomniacp »

there is enough context there... he is setting a btTriangleMesh equal to a irr::scene::CDynamicMeshBuffer which are not compatible and do not represent the same data.
Instead you need to learn what a btTriangleMesh requires, vertex array, index array, number of vertices, number of indices, and build that using the data in the mesh buffer.

Code: Select all

//used to get a bullet triangle mesh from an irrlicht mesh
btTriangleMesh *getTriangleMesh(IMesh* const mesh, const vector3df scale=vector3df(1,1,1))
{
    btVector3 vertices[3];
        u32 i, j, k;
        s32 index, numVertices;
        u16* mb_indices;
 
        btTriangleMesh *pTriMesh = new btTriangleMesh();
 
        for(i = 0; i < mesh->getMeshBufferCount(); i++)
        {
                irr::scene::IMeshBuffer* mb=mesh->getMeshBuffer(i);
 
        //////////////////////////////////////////////////////////////////////////
                // Extract vertex data                                                  //
                // Because the vertices are stored as structs with no common base class,//
                // We need to handle each type separately                               //
                //////////////////////////////////////////////////////////////////////////
                if(mb->getVertexType() == irr::video::EVT_STANDARD)
                {
                        irr::video::S3DVertex* mb_vertices=(irr::video::S3DVertex*)mb->getVertices();
                        mb_indices = mb->getIndices();
                        numVertices = mb->getVertexCount();
                        for(j=0;j<mb->getIndexCount();j+=3)
                        { //get index into vertex list
                                for (k=0;k<3;k++)
                                {
                                    //three verts per triangle
                                        index = mb_indices[j+k];
                                        if (index > numVertices) continue;
                                        //convert to btVector3
                                        vertices[k] = btVector3(mb_vertices[index].Pos.X * scale.X, mb_vertices[index].Pos.Y * scale.Y,mb_vertices[index].Pos.Z * scale.Z); // 1100
                                }
                                pTriMesh->addTriangle(vertices[0], vertices[1], vertices[2]);
                        }
 
                }
                else
                if(mb->getVertexType()==irr::video::EVT_2TCOORDS)
                {
                        // Same but for S3DVertex2TCoords data
                        irr::video::S3DVertex2TCoords* mb_vertices=(irr::video::S3DVertex2TCoords*)mb->getVertices();
                        u16* mb_indices = mb->getIndices();
                        s32 numVertices = mb->getVertexCount();
                        for(j=0;j<mb->getIndexCount();j+=3)
                        { //index into irrlicht data
                                for (k=0;k<3;k++)
                                {
                                        s32 index = mb_indices[j+k];
                                        if (index > numVertices) continue;
                                        vertices[k] = btVector3(mb_vertices[index].Pos.X * scale.X, mb_vertices[index].Pos.Y * scale.Y, mb_vertices[index].Pos.Z * scale.Z);
                                }
                                pTriMesh->addTriangle(vertices[0], vertices[1], vertices[2]);
                        }
                }
 
                // Does not handle the EVT_TANGENTS type
        }
 
        if(pTriMesh)
        {
            return pTriMesh;
        }
 
        return 0;
}
I borrowed this from one of the bullet wrappers for irrlicht. This is an example (working one) on how to convert an irrlicht mesh into a bullet mesh. Having no direct conversion or overloads to simply do an = operation you need to program things like this to convert everything back and forth. Integrating a physics engine with irrlicht will not be as easy as you may have thought so you may want to look into one of the wrappers that already exist and are pretty much fully functional.
Granyte
Posts: 850
Joined: Tue Jan 25, 2011 11:07 pm
Contact:

Re: Irrlicht terrain and bullet

Post by Granyte »

well for the camera code i was trying to have a box or sphere created at the camera coordinate
but i found my but it was a pointer setting bug i got it fixed

thanks i'm trying it

i knew about irrbullet but sadly irrbullet does not cover the terrain ethier(as well as having issues with irrlicht SVN) i don't know about other wrapper
Last edited by Granyte on Fri Aug 05, 2011 7:45 pm, edited 1 time in total.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Irrlicht terrain and bullet

Post by serengeor »

Insomniacp wrote:Integrating a physics engine with irrlicht will not be as easy as you may have thought so you may want to look into one of the wrappers that already exist and are pretty much fully functional.
It is quite easy to integrate bullet with irrlicht, I think the hardest part is actually converting the mesh :D
Working on game: Marrbles (Currently stopped).
Insomniacp
Posts: 288
Joined: Wed Apr 16, 2008 1:45 am
Contact:

Re: Irrlicht terrain and bullet

Post by Insomniacp »

yeah, but it isn't as easy as using equal signs. the rest is fairly straight forward since they aren't complex objects :)
Granyte
Posts: 850
Joined: Tue Jan 25, 2011 11:07 pm
Contact:

Re: Irrlicht terrain and bullet

Post by Granyte »

i was not expecting it to be as easy as an equal sing it was more to represent where i didn't know what to do anymore

and now again my lack of experience/knowledge strike

on one side i got a function that take a IMesh and on the other i have a way to extract a CDynamicMeshBuffer from the terrain
Insomniacp
Posts: 288
Joined: Wed Apr 16, 2008 1:45 am
Contact:

Re: Irrlicht terrain and bullet

Post by Insomniacp »

Yeah, I would look into some of the bullet wrappers to see how things will work together and if you still want to do it yourself then you will have a good idea on how to approach it. I was at the same point just a month ago when I started my physics system which is an abstract layer on top of a physics engine (for now Nvidia Physx and I will add bullet later depending performance). So having to learn how everything gets built together is troublesome at times and having a reference to something that is working is very helpful at times.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Irrlicht terrain and bullet

Post by serengeor »

Use your brain. What you need for bullet is vertices and CDynamicMeshBuffer has those vertices!
What do you do? Look at the code for IMesh to bullet mesh converter, do a similar thing with your CDynamicMeshBuffer(look at API how to get those vertices). Might be not that easy for a beginner but will sure be a lot useful(mostly experience) if you get it to work.
Working on game: Marrbles (Currently stopped).
Granyte
Posts: 850
Joined: Tue Jan 25, 2011 11:07 pm
Contact:

Re: Irrlicht terrain and bullet

Post by Granyte »

Code: Select all

btTriangleMesh *getTriangleMesh(CDynamicMeshBuffer* mb, const vector3df scale)
{
    btVector3 vertices[3];
        u32 i, j, k;
        s32 index, numVertices;
        u16* mb_indices;
 
        btTriangleMesh *pTriMesh = new btTriangleMesh();
 
                                
 
        //////////////////////////////////////////////////////////////////////////
                // Extract vertex data                                                  //
                // Because the vertices are stored as structs with no common base class,//
                // We need to handle each type separately                               //
                //////////////////////////////////////////////////////////////////////////
                if(mb->getVertexType() == irr::video::EVT_STANDARD)
                {
                        irr::video::S3DVertex* mb_vertices=(irr::video::S3DVertex*)mb->getVertices();
                        mb_indices = mb->getIndices();
                        numVertices = mb->getVertexCount();
                        for(j=0;j<mb->getIndexCount();j+=3)
                        { //get index into vertex list
i updated the function that way but now i get

cannot convert parameter 1 from 'irr::scene::CDynamicMeshBuffer' to 'irr::scene::CDynamicMeshBuffer *
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Irrlicht terrain and bullet

Post by serengeor »

Granyte wrote: i updated the function that way but now i get

cannot convert parameter 1 from 'irr::scene::CDynamicMeshBuffer' to 'irr::scene::CDynamicMeshBuffer *
simple c++, either when you call your function use

Code: Select all

getTriangleMesh([b]&[/b]dynamicmeshbuffer, vector3df(...))
or change function to:

Code: Select all

btTriangleMesh *getTriangleMesh(const CDynamicMeshBuffer & mb, const vector3df scale)
and inside of the function use . for mb
Working on game: Marrbles (Currently stopped).
Post Reply