Page 1 of 1

[SOLVED] Irrlicht to Newton mesh - Segmentation fault

Posted: Sun Jan 25, 2009 4:13 am
by hach-que
I'm having trouble converting my Irrlicht mesh into a newton collision. When I run the following code, Newton causes a segmentation fault. I've been getting help at the Newton Dynamics forums, but after pointing out multiple things I was doing wrong with Newton (and me fixing them), they are now saying that I must be collecting the triangles incorrectly.

Can anyone here help?

Code: Select all

    collision = NewtonCreateTreeCollision(physworld, NULL);
                NewtonTreeCollisionBeginBuild(collision);

                // loop through, adding triangles.
                mesh->grab();
                singleframe = mesh;
                totalbuffers = singleframe->getMeshBufferCount();
                for (int i = 0; i < totalbuffers; i++)
                {
                   meshbuffer = singleframe->getMeshBuffer(i);
                   meshbuffer->grab();
                   //mb_vertices = (irr::video::S3DVertex2TCoords*)meshbuffer->getVertices();
                   //mb_indices = meshbuffer->getVertices();
                   mb_vertices_total = meshbuffer->getVertexCount();
                   for (int a = 0; a < mb_vertices_total-2; a++)
                   {
                      for (int b = 0; b < 3; b++)
                      {
                         p[b] = meshbuffer->getPosition(a+b);
                         newvertex[b][0] = p[b].X;
                         newvertex[b][1] = p[b].Y;
                         newvertex[b][2] = p[b].Z;
                      }
                      NewtonTreeCollisionAddFace(
                         collision,
                         3,
                         &newvertex[0][0],
                         sizeof(float)*3,
                         1);
                      nonlua_debug("Adding triangle " << a << " of " << mb_vertices_total-2,LVL_INFO);
                   }
                   meshbuffer->drop();
                }
                singleframe = 0;
                mesh->drop();
                NewtonTreeCollisionEndBuild(collision, 0);

Posted: Sun Jan 25, 2009 4:57 am
by vitek
  1. Your code makes the assumption that the mesh buffer has S3DVertex2TCoords[/i] vertices. You do nothing to check.
  2. Your nested loops are wrong for getting triangle faces. You need to use the index buffer to interpret the triangle data.


A simple search for NewtonTreeCollisionBeginBuild should probably get you something that you can use.

Posted: Sun Jan 25, 2009 5:08 am
by hach-que
Actually I don't even use that vertices line, I just get the total number of vertices so I can check how many positions are available.

But getting 3 positions in a group should create a triangle correct?

Posted: Sun Jan 25, 2009 6:01 am
by Dark_Kilauea
You NEED to use the index buffer to access the vertices, otherwise the data you pull out makes no sense.

Read up on index buffers for more information.

Posted: Sun Jan 25, 2009 6:47 am
by hach-que
DARN IT!

I've been looking in the wrong place.

It wasn't the terrain physics, it was the fact I was loosing reference to the object in Lua. I guess I'll have to find out why that's happening now (even though Lua has been told not to clean up those objects).