Fix for LoadingAnIRRSceneMadeFromIRREditAndApplyingCollision

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
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Fix for LoadingAnIRRSceneMadeFromIRREditAndApplyingCollision

Post by dlangdev »

posted a code fix for the sample code that loads an irr file with meta selectors. it now runs on 1.4, with a minor bug.

i'm still figuring out why the camera is getting stuck, though i got the camera moving around when i changed the height of sceneobject relative to the camera.

http://www.irrlicht3d.org/wiki/index.ph ... gCollision

Code: Select all

#include <irrlicht.h> 
#pragma comment(lib, "Irrlicht.lib") 
#include <iostream> 

// Used for getting Desktop Resolution
#include <windows.h> 

using namespace irr; 
using namespace irr::core; 
using namespace irr::scene; 

IrrlichtDevice *intro = 0; 
video::IVideoDriver* driver = 0; 
scene::ISceneManager* smgr; 
scene::IMetaTriangleSelector* metaSelector; 
scene::ISceneNodeAnimatorCollisionResponse* anim; 
ICameraSceneNode* camera = 0; 

bool fin1; 

void recursiveFillMetaSelector(scene::ISceneNode* node, scene::IMetaTriangleSelector* meta ) 
{ 
  // 
  // the following if is basically the same as ISceneNode_assignTriangleSelector 
  // 
  printf ("Node name is: %s \n",node->getName()); 
  printf ("Node id is: %d \n",node->getID()); 
  printf ("Node type:"); 
  //  printf ("Node type: %s=",smgr->getSceneNodeTypeName()); 
  if (node->getType() ==   ESNT_UNKNOWN) printf("Unknown mesh type \n\n"); 
  if (node->getType() ==   ESNT_MESH) printf("Standard Mesh \n\n"); 
  if (node->getType() ==   ESNT_ANIMATED_MESH) printf("Animated Mesh! \n\n"); 
  if (node->getType() ==   ESNT_SKY_BOX) printf("SkyBox! \n\n"); 
  if (node->getType() ==   ESNT_CAMERA_FPS) printf("Fps Camera! \n\n"); 
  if (node->getType() ==   ESNT_CAMERA_MAYA ) printf("Maya Camera! \n\n"); 
  if (node->getType() ==   ESNT_CAMERA ) 
  { printf("STD Camera! \n"); 
   printf ("The current position of this camera is: %f,%f,%f\n\n",node->getPosition().X,node->getPosition().Y,node->getPosition().Z); 
   camera->setPosition(node->getPosition()); 
  } 
  if (node->getType() ==   ESNT_PARTICLE_SYSTEM ) printf("Particles! \n\n"); 
  if (node->getType() ==   ESNT_LIGHT  ) printf("Light! \n\n"); 
  if (node->getType() ==   ESNT_OCT_TREE) 
  { 
      // Occ Trees are for land 
      printf("Occtree! \n"); 
      io::IAttributes* attribs = intro->getFileSystem()->createEmptyAttributes(); 
      if (attribs) 
        {// get the mesh name out 
         node->serializeAttributes(attribs); 
         core::stringc name = attribs->getAttributeAsString("Mesh"); 
         attribs->drop(); 
         // get the animated mesh for the object 
         scene::IAnimatedMesh* mesh = smgr->getMesh(name.c_str()); 
         if (mesh) 
         { 
            scene::ITriangleSelector* selector = 
            smgr->createOctTreeTriangleSelector(mesh->getMesh(0), node, 128); 
            node->setTriangleSelector(selector); 
            metaSelector->addTriangleSelector(selector); 
            selector->drop(); 
         } 

     } 

  }  
  // now recurse on children... 
  core::list<scene::ISceneNode*>::ConstIterator begin = node->getChildren().begin(); 
  core::list<scene::ISceneNode*>::ConstIterator end   = node->getChildren().end(); 

  for (; begin != end; ++begin) 
    recursiveFillMetaSelector(*begin, meta); 
} 


int main() 
{ 
    // Get the full desktop size from Windows.H 
    // First the width 
    int nWidth = GetSystemMetrics(0); 
    // Second the Height 
    int nHeight = GetSystemMetrics(1); 
    intro = createDevice(video::EDT_DIRECT3D9, core::dimension2d<s32>(1024, 768), 32, false, true, true); 
    driver = intro->getVideoDriver(); 
    smgr = intro->getSceneManager(); 

    core::stringw str = "Level Demo"; 
   intro->setWindowCaption(str.c_str()); 

// Define the Keyboard to use for the camera moves 
    SKeyMap keyMap[9]; 
    keyMap[0].Action = EKA_MOVE_FORWARD; 
   keyMap[0].KeyCode = KEY_UP; 
   keyMap[1].Action = EKA_MOVE_FORWARD; 
   keyMap[1].KeyCode = KEY_KEY_W; 

   keyMap[2].Action = EKA_MOVE_BACKWARD; 
   keyMap[2].KeyCode = KEY_DOWN; 
   keyMap[3].Action = EKA_MOVE_BACKWARD; 
   keyMap[3].KeyCode = KEY_KEY_S; 

   keyMap[4].Action = EKA_STRAFE_LEFT; 
   keyMap[4].KeyCode = KEY_LEFT; 
   keyMap[5].Action = EKA_STRAFE_LEFT; 
   keyMap[5].KeyCode = KEY_KEY_A; 

   keyMap[6].Action = EKA_STRAFE_RIGHT; 
   keyMap[6].KeyCode = KEY_RIGHT; 
   keyMap[7].Action = EKA_STRAFE_RIGHT; 
   keyMap[7].KeyCode = KEY_KEY_D; 

   keyMap[8].Action = EKA_JUMP_UP; 
   keyMap[8].KeyCode = KEY_SPACE ; 

                // Setup the ZIP file "MyLevel.pk3" to be read directly 
       //intro->getFileSystem()->addZipFileArchive("levels/MyLevel.pk3"); 
                // Load the scene file from the archive 
   smgr->loadScene("MyLevel.irr"); 

   camera = smgr->addCameraSceneNodeFPS(0, 120.0f, 120.0f, -1, keyMap, 9, true,0.25f); 
       camera->setRotation(core::vector3df(0,135,0)); 
       camera->setFarValue(11000.0); 
       camera->setAspectRatio(16/10); 
       camera->setFOV(PI/2.5); 
    metaSelector = smgr->createMetaTriangleSelector(); 
   recursiveFillMetaSelector( smgr->getRootSceneNode(), metaSelector ); 
   // DEfine the collision reponse for the FPS Camera 
   anim = smgr->createCollisionResponseAnimator(   metaSelector, camera, core::vector3df(10,26,10), 
      //metaSelector, camera, core::vector3df(10,26,10), 
      core::vector3df(0,-2.0f,0), 
      core::vector3df(0,24,0)); 
   camera->addAnimator(anim); 
   anim->drop(); 
    metaSelector->drop(); 
    while(intro->run() && fin1==false) 
   { 
      if (intro->isWindowActive()) 
      { 
         driver->beginScene(true, true, video::SColor(0,0,0,0)); 
         smgr->drawAll(); 
         driver->endScene(); 
      } 
   } 
   intro->closeDevice(); 
    return 0; 
} 
Image
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

somehow, pressing the space bar during runtime fixes it. a jump actually adjusts the height of the camera to the correct height.
Image
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

there's a performance hit with the new code added if your machine/video card is antiquated.

Code: Select all

#include <irrlicht.h> 
#pragma comment(lib, "Irrlicht.lib") 
#include <iostream> 

// Used for getting Desktop Resolution
#include <windows.h> 

//using namespace irr; 
//using namespace irr::core; 
//using namespace irr::scene; 

using namespace irr;

using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

IrrlichtDevice *intro = 0; 
video::IVideoDriver* driver = 0; 
scene::ISceneManager* smgr; 
scene::IMetaTriangleSelector* metaSelector; 
scene::ISceneNodeAnimatorCollisionResponse* anim; 
ICameraSceneNode* camera = 0; 

bool fin1; 

void recursiveFillMetaSelector(scene::ISceneNode* node, scene::IMetaTriangleSelector* meta ) 
{ 
  // 
  // the following if is basically the same as ISceneNode_assignTriangleSelector 
  // 
  printf ("Node name is: %s \n",node->getName()); 
  printf ("Node id is: %d \n",node->getID()); 
  printf ("Node type:"); 
  //  printf ("Node type: %s=",smgr->getSceneNodeTypeName()); 
  if (node->getType() ==   ESNT_UNKNOWN) printf("Unknown mesh type \n\n"); 
  if (node->getType() ==   ESNT_MESH) printf("Standard Mesh \n\n"); 
  if (node->getType() ==   ESNT_ANIMATED_MESH) printf("Animated Mesh! \n\n"); 
  if (node->getType() ==   ESNT_SKY_BOX) printf("SkyBox! \n\n"); 
  if (node->getType() ==   ESNT_CAMERA_FPS) printf("Fps Camera! \n\n"); 
  if (node->getType() ==   ESNT_CAMERA_MAYA ) printf("Maya Camera! \n\n"); 
  if (node->getType() ==   ESNT_CAMERA ) 
  { printf("STD Camera! \n"); 
   printf ("The current position of this camera is: %f,%f,%f\n\n",node->getPosition().X,node->getPosition().Y,node->getPosition().Z); 
   camera->setPosition(node->getPosition()); 
  } 
  if (node->getType() ==   ESNT_PARTICLE_SYSTEM ) printf("Particles! \n\n"); 
  if (node->getType() ==   ESNT_LIGHT  ) printf("Light! \n\n"); 
  if (node->getType() ==   ESNT_OCT_TREE) 
  { 
      // Occ Trees are for land 
      printf("Occtree! \n"); 
      io::IAttributes* attribs = intro->getFileSystem()->createEmptyAttributes(); 
      if (attribs) 
        {// get the mesh name out 
         node->serializeAttributes(attribs); 
         core::stringc name = attribs->getAttributeAsString("Mesh"); 
         attribs->drop(); 
         // get the animated mesh for the object 
         scene::IAnimatedMesh* mesh = smgr->getMesh(name.c_str()); 
         if (mesh) 
         { 
            scene::ITriangleSelector* selector = 
            smgr->createOctTreeTriangleSelector(mesh->getMesh(0), node, 128); 
            node->setTriangleSelector(selector); 
            metaSelector->addTriangleSelector(selector); 
            selector->drop(); 
         } 

     } 

  }  
  // now recurse on children... 
  core::list<scene::ISceneNode*>::ConstIterator begin = node->getChildren().begin(); 
  core::list<scene::ISceneNode*>::ConstIterator end   = node->getChildren().end(); 

  for (; begin != end; ++begin) 
    recursiveFillMetaSelector(*begin, meta); 
} 


int main() 
{ 
    // Get the full desktop size from Windows.H 
    // First the width 
    int nWidth = GetSystemMetrics(0); 
    // Second the Height 
    int nHeight = GetSystemMetrics(1); 
    intro = createDevice(video::EDT_DIRECT3D9, core::dimension2d<s32>(1024, 768), 32, false, true, true); 
    driver = intro->getVideoDriver(); 
    smgr = intro->getSceneManager(); 

    core::stringw str = "Level Demo"; 
   intro->setWindowCaption(str.c_str()); 

// Define the Keyboard to use for the camera moves 
    SKeyMap keyMap[9]; 
    keyMap[0].Action = EKA_MOVE_FORWARD; 
   keyMap[0].KeyCode = KEY_UP; 
   keyMap[1].Action = EKA_MOVE_FORWARD; 
   keyMap[1].KeyCode = KEY_KEY_W; 

   keyMap[2].Action = EKA_MOVE_BACKWARD; 
   keyMap[2].KeyCode = KEY_DOWN; 
   keyMap[3].Action = EKA_MOVE_BACKWARD; 
   keyMap[3].KeyCode = KEY_KEY_S; 

   keyMap[4].Action = EKA_STRAFE_LEFT; 
   keyMap[4].KeyCode = KEY_LEFT; 
   keyMap[5].Action = EKA_STRAFE_LEFT; 
   keyMap[5].KeyCode = KEY_KEY_A; 

   keyMap[6].Action = EKA_STRAFE_RIGHT; 
   keyMap[6].KeyCode = KEY_RIGHT; 
   keyMap[7].Action = EKA_STRAFE_RIGHT; 
   keyMap[7].KeyCode = KEY_KEY_D; 

   keyMap[8].Action = EKA_JUMP_UP; 
   keyMap[8].KeyCode = KEY_SPACE ; 

	int k1 = 10;

	IGUIEnvironment *guienv = intro->getGUIEnvironment();
	
	IGUIStaticText* info = guienv->addStaticText(L"Exit:  Alt F4", rect<int>(10,k1,200,200),false);
	info->setOverrideColor(video::SColor(255,255,255,255));
	k1 = k1 + 15;
	info = guienv->addStaticText(L"Forward:  Up Arrow", rect<int>(10,k1,200,200),false);
	info->setOverrideColor(video::SColor(255,255,255,255));
	k1 = k1 + 15;
	info = guienv->addStaticText(L"Back:  Down Arrow", rect<int>(10,k1,200,200),false);
	info->setOverrideColor(video::SColor(255,255,255,255));
	k1 = k1 + 15;
	info = guienv->addStaticText(L"Left:  Left Arrow", rect<int>(10,k1,200,200),false);
	info->setOverrideColor(video::SColor(255,255,255,255));
	k1 = k1 + 15;
	info = guienv->addStaticText(L"Right:  Right Arrow", rect<int>(10,k1,200,200),false);
	info->setOverrideColor(video::SColor(255,255,255,255));
	k1 = k1 + 15;
	info = guienv->addStaticText(L"Jump:  SPACE", rect<int>(10,k1,200,200),false);
	info->setOverrideColor(video::SColor(255,255,255,255));

                // Setup the ZIP file "MyLevel.pk3" to be read directly 
       //intro->getFileSystem()->addZipFileArchive("levels/MyLevel.pk3"); 
                // Load the scene file from the archive 
   smgr->loadScene("MyLevel.irr"); 

   camera = smgr->addCameraSceneNodeFPS(0, 120.0f, 120.0f, -1, keyMap, 9, true,0.25f); 
       camera->setRotation(core::vector3df(0,135,0)); 
       camera->setFarValue(11000.0); 
       camera->setAspectRatio(16/10); 
       camera->setFOV(PI/2.5); 
    metaSelector = smgr->createMetaTriangleSelector(); 
   recursiveFillMetaSelector( smgr->getRootSceneNode(), metaSelector ); 
   // DEfine the collision reponse for the FPS Camera 
   anim = smgr->createCollisionResponseAnimator(   metaSelector, camera, core::vector3df(10,26,10), 
      //metaSelector, camera, core::vector3df(10,26,10), 
      core::vector3df(0,-2.0f,0), 
      core::vector3df(0,24,0)); 
   camera->addAnimator(anim); 
   anim->drop(); 
    metaSelector->drop(); 
    while(intro->run() && fin1==false) 
   { 
      if (intro->isWindowActive()) 
      { 
         driver->beginScene(true, true, video::SColor(0,0,0,0)); 
         smgr->drawAll(); 
         guienv->drawAll();
         driver->endScene(); 
      } 
   } 
   intro->closeDevice(); 
    return 0; 
} 
Image
Dronchik
Posts: 36
Joined: Sun Feb 17, 2008 1:30 pm
Location: Kazahstan::Astana
Contact:

Post by Dronchik »

dlangdev, I have copied your first code. But it does not work. At me the chamber falls through .irr a stage. Is not present collisions. Collect please an example for uploading where is collisions .irr stages.
P.S.
For that as I on English language speak do not abuse. I only learn English language.
Post Reply