Irrlicht terrain collides with Newton

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
RobbyRob
Posts: 21
Joined: Fri Mar 02, 2007 4:30 pm
Location: Germany

Irrlicht terrain collides with Newton

Post by RobbyRob »

Hello,

attached u will find a code snippet of my terrain that is combined with a rigid Newton body. It is similair to any code for this purpose except the fact, that I don´t use a bsp-mesh but an obj (or.x) mesh;
unfortunatelly there is no collision between a common Newton cube and the terrain; the cube always falls through the surface; if I change to the bsp-mesh everything works fine, but usually I use x files for creating landscapes and besides of that, I don´t have a tool creating bsp files;
I guess it has something to do with the mesh buffer, because the printf code always shows me "buff 0" in the console;
does anybody has an idea what is going wrong with my code ?
I tried everything I could found in the forum, no success,
Thank´s for any help


Robert

the code:

Code: Select all

 //Level
   scene::IAnimatedMesh* q2levelmesh = smgr->getMesh("./data/terrain_5.obj");
   scene::ISceneNode* q2node = 0;

     if (q2levelmesh)
    q2node = smgr->addMeshSceneNode(q2levelmesh->getMesh(0));
    q2node->setPosition(core::vector3df(-1370,-130,-1400));
    //q3node->setPosition(core::vector3df(0,0,0));

    q2node->setMaterialTexture(0, driver->getTexture("./data/detailmap2.bmp"));
    q2node->setMaterialTexture(1, driver->getTexture("./data/detail_map1.bmp"));
    q2node->setMaterialType(video::EMT_DETAIL_MAP   );
    q2node->setScale(vector3df(500,500,500));
    q2node->setRotation(vector3df(-20,0,0));
    q2node->setMaterialFlag(video::EMF_LIGHTING,false);



  //LevelNewtonCollision
     NewtonCollision* nmapcollision = NewtonCreateTreeCollision(nWorld, NULL);
     NewtonTreeCollisionBeginBuild(nmapcollision);
     int cMeshBuffer=0;
     int j=0;
     int v1i, v2i, v3i;
     IMeshBuffer *mb;
     float vArray[9]; // vertex array (3*3 floats)
     int tmpCount = 0;

   core::aabbox3d<f32> box;
   //size the box in Newton units
    box = q2levelmesh->getMesh(0)->getBoundingBox();
   vector3df size = box.getExtent()*IrrToNewton;


     for (cMeshBuffer=0; cMeshBuffer<q2levelmesh->getMesh(0)->getMeshBufferCount(); cMeshBuffer++)
     //for (cMeshBuffer=0; cMeshBuffer<10000; cMeshBuffer++)
   {

      mb = q2levelmesh->getMesh(0)->getMeshBuffer(cMeshBuffer);
      S3DVertex* mb_vertices = (S3DVertex*)mb->getVertices();
      u16* mb_indices  = mb->getIndices();
      // add each triangle from the mesh
      for (j=0; j<mb->getIndexCount(); j+=3)
      {


         printf("buff %d count %d\n",cMeshBuffer,j);
         v1i = mb_indices[j];
         v2i = mb_indices[j+1];
         v3i = mb_indices[j+2];
         vArray[0] = mb_vertices[v1i].Pos.X* IrrToNewton;
         vArray[1] = mb_vertices[v1i].Pos.Y* IrrToNewton;
         vArray[2] = mb_vertices[v1i].Pos.Z* IrrToNewton;
         vArray[3] = mb_vertices[v2i].Pos.X* IrrToNewton;
         vArray[4] = mb_vertices[v2i].Pos.Y* IrrToNewton;
         vArray[5] = mb_vertices[v2i].Pos.Z* IrrToNewton;
         vArray[6] = mb_vertices[v3i].Pos.X* IrrToNewton;
         vArray[7] = mb_vertices[v3i].Pos.Y* IrrToNewton;
         vArray[8] = mb_vertices[v3i].Pos.Z* IrrToNewton;

          NewtonTreeCollisionAddFace(nmapcollision, 3, &vArray[0], 12, 1);
     }
   }

      NewtonTreeCollisionEndBuild(nmapcollision, 0);
      NewtonBody* nmapbody = NewtonCreateBody(nWorld, nmapcollision);

      // set the newton world size based on the bsp size
      float boxP0[3];
      float boxP1[3];
      float matrix[4][4];
     matrix4 mmm;
     mmm.setTranslation(vector3df(-1370,-130,-1400)*IrrToNewton);
     mmm.setRotationDegrees(vector3df(0.0f, 90.0f, 0.0f)*IrrToNewton);
     NewtonBodySetMatrix(nmapbody,mmm.pointer());
     NewtonCollisionCalculateAABB (nmapcollision, mmm.pointer(),  &boxP0[0], &boxP1[0]);
    //NewtonSetWorldSize (nWorld, (float*)boxP0, (float*)boxP1);
     NewtonReleaseCollision(nWorld, nmapcollision);
Last edited by RobbyRob on Thu Nov 22, 2007 7:39 pm, edited 1 time in total.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

You might have better luck posting this somewhere other than 'Project Announcements'.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

right, and please put the code between code tags, just copy the code into the text is really hard to read (I tried to read it for a short time and gave up because my eyes starts to hurt with this) !!! ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
RobbyRob
Posts: 21
Joined: Fri Mar 02, 2007 4:30 pm
Location: Germany

Irrlicht terrain collides with Newton

Post by RobbyRob »

Sorry,

I changed to "External stuff", hopefully the right forum and I put the code in tags;
every day I will learn something new but that makes live interesting,

Robert
javascript:emoticon(':oops:')
Embarassed
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

No, keep this position and thread, that's why I moved it here. The External forum is indeed for other game engine elements such as Physic engines, but useful Code snippets are better kept here.
RobbyRob
Posts: 21
Joined: Fri Mar 02, 2007 4:30 pm
Location: Germany

Irrlicht terrain meets Newton collision

Post by RobbyRob »

Okay, now I have changed the basic outfit, perhaps anybody could give me some information about my problem;
I tried it with other x files, rather simple, but nothing changed; when I use bsp-files I can see in the console that it runs with buff 20 and 21 and j counts always 3 times up; but not with the x or obj. files (0,0)
I reduced vArray to [3], but this makes the performance slower;
another interesting fact is when I use the code line

Code: Select all

NewtonSetWorldSize (nWorld, (float*)boxP0, (float*)boxP1);
my Newton cube will be frozen right at the place where it is created (same thing using bsp-files!!);
that´s all for the moment I will go on with my desperate search, but I guess it is also my lack on basic knowledge about the use of mesh buffers,

Robert
Post Reply